I am wondering about the following: I have a method that takes a float as input and returns a float after some calculation. The calculations may involve anything like addition, division, logarithms, powers etcetera. While doing so, the intermediate result needs to be stored several times. Something crazy like this:
float performSomeComputations(float value)
{
// addition, substraction, logarithms, multiplication, etcetera
float a = value * 12.0;
float b = math.pow(a, 34.0);
float c = math.log10(b + value - 567);
return a + b + c;
}
My question is: does it matter how you store the intermediate results? I.e. as float
or as double
? Let us assume that the intermediate values don't exceed the minimum and/or maximum value, obviously there will be a difference then. But would the return value be more accurate if double
types are used, or would you get always exactly the same answer, after it's finally cast back to float
? And is it possible that the answer could vary per programming language?