In my code I started noticing that the same initial conditions would output different results.
After hours of hunting, I found where the bug occurs.
I have a simple function, which takes a few parameters, one of which is v. v is a double.
One of the calculations inside that function is Math.pow(v, 2).
After writing some code that recorded all the inputs of that function and all the intermediate calculations inside that function, I've discovered that v is always equal -54.35178459279, but sometimes
Math.pow(-54.35178459279, 2) = 2954.116488421044
and sometimes (look at the ending 3)
Math.pow(-54.35178459279, 2) = 2954.1164884210443
What? What's going on?
I've tried running Math.pow(-54.35178459279, 2) in an infinite loop to see if it sometimes gives a different output. Well, it doesn't.
My only guesses so far is that either:
a. My debugger does not show all the digits in a number
b. That even though the debugger shows the same representation for vs, the underlying scala double is slightly different.
For those who really want an example, this is really the best I can do:
val precision = 1000000000000.0
def step(v: Double) = {
val newV = Math.round(calcV(v)*precision) / precision
step(newV)
}
def calcV(v: Double) = Math.pow(v, 2)
Just got a clue:
scala> -54.35178459279 * -54.35178459279
res0: Double = 2954.1164884210443
scala> Math.pow(-54.35178459279, 2)
res0: Double = 2954.116488421044