-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
round ties behaviour #8750
Comments
FWIW, I suspect most scientific users will expect (a) to happen. It would be nice if |
@davidssmith I guess if you define "scientific users" as C/Fortran/Matlab. Mathematica/R/Python 3 users might disagree. |
Wow, interesting that Python is changing their rounding behavior. I guess I meant people who learned rounding in math class, not CS class. I think |
@simonbyrne: first, that table is amazing. Really nice work. Second, when it comes to floating point, I'd just do whatever @simonbyrne told me to do...oh, right, that's you. Seriously, I'd put a lot of faith in whatever you think is best, but I tend to generally think Julia should usually follow whatever is implemented by the hardware (unless there are strong reasons to do otherwise). |
That table really is amazing. It should perhaps be added to the wikipedia entry on Rounding. I would have found (a) to be more intuitive, and wouldn't have guessed that so many systems choose (b). I also would have naively expected that (a) and not (b) would have a native CPU instruction. FWIW, the |
It would be great to know why Python decided to change. I had a look through the python-dev archives, but couldn't find any discussion as to how it was decided. If anyone knows more, I would certainly be interested to hear it. The classic argument for (b) is that it is unbiased: if you're summing or averaging a bunch of rounded numbers, you won't introduce an unexpected bias. It's typical in stats (I was taught this way in my first-year undergrad stats class), which is probably why R does it that way. |
Apparently, the ISO 60559 standard is identical to IEEE 754. |
@simonbyrne That does sound intuitive. The R manual claims that the IEEE standard requires (b). The Wikipedia page on IEEE seems to be consistent with what you say - provide both modes, but no names or defaults are discussed. |
I tried to create a benchmark here: This compares the native instruction |
And to answer my own question about LLVM support: looking at the source, it seems they don't currently expose ties-to-even instruction in a way that doesn't depend on rounding mode (if they did, the appropriate code would be |
I emailed Jeffrey Yasskin asking about the reason for the Python change, he responded:
|
Can't we just have an optional rounding mode argument to |
I don't find these numerical arguments terribly convincing either. They do make sense in the context of ordinary floating point operations (which is what the C++ link was referring to), but when you call Now we can dispatch on rounding mode, it should be fairly easy to implement them all via optional arguments to The main reasons for (a):
The main reasons for (b):
I would lean toward (b) purely for performance reasons, but am not going to be disheartened if we go the other way. [1] The "prevfloat" trick I used for the fast benchmark for (a) isn't strictly correct according to the IEEE standard, as it will misbehave under different global rounding modes, and incorrectly raise Inexact floating point exceptions, which I suspect is why it isn't used in any library. Also, the |
Isn't (b) also defined as the standard rounding behaviour in IEEE754-2008 (at least according to Wikipedia it is) |
@vchuravy No, in section 5.9 (or for free, see this late draft) it clearly says to offer:
|
@simonbyrne Do I get it right that Float64 and Float32 are equivalent to the binary64 and binary32 formats in IEEE754-2008? If so then section 4.3.3 defines a default rounding
Edit: |
@vchuravy In general, when the standard refers to "rounding" (as in Section 4.3), it means "rounding to fit in the destination format", i.e. after some "computational operation" you have a real number (say 1/3), which then has to be "rounded" to a value that is representable by the format ( This is distinct from the |
FWIW, C in future will have a |
@simonbyrne thanks for the explanation. If we are voting I would vote for |
The entire problem could be avoided by using ternary floating point. (I don't know whether Setsun had floating point or not.) . Seriously, I have a slight preference for round-to-even since that's what the modern hardware does best. |
My vote is for keeping |
If we were to change the behaviour of
|
I like @simonbyrne's proposal. |
I'm on board with this proposal too. |
+1 |
This has come up before (#5983), but probably deserves its own issue. How should
round
handle ties, that is, values with fractional part of 1/2.The two most common options are:
a) round ties away from zero:
round(0.5) = 1.0
,round(1.5) = 2.0
.b) round ties to even (aka banker's rounding):
round(0.5) = 0.0
,round(1.5) = 2.0
.LLVM provides an intrinsic for (a), but this typically requires multiple CPU instructions, and doesn't vectorize (see #8364). On the other hand (b) often corresponds to a native CPU instruction (e.g. table 4-15 of the Intel developer manual), but I don't know if LLVM offers a way to access it. (UPDATE:
rint
can be used, but depends on rounding mode).The IEEE spec requires that we offer both, but doesn't have anything to say about names or interface.
Here is what other languages do:
round
and variantsrint
/nearbyint
(depends on global rounding mode) ,roundeven
in futureround
round
round
round
Round
Math.Round
round
: ties to +Infround
: ties to +InfANINT
ROUND
(though not specified in standard)fround
round
fround
round
round
functionround
: ties to +Infround
round
round
round
Round
functionround
functionround
round
Round
machine dependentround
implementation defined.round
functionround
roundEven
round
is up to implementation (presumably whatever is fastest).round
rint
(regardless of rounding mode)Rounding
Unbiased_Rounding
Machine_Rounding
: whatever is most efficient.round
?rounding as taught in school
optionround
ROUND
ROUNDE
round
ROUNDED
clauseround
(changed in dlang/phobos@91c38b4)FROUND
round
(clarified in bug report)rounded
round
round
ties to +InfRound
round-half-to-even
round
: ties to +InfROUND
(mostly, though varies)My summary:
The text was updated successfully, but these errors were encountered: