Varnn1807
Varnn1807

Reputation: 3

Floating point precision in Visual Studio 2008 and Xcode

In visual studio 2008 I have for example this

float a1 = 1.2f/1.8f;
float a2 = 450.f;
float a3 = a1*a2;

float res1 = 350.f - a3;
float res2 = 350.f - a1*a2;

Values from res1 and res2 give different results, and from what I have read this is because a1*a2 ( in res2) is done in higher precision and it is different than the value that we save in a3.

On Xcode

If we have the same set, res1 and res 2 give the same result

How can we set up in Xcode to have the same results as in visual studio and as well how to set up in visual studio to have the same results as in Xcode?

Thank you

Edit: What I have found in visual studio to give both the same result is:

float res1 = 350.f - a3;
float res2 = 350.f - (float)(a1*a2);

So it means without (float) a1*a2 is done in higher precision is subtracted from 350.f and the result is then "scaled" to float. With (float) a1*a2 is done in higher precision is scaled first to float then is subtracted from 350.f in higher precision and then "scaled" back to float.

Upvotes: 0

Views: 500

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129374

Edit to actually answer the question: I doubt there is a GUARANTEED way to always get the same calculation produce the exact same result with different compilers - the compiler WILL split/combine various steps of calculation as it sees fit. /EDIT

This all comes down to EXACTLY how the compiler optimises and arranges the instructions. Unless you know very well what you are doing (and what the compiler will do), any floating point calculation will need to allow for small errors that are introduced during calculation steps. Note however that I would expect that even the lowest level of optimisation to have the compiler calculate a1 * a2 ONCE, and not twice. This is called "CSE" or "Common Sub-expression" optimisation (same calculation being done several times in a block of code). So I'm guessing you are testing this in a "non-optimised" build. (There are cases where compiler may not optimise things because it produces a different result, but this doesn't look like one of those to me).

However, it is entirely true that on a lot of systems, the actual calculation of floating point values is done with higher precision and then "shortened" to the actual size of float or double or whatever when the value is store. This can lead to results being different when you expect the same result, because the calculation is done in different order or different ways, leading to the actual value being slightly different.

Upvotes: 1

Related Questions