# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2011-09-17 02:44:39

xot
Registered: 2007-08-18
Posts: 1,201

### round - It's just gross. Ewww!

Mike Dailly over at YoYo Games pointed out to me a problem with the round() function. It's a Delphi problem which GML inherited and it is turning out to be a pain in the behind to port the broken behavior. Sometimes I think their stance on backwards compatibility is a bit backwards itself. Mike did suggest that there might someday be an option to select the behavior.

Anyway, the problem is it doesn't always round up when the fractional portion of a real equals 0.5.

Put simply: round(3.5) == round(4.5)

Perhaps floor(n+0.5) is a better option.

This "feature" exists in GM5, GM6, GM7, and GM8, and will presumably persist for the foreseeable future.

Abusing forum power since 1986.

Offline

## #2 2011-09-17 04:06:35

paul23
Member
Registered: 2007-10-17
Posts: 110

### Re: round - It's just gross. Ewww!

I posted this long ago already, this is supposed to be usefull for statistical calculations (ie taking average of large data).

With normal round, combined with the fact that you use normally natural numbers (and .5 is an often used number), you'll see a slight bias towards rounding "up". With this rounding that bias was removed already.

I myself always use floor(n+0.5) nowadays indeed.

Offline

## #3 2011-09-17 04:15:23

xot
Registered: 2007-08-18
Posts: 1,201

### Re: round - It's just gross. Ewww!

Ah, you've jogged my memory. I remember that being called banker's rounding, and like you say, it was intended to reduce bias. It is also known by the simple and descriptive "round half to even" rule, which is exactly what GameMaker does.

http://en.wikipedia.org/wiki/Rounding#R … lf_to_even

It appears that this is not necessarily a Delphi oddity. The linked Wikipedia article says it is the default behavior of the IEEE 754 floating-point routines. Python doesn't include a round function, but fsum() depends on IEEE 754 default rounding.

C and C++ wisely avoid the whole mess and do not include a rounding function -- C# does however. Interestingly its behavior is selectable with a second argument. PHP has a selectable mode as well.

Java and ECMAScript/Javascript/AS3 have methods for rounding that work like floor(n+0.5), with a distinction in the case of the latter concerning negative values of zero.

ECMA-262 Specification wrote:

The value of Math.round(x) is the same as the value of Math.floor(x+0.5), except when x is -0 or is
less than 0 but greater than or equal to  -0.5; for these cases  Math.round(x) returns -0, but Math.floor(x+0.5)
returns +0.

Ruby tries to distinguish itself with a "round half away from zero" rule.

Isn't it wonderful to have so many choices!

Abusing forum power since 1986.

Offline

## #4 2011-09-26 09:34:08

paul23
Member
Registered: 2007-10-17
Posts: 110

### Re: round - It's just gross. Ewww!

Wonder if my bug report is being missed or something . - It's not assigned to someone. (The report about literal "\" getting converted to "\\").

It's funny how you can see who works on what by looking at the bug reports:
Mike seems to be mainly in charge, dividing the work. Also works on the runner.
Russel seems to be mainly working on the backend/runner
Scottdunbar seems to be working on the IDE
Dadio seems to be responsible for the manual
Nal seems to be working on the in-gamemaker-help

Offline