-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
float division by zero #395
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
Comments
Relevant: https://en.wikipedia.org/wiki/IEEE_floating_point#Exception_handling Sometimes |
Floating point math is certainly a tricky subject. There was some discussion here tiehuis/zig-fmath#7 on which was decided at least for the moment not to expose an exception testing interface. This can be added at a later time without much issue I can see. The system c library should be able to be used in the case that fenv access is required at least for the moment. To me it seems like we should allow division by zero to occur, and also overflow/underflow for floats, even in debug mode. As an example, Rust does this by default and if we are trying to conform to IEEE by default now it makes sense. |
I agree that there is value in conforming to IEEE. But I also think that dividing by zero is almost always a bug. What do you think about using setFloatMode to control this? Currently If the programmer sets |
I think that sounds reasonable. Given that |
Is setting modes the best design? i understand there's a precedent already for it, but it might be worth at least discussing before diving in. what's the scope of the state? is it compiletime or runtime? is it local to the thread or library? generally setting a mode is a design smell, so I just wanted to get some confirmation that this is really what we want. |
As far as I am aware, this is a compile-time option specific to the scope that is passed to the builtin. I'm actually unsure the valid scope options that can be used (besides This is a pretty good overview of the exact things that it allows the compiler to assume. I think this is okay to set via a mode, since it would only be required to be adjusted when a user explicitly needs IEEE behavior for nan/inf or is dealing with sub-normals or another less-known feature. I would think this is the minority compared to standard float usage. Also, what is the original rationale for preferring fast-math in the general case? I'm inclined to see it as the better option in the case of usual floating point usage, but extra justification and reassurance is always good. |
What's the alternative proposal here? Different operators like wrapping integer arithmetic? To be clear, the precedent set is setting the floating point mode at the compile unit scope using command line options. Having the floating point mode set in the source at block scope level is bringing something new to the table.
|
My initial question on this is at least answered. I think the runtime error on division by zero does make sense, as long as we are consistent across all modes (release vs. debug). I've also made changes in the math library to not rely on this behavior at all, so okay on that end, too. |
Currently division by zero of floats will be caught at runtime and the program will abort when encountered. However, most other languages will allow float division by zero and instead generate a signalling nan with the FPU div by zero error set.
This is currently used in a few places in the math library as a result of musl relying on division by zero to signal nans, and so division by zero errors will occur with valid inputs. The implementation here though is not a huge concern, since I can manually set whatever exceptions we need and rewrite the code to avoid them internally.
The question is more about what behavior is considered right and we should follow so the user gets the least surprise. It is probably more correct to allow for float division by zero to be valid and not panic when encountered.
Thoughts?
The text was updated successfully, but these errors were encountered: