Skip to content

Commit

Permalink
scientific functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolony committed Mar 16, 2017
1 parent 155d2bf commit 4cca633
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Acos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bytezone.diskbrowser.visicalc;

public class Acos extends Function
{
Value v;

Acos (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.acos (v.getValue ());

if (Double.isNaN (value))
valueType = ValueType.ERROR;
}
}
30 changes: 30 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Asin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bytezone.diskbrowser.visicalc;

public class Asin extends Function
{
Value v;

Asin (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.asin (v.getValue ());

if (Double.isNaN (value))
valueType = ValueType.ERROR;
}
}
30 changes: 30 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Atan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bytezone.diskbrowser.visicalc;

public class Atan extends Function
{
Value v;

Atan (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.atan (v.getValue ());

if (Double.isNaN (value))
valueType = ValueType.ERROR;
}
}
9 changes: 8 additions & 1 deletion src/com/bytezone/diskbrowser/visicalc/Choose.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public class Choose extends Function
public void calculate ()
{
source.calculate ();
Address address = range.get ((int) source.getValue () - 1);
int index = (int) source.getValue () - 1;
if (index < 0 || index >= range.size ())
{
valueType = ValueType.NA;
return;
}

Address address = range.get (index);
if (address == null)
valueType = ValueType.NA;
else
Expand Down
27 changes: 27 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Cos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bytezone.diskbrowser.visicalc;

public class Cos extends Function
{
Value v;

Cos (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.cos (v.getValue ());
}
}
30 changes: 30 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Exp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bytezone.diskbrowser.visicalc;

public class Exp extends Function
{
Value v;

Exp (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.exp (v.getValue ());

if (Double.isNaN (value))
valueType = ValueType.ERROR;
}
}
2 changes: 2 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ static String format (Value value, char formatChar, int colWidth)
val = val.substring (0, val.length () - 1);
if (val.startsWith ("0."))
val = val.substring (1);
if (val.startsWith ("-0."))
val = "-" + val.substring (2);

if (val.length () > colWidth && val.indexOf ('.') >= 0)
val = val.substring (0, colWidth);
Expand Down
27 changes: 27 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ static Function getInstance (Sheet parent, Cell cell, String text)
if (text.startsWith ("@ABS("))
return new Abs (parent, cell, text);

if (text.startsWith ("@ACOS("))
return new Acos (parent, cell, text);

if (text.startsWith ("@AND("))
return new And (parent, cell, text);

if (text.startsWith ("@ASIN("))
return new Asin (parent, cell, text);

if (text.startsWith ("@ATAN("))
return new Atan (parent, cell, text);

if (text.startsWith ("@AVERAGE("))
return new Average (parent, cell, text);

Expand All @@ -54,9 +63,15 @@ static Function getInstance (Sheet parent, Cell cell, String text)
if (text.startsWith ("@CHOOSE("))
return new Choose (parent, cell, text);

if (text.startsWith ("@COS("))
return new Cos (parent, cell, text);

if (text.startsWith ("@ERROR"))
return new Error (parent, cell, text);

if (text.startsWith ("@EXP"))
return new Exp (parent, cell, text);

if (text.startsWith ("@IF("))
return new If (parent, cell, text);

Expand All @@ -69,9 +84,15 @@ static Function getInstance (Sheet parent, Cell cell, String text)
if (text.startsWith ("@ISNA("))
return new IsNa (parent, cell, text);

if (text.startsWith ("@LOG10("))
return new Log10 (parent, cell, text);

if (text.startsWith ("@LOOKUP("))
return new Lookup (parent, cell, text);

if (text.startsWith ("@LN("))
return new Ln (parent, cell, text);

if (text.startsWith ("@MIN("))
return new Min (parent, cell, text);

Expand All @@ -90,12 +111,18 @@ static Function getInstance (Sheet parent, Cell cell, String text)
if (text.startsWith ("@PI"))
return new Pi (parent, cell, text);

if (text.startsWith ("@SIN("))
return new Sin (parent, cell, text);

if (text.startsWith ("@SUM("))
return new Sum (parent, cell, text);

if (text.startsWith ("@SQRT("))
return new Sqrt (parent, cell, text);

if (text.startsWith ("@TAN("))
return new Tan (parent, cell, text);

System.out.printf ("Unknown function: [%s]%n", text);
return new Error (parent, cell, "@ERROR");
}
Expand Down
27 changes: 27 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Ln.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bytezone.diskbrowser.visicalc;

public class Ln extends Function
{
Value v;

Ln (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.log (v.getValue ());
}
}
27 changes: 27 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Log10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bytezone.diskbrowser.visicalc;

public class Log10 extends Function
{
Value v;

Log10 (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.log10 (v.getValue ());
}
}
2 changes: 1 addition & 1 deletion src/com/bytezone/diskbrowser/visicalc/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public int size ()

public Address get (int index)
{
return range.get (index);
return index < 0 || index >= range.size () ? null : range.get (index);
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Sin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bytezone.diskbrowser.visicalc;

public class Sin extends Function
{
Value v;

Sin (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.sin (v.getValue ());
}
}
27 changes: 27 additions & 0 deletions src/com/bytezone/diskbrowser/visicalc/Tan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bytezone.diskbrowser.visicalc;

public class Tan extends Function
{
Value v;

Tan (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);

v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;
}

@Override
public void calculate ()
{
v.calculate ();
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}

value = Math.tan (v.getValue ());
}
}

0 comments on commit 4cca633

Please sign in to comment.