Skip to content

Commit

Permalink
Fix scala Erf & add Expm1
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenk committed Apr 12, 2012
1 parent d4153fe commit 0fd73f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion scala/SConscript
Expand Up @@ -3,7 +3,7 @@ PACKAGE_NAME = "picomath"
#This is kind of a hack to deal with the lack of scons support for the
#autogenerated class files from scala that don't directly map to
#.scala files (i.e. Erf$.class)
bld = Builder(action = 'jar cfm picomath.jar MANIFEST classes/Erf$.class -C classes Erf.class classes/Main$.class -C classes Main.class')
bld = Builder(action = 'cd scala;jar cfm picomath.jar MANIFEST classes/Erf$.class -C classes Erf.class classes/Main$.class -C classes Main.class -C classes Expm1.class classes/Expm1$.class')
env = Environment(JAVAC = "scalac", JAVASUFFIX=".scala")
env.Append(BUILDERS = {'hobojar' : bld})
env.Java('classes', 'src')
Expand Down
3 changes: 2 additions & 1 deletion scala/src/Erf.scala
@@ -1,5 +1,6 @@
import scala.math

//Calculates the error function using Horner’s method.
class Erf {
// constants
val a1: Double = 0.254829592;
Expand All @@ -11,7 +12,7 @@ class Erf {

def erf(x: Double): Double = {
// Save the sign of x
val sign = if (x < 0) 1 else 0
val sign = if (x < 0) -1 else 1
val absx = math.abs(x)

// A&S formula 7.1.26
Expand Down
14 changes: 14 additions & 0 deletions scala/src/Expm1.scala
@@ -0,0 +1,14 @@
import scala.math

//Calculates the value of the natural logarithm base (e) raised to the power of x, minus 1.
class Expm1 {
def expm1(x: Double): Double = {
if (math.abs(x) < 1e-5) {
return x + 0.5*x*x;
} else {
return math.exp(x) - 1.0;
}
}
}

object Expm1 extends Expm1
4 changes: 2 additions & 2 deletions scala/src/Main.scala
Expand Up @@ -11,9 +11,9 @@ object Main extends App {
val x: Double = a.apply(1).toDouble;
a.apply(0) match {
case "erf" => println(Erf.erf(x));
case _ => println("Unknown function: " + a.apply(0));
case "expm1" => println(Expm1.expm1(x));
case _ => { println("Unknown function: " + a.apply(0)); return;}
}
return;
}
}
}

0 comments on commit 0fd73f3

Please sign in to comment.