sschwarzer
2021-6-15 08:15:10

I’ve got the impression (without having hard numbers), that new/revised languages are shifting from “round up on ties” to “round to even.” At the moment, I have only one concrete data point. :wink: … Python 2 used to round up on ties whereas Python 3 rounds to even.

In that light, I wouldn’t say that “round up on ties” is a “standard” round function.

I think it’s fine to have “round to even” as default behavior for round, but I’m not opposed to an extra function that rounds up on ties. :slightly_smiling_face:


sschwarzer
2021-6-15 08:15:44

Why do you want the “round up on ties” behavior rather than “round to even”?


soegaard2
2021-6-15 08:22:36

For numerical computations the “round ties to even” seems better. I am working on a version of Processing for Racket, where the main focus is producing images and animations. They (or should I say Java) uses the “round ties up” rule. Now I haven’t looked for programs that rely on the rule, but I can imagine that some images will have odd patterns if ties are broken to the same side every time.


soegaard2
2021-6-15 08:23:38

I looked at C “round” and Java “round”. I didn’t know Python switched.


soegaard2
2021-6-15 08:33:29

Turns out Python 2 uses “round a way from 0” (which for positive numbers is the same as round up).


sschwarzer
2021-6-15 08:35:27

You’re right. Interesting detail. That’s actually what I had in mind when I wrote that Python 2 rounded up, but that’s indeed not precise. :-D


soegaard2
2021-6-15 08:40:44

I checked the docs of P5.js (the JavaScript version of Processing) - and they don’t mention what happens on ties (but mentions Math.round is used). However Math.round happens to use the “round towards +infinity” rule.

I am beginning to think, it is better to stick to the standard Racket function - and maybe include a separate round-up.


sschwarzer
2021-6-15 08:56:12

Could/would you use a parameter to control rounding behavior while otherwise keeping the code the same (so you can easily switch the rounding behavior)? Or is this unnecessary complexity?


sschwarzer
2021-6-15 08:57:44

I guess it may be practical to just live with small rounding differences between the implementations, especially if there isn’t an “obvious”/"ideal" approach.


soegaard2
2021-6-15 09:08:14

> Description > > *round* returns a value equal to the nearest integer to _x_. The fraction 0.5 will round in a direction chosen by the implementation, presumably the direction that is fastest. This includes the possibility that *round*(_x_) returns the same value as roundEven(_x_) for all values of _x_.


soegaard2
2021-6-15 09:08:36

Just found this from the OpenGL specs. Seems there is nothing to worry about :slightly_smiling_face:


badkins
2021-6-15 14:21:53

A possible advantage of returning the old value is that the new value can be obtained from a, but the old value will be lost.


laurent.orseau
2021-6-15 15:04:31

which can be useful for backup/restore stuff for set! in general (but not really so useful for ++)


notjack
2021-6-16 00:34:58

One time I got bored and wrote a little micro library for dealing with different rounding modes