More NaN handling

This commit is contained in:
Denis Molony 2016-07-21 21:28:22 +10:00
parent 0ee07d662a
commit 1c199f3c5c
5 changed files with 42 additions and 9 deletions

View File

@ -121,12 +121,10 @@ class Cell implements Comparable<Cell>, Value
return justify (repeat, colWidth); return justify (repeat, colWidth);
case VALUE: case VALUE:
if (value.isError () || value.isNotAvailable ()) if (value.isError () || value.isNotAvailable () || value.isNotANumber ())
return justify (value.getText (), colWidth); return justify (value.getText (), colWidth);
Double thisValue = value.getValue (); Double thisValue = value.getValue ();
if (thisValue.isNaN ())
return justify ("NaN", colWidth);
char format = cellFormat != ' ' ? cellFormat : defaultFormat; char format = cellFormat != ' ' ? cellFormat : defaultFormat;
if (format == 'I') if (format == 'I')
@ -210,6 +208,15 @@ class Cell implements Comparable<Cell>, Value
return value.isNotAvailable (); return value.isNotAvailable ();
} }
@Override
public boolean isNotANumber ()
{
// assert type == CellType.VALUE : "Cell type: " + type;
// if (!isValue ())
// return true;
return value.isNotANumber ();
}
@Override @Override
public Value calculate () public Value calculate ()
{ {

View File

@ -173,7 +173,11 @@ class Expression implements Value
else if (operator.equals ("^")) else if (operator.equals ("^"))
value = Math.pow (value, nextValue); value = Math.pow (value, nextValue);
} }
valueType = ValueType.VALUE;
if (Double.isNaN (value))
valueType = ValueType.NAN;
else
valueType = ValueType.VALUE;
} }
catch (Exception e) catch (Exception e)
{ {
@ -202,6 +206,12 @@ class Expression implements Value
return valueType == ValueType.NA; return valueType == ValueType.NA;
} }
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
}
@Override @Override
public boolean isError () public boolean isError ()
{ {
@ -211,14 +221,15 @@ class Expression implements Value
@Override @Override
public double getValue () public double getValue ()
{ {
assert valueType == ValueType.VALUE : "Expression ValueType = " + valueType; // assert valueType == ValueType.VALUE : "Expression ValueType = " + valueType;
return value; return value;
} }
@Override @Override
public String getText () public String getText ()
{ {
return isNotAvailable () ? "NA" : isError () ? "Error" : ""; return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
// return isNotAvailable () ? "NA" : isError () ? "Error" : "";
} }
private String checkBrackets (String input) private String checkBrackets (String input)

View File

@ -139,6 +139,12 @@ abstract class Function implements Value
return valueType == ValueType.NA; return valueType == ValueType.NA;
} }
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
}
@Override @Override
public double getValue () public double getValue ()
{ {
@ -150,7 +156,7 @@ abstract class Function implements Value
@Override @Override
public String getText () public String getText ()
{ {
return isNotAvailable () ? "" : isError () ? "Error" : ""; return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
} }
protected Range getRange (String text) protected Range getRange (String text)

View File

@ -36,6 +36,12 @@ class Number implements Value
return valueType == ValueType.NA; return valueType == ValueType.NA;
} }
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
}
@Override @Override
public String toString () public String toString ()
{ {
@ -51,7 +57,8 @@ class Number implements Value
@Override @Override
public String getText () public String getText ()
{ {
return valueType == ValueType.ERROR ? "Error" : ""; return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
// return valueType == ValueType.ERROR ? "Error" : "";
} }
@Override @Override

View File

@ -4,7 +4,7 @@ interface Value
{ {
enum ValueType enum ValueType
{ {
VALUE, ERROR, NA VALUE, ERROR, NA, NAN
} }
public ValueType getValueType (); public ValueType getValueType ();
@ -19,5 +19,7 @@ interface Value
public boolean isNotAvailable (); public boolean isNotAvailable ();
public boolean isNotANumber ();
public Value calculate (); public Value calculate ();
} }