Jump to content
  • Advertisement
Sign in to follow this  
Darkbouncer4689

Different values on linux vs windows (C++)

This topic is 2604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I implemented an algorithm in visual studios 2010 on windows and get one answer, then I sent all of the .cpp and .h over to my linux box and recompiled using gcc and I get a slightly different answer >_<

The algorithm involves a lot of floating point numbers, divisions, multiplications, sin and cos calculations. I'm assuming it has to do with floating point rounding?

Is this a linux vs windows thing or a VS2010 vs gcc thing?

Thanks!
Dark


Share this post


Link to post
Share on other sites
Advertisement
This is down to the compiler. Certain optimizations are permissible in certain circumstances, and each compiler has different philosophies on what optimizations it will allow by default. Try changing your compiler settings under "floating point accuracy model" to see if it affects how close your results are.


In general, though, the real problem is relying on floating point arithmetic for anything that requires any form of accuracy or appreciable precision.

Share this post


Link to post
Share on other sites
It could be a hardware issue. Intel and AMD machines handle floating point slightly differently, so you shouldn't expect identical answers.

It could be a compiler optimisation issue. Float point error will accumulate differently depending on the exact order and nature of operations. Comparing the generated assembly might be illuminating.

I'm not aware of anything particularly OS specific about floating point handling, but indeed there may be, adding yet another variable to the equation.

That said, by simply using floating point, you've already lost. You've made the choice to sacrifice representational accuracy for efficient computation and storage of a large range of values. If you need the values to match, then perhaps using native float operations isn't for you. A fixed point or bignum solution might be better suited, depending on your requirements and constraints.

Using doubles may help, but this really just pushes off the precision issues. Though most of the time, you won't need more precision than a double offers.

Share this post


Link to post
Share on other sites
Thanks rip-off. So I'm assuming a game engine will implement it's own floating point precision techniques in software to assure consistency throughout different platforms.

Share this post


Link to post
Share on other sites
Nope.

If consistency is required, you use fixed-point or a custom numerics library. Fixed-point is probably the most common, because integer operations are already fast on basically every modern architecture, and don't involve code overhead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!