user240092
user240092

Reputation:

Floating point precision in Visual C++

HI,

I am trying to use the robust predicates for computational geometry from Jonathan Richard Shewchuk.

I am not a programmer, so I am not even sure of what I am saying, I may be doing some basic mistake.

The point is the predicates should allow for precise aritmthetic with adaptive floating point precision. On my computer: Asus pro31/S (Core Due Centrino Processor) they do not work. The problem may stay in the fact the my computer may use some improvements in the floating point precision taht conflicts with the one used by Shewchuk. The author says:

/* On some machines, the exact arithmetic routines might be defeated by the  */
/*   use of internal extended precision floating-point registers.  Sometimes */
/*   this problem can be fixed by defining certain values to be volatile,    */
/*   thus forcing them to be stored to memory and rounded off.  This isn't   */
/*   a great solution, though, as it slows the arithmetic down.              */

Now what I would like to know is that there is a way, maybe some compiler option, to turn off the internal extended precision floating-point registers.

I really appriaciate your help

Upvotes: 8

Views: 7067

Answers (5)

Stephen Canon
Stephen Canon

Reputation: 106317

As others have noted, you can deal with this by setting the x87 control word to limit floating point precision. However, a better way would be to get MSVC to generate SSE/SSE2 code for the floating-point operations; I'm surprised that it doesn't do that by default in this day and age, given the performance advantages (and the fact that it prevents one from running into annoying bugs like what you're seeing), but there's no accounting for MSVC's idiosyncrasies.

Ranting about MSVC aside, I believe that the /arch:SSE2 flag will cause MSVC to use SSE and SSE2 instructions for single- and double-precision arithmetic, which should resolve the issue.

Upvotes: 1

MSN
MSN

Reputation: 54634

_control87(_PC_53, _MCW_PC) or _control87(_PC_24, _MCW_PC) will do the trick. Those set the precision to double and single respectively with MSVC. You might want to use _controlfp_s(...), as that allows you to retrieve the current control word explicitly after setting it.

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 942307

Yes, you'll have to change the FPU control word to avoid this. It is explained well for most popular compilers in this web page. Beware that this is dramatically incompatible with what most libraries expect the FPU to do, don't mix and match. Always restore the FPU control word after you're done.

Upvotes: 3

JoeG
JoeG

Reputation: 13192

The complier option you want for Visual Studio is /fp:strict which is exposed in the IDE as Project->Properties->C/C++->Code Generation->Floating Point Model

Upvotes: 3

Michael Burr
Michael Burr

Reputation: 340446

If you're using GCC, the SO answer here might help:

If you're using another compiler, you might be able to find some clues in that example (or maybe post a comment to that answer to see if Mike Dinsdale might know.

Upvotes: 0

Related Questions