I am porting some code to Scala, which makes extensive use of floating point arithmetic. I wrote the following code in Scala based on copy / paste version of C:
val complimentaryTerms = 2640.96e-6 * sin (f5) + 63.52e-6 * sin (2.0 * f5) + 11.75e-6 * sin (2.0 * f3 - 2.0 * f4 + 3.0 * f5) + 11.21e-6 * sin (2.0 * f3 - 2.0 * f4 + f5) - 4.55e-6 * sin (2.0 * f3 - 2.0 * f4 + 2.0 * f5) + 2.02e-6 * sin (2.0 * f3 + 3.0 * f5) + 1.98e-6 * sin (2.0 * f3 + f5) - 1.72e-6 * sin (3.0 * f5) - 0.87e-6 * t * sin (f5)
The result of this calculation is a little from what version C produces. However, if I enclose the expression in parentheses, for example:
val complimentaryTerms = (2640.96e-6 * sin (f5) + 63.52e-6 * sin (2.0 * f5) + 11.75e-6 * sin (2.0 * f3 - 2.0 * f4 + 3.0 * f5) + 11.21e-6 * sin (2.0 * f3 - 2.0 * f4 + f5) - 4.55e-6 * sin (2.0 * f3 - 2.0 * f4 + 2.0 * f5) + 2.02e-6 * sin (2.0 * f3 + 3.0 * f5) + 1.98e-6 * sin (2.0 * f3 + f5) - 1.72e-6 * sin (3.0 * f5) - 0.87e-6 * t * sin (f5))
the resulting value exactly matches version C. It seems the order of operations should be different when there are parentheses or not, but I don't understand why this matters. Any idea what is going on here?
scala
anelson
source share