what languages expose IEEE 754 traps to the developer?

后端 未结 4 1039
天命终不由人
天命终不由人 2021-02-14 22:38

I\'d like to play with those traps for educational purpose.

A common problem with the default behavior in numerical calculus is that we \"miss\" the Nan (or +-inf) that

相关标签:
4条回答
  • 2021-02-14 23:05

    C and probably most languages derived from it like C++ or python (may be indirect access though). It's probably reasonable to expect that low level languages will have such support.

    See http://www.math.utah.edu/~beebe/software/ieee/#c-notes which has numerous scripts and notes on working with IEEE 754 numbers. In particular of1.c deals with floating point exceptions. Finally, from the source http://grouper.ieee.org/groups/754/reading.html which includes a bunch of useful info.

    0 讨论(0)
  • 2021-02-14 23:06

    I'm unsure of what the standard is, but I can tell you what I've seen from experience as it may be useful. I have coded in C++ and NaN's are sometimes my worst nightmare. They appear silently and propogate through the computation all the way to the end, until I just have useless output. I've often had to create additional code to specifically detect the NaN-causing circumstances. I'm using Visual C++ 2008, so I expect that it'd be following the IEEE standard in this manner.

    0 讨论(0)
  • 2021-02-14 23:11

    Maple's programming language has a numeric model that respects IEEE-754 and allows you to set your own trap handlers, if you want. Here are some links:

    • Differences between IEEE-754 and Maple's numerics (they're pretty minor - it's mostly just names of functions)
    • General info on numeric computation in Maple
    • Info on how to obtain & set numeric event handlers (the Maple term for trap handlers)

    An uncommon property of Maple is that default floating point numbers are decimal (not binary) and of arbitrary precision. If you want to deal with 64-bit binary floating point numbers, wrap them in HFloat. For example, 0.2 represents the decimal number exactly, whereas HFloat(0.2) represents the same number you'd get by assigning 0.2 to a double in C. This is evident by running, for example,

    a := HFloat(0.2);
    b := 0.2;
    evalf[20](a - b);
    

    This computes the difference between a and b using 20 decimal digit arithmetic, and the result is 0.11E-16.

    0 讨论(0)
  • 2021-02-14 23:14

    As far as I know, you have two choices for floating point exception handling in C and C++:

    First, if you disable/mask floating point exceptions (which most environments do by default), you can see whether any floating point exceptions have occurred by calling fetestexcept. fetestexcept isn't available in Visual C++, but you can steal the MinGW Runtime's implementation easily enough. (It's in the public domain.) Once an exception has been flagged, it's not cleared until you call feclearexcept, so you can call fetestexcept at the end of a series of calculations to see if any of them raised an exception. This doesn't give you the traps that you asked for, but it does let you test if problems like NaN or +/-inf have occurred and react as needed.

    Second, you can enable/unmask floating point exceptions by calling feenableexcept in Linux or _controlfp in Windows. How the operating system handles a processor-generated floating point exception depends on your operating system.

    • In Linux, the OS sends a SIGFPE signal, so you can install a signal handler to catch that and set a flag that tells your routine to react appropriately.
    • In Windows, the OS invokes Structured Exception Handling to convert the processor exception into a language exception that you can catch using a __try / __catch block in C or try / catch block in C++.
    • Update: For Mac OS X, as described in this answer, you should be able to enable/unmask exceptions using _MM_SET_EXCEPTION_MASK from xmmintrin.h, and as long as you use the default compiler options (i.e., don't disable SSE), you should be able to catch exceptions using SIGFPE.

    (I've written a bit more on this and other floating point issues in C and C++ in this blog posting if you're curious.)

    0 讨论(0)
提交回复
热议问题