Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: expose flags set by floating-point exceptions #2976

Open
1 of 3 tasks
jiahao opened this issue Apr 30, 2013 · 13 comments
Open
1 of 3 tasks

Proposal: expose flags set by floating-point exceptions #2976

jiahao opened this issue Apr 30, 2013 · 13 comments
Labels
help wanted Indicates that a maintainer wants help on an issue or pull request maths Mathematical functions

Comments

@jiahao
Copy link
Member

jiahao commented Apr 30, 2013

There is currently no Base function that allows users to check for the flags set by floating-point operations.1 This does not allow the user to compute in a fully IEEE-compatible manner and could decrease code legibility should the user be forced to test implicitly for a raised flag by inspecting the consequences of the computation.

This continues a discussion started in #2085, and extends the concept of overflow checking proposed for integers in #855.

  • Detect the five flags raised by floating-point exceptions
    • Invalid
    • Division by zero
    • Overflow
    • Underflow
    • Inexact
  • Allow the floating-point rounding mode to be set:
    • round to nearest, ties round to the nearest even digit in the required position
    • round to nearest, ties round away from zero
    • round up toward Inf
    • round down toward -Inf
    • round toward 0
  • [ ] Honor the difference between quiet and signaling NaNs
  • Expose similar flag-handling behavior from MPFR

cc: @andrioni @JeffBezanson

@andrioni
Copy link
Member

I'd really like the flags to be extendable, if possible, as MPFR also uses a range one (for impossible conversions, i.e. a NaN to int), and flags seem to be a good alternative to return the rounding of the last operation.

I'm not sure though what's the best way to implement them, my first idea for MPFR was using a ENV-like dict and/or constants, as the precision is currently stored.

@andrioni
Copy link
Member

Also, I may be wrong (and I do hope so), but I think LLVM just ignores the floating point flags, so we would have to detect and trap them manually in intrinsics.cpp.

@bkalpert
Copy link
Contributor

bkalpert commented May 3, 2013

This capability would seem to go well with the capability to set the floating-point rounding mode (to nearest, upward, downward, toward zero), which is very helpful for checking the stability of a numerical code (and is a key component of the IEEE 754 standard). A bonus is that it would distinguish Julia, for it is unavailable in most other high-level environments.

@jiahao
Copy link
Member Author

jiahao commented May 3, 2013

@bkalpert yeah, that would be nice. I've added that as a todo item

@andrioni
Copy link
Member

andrioni commented May 3, 2013

Some[1] [2] references on changing the rounding mode on LLVM.

@StefanKarpinski
Copy link
Member

We will definitely do this – it's absolutely necessary for getting the Kahan stamp of approval (or even getting close).

@ViralBShah
Copy link
Member

For setting the floating point environment, how feasible is it to ccall the various fenv functions? eg. fegetround, fesetround, etc.?

@andrioni
Copy link
Member

I think they wouldn't work on Windows and/or Visual C++, as fenv.h is POSIX/C99.

@andrioni
Copy link
Member

Also,

julia> ccall(:fegetround, Cint, ())
0

julia> ccall(:fesetround, Cint, (Cint, ), 1)
1

julia> ccall(:fegetround, Cint, ())
0

@andrioni
Copy link
Member

Ah, binary enum:

julia> ccall((:fesetround, :libm), Cint, (Cint,), 1024)
0

julia> ccall((:fegetround, :libm), Cint, ())
1024

Seems good!

@bkalpert
Copy link
Contributor

Looks like excellent progress!

StefanKarpinski added a commit that referenced this issue Sep 2, 2013
Ability to change rounding mode for all floats (cf. #2976)
@quinnj
Copy link
Member

quinnj commented Jun 10, 2014

Bump. @jiahao, is the TODO list at the top still up to date? Anyone feel like tackling this?

@andrioni
Copy link
Member

It is nearly finished in #6170, IIRC, just needs some final touches and a merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Indicates that a maintainer wants help on an issue or pull request maths Mathematical functions
Projects
None yet
Development

No branches or pull requests

7 participants