I have a function that accepts a float, I do some calculations with them, and I would like to keep as much accuracy as possible in the returned result. I read that when you multiply two floats, you double the number of significant digits.
So, when two floats get multiplication, for example, float e, f;and I do double g = e * fwhen the bits are truncated?
In my sample function below, I need casting, and if so, where? This is in a tight inner loop, if I put static_cast<double>(x)around every variable a b c dwhere it is used, I get 5-10% slowdown. But I suspect that I do not need to throw each variable separately, and only in some places, if at all? Or is the return of the double here not giving me any benefit in any way, and can I just return the float?
double func(float a, float b, float c, float d) {
return (a - b) * c + (a - c) * b;
}
source
share