Is there any way to make sure that the output of a floating point is the same in different OS?

Here is my code:

int a = 0x451998a0; float b = *((float *)&a); printf("coverto float: %f, %.10lf\n", b, b); 

In windows output:

 coverto float: 2457.539063, 2457.5390625000 

In linux, the output is:

 coverto float: 2457.539062, 2457.5390625000 

Is there a way to make sure the output is the same?

+4
source share
3 answers

The behavior you see is a consequence of the fact that the Windows printf() function is implemented differently than the Linux printf() function. Most likely, the difference is how printf() implements rounding of numbers.

How printf() works under the hood in any system is an implementation detail; thus, the system is unlikely to provide such small-scale control over how printf() displays floating point values.

There are two ways to keep them the same:

  • Use more precision during the calculation than when displaying it. For example, some scientific and graphing calculators use double precision for all internal calculations, but only display results with float precision.

  • Use the cross-platform library printf() . Such libraries are likely to have the same behavior on all platforms, since the calculations needed to determine which numbers to display are usually platform agnostic.

However, this is really not such a big problem as you think. The difference between the outputs is 0.000001. This is minus 0.0000000004% of both values. The display error is very slight.

Consider this: the distance between Los Angeles and New York is 2464 miles , which is in the same order of magnitude as the numbers in your display outputs. The difference of 0.000001 miles is 1.61 millimeters. Of course, we do not measure the distance between cities with such accuracy. :-)

+7
source

If you use the same printf () function, they have a good chance of showing the same result. Depending on what you are doing, it may be easier to use the GNU GCC for both operating systems or get the printf () source code and add it to your project (you should not have problems finding one at a time).

BTW - did you really verify that it encodes this hexadecimal number? Should it round up or down? The 625 thing is probably rounded itself, so you should not assume that it should round to 63 ....

0
source

The obvious answer is to use less precision in your work. In general, if there are any calculations, you cannot even be sure that the actual floating point values ​​are identical. And both printf and ostream round are implementations, even if floating point values ​​are equal.

In general, C ++ does not guarantee that two implementations produce the same results. In this particular case, if it is important, you can do the rounding manually before doing the conversion, but you will still have random problems, since the actual floating point values ​​will be different. This can happen even at different levels of optimization with the same compiler. So, everything you try (except writing the entire program in assembler) will inevitably be a battle at the end.

0
source

Source: https://habr.com/ru/post/1433296/


All Articles