Page MenuHomePhabricator

nondeterministic #expr rounding error
Open, LowPublic

Description

{{#expr: 1000.33 - 1000}} sometimes evaluates to 0.33, other times to 0.33000000000004 (the second one happens far more often). I'm not sure which one is correct (it has the right magnitude for a floating point precision error, 0.00000000000004 being about the 10^17-th part of 1000, and 2^53 ~= 10^16 (a double uses 53 bits for the mantissa), though as far as my understanding of floating point arithmetic goes, that should be rounded off), but the nondeterministic behavior is definitely weird.


Version: unspecified
Severity: minor
URL: http://hu.wikipedia.org/w/index.php?title=Wikip%C3%A9dia:Kocsmafal_(m%C5%B1szaki)&oldid=5211133#Sz.C3.A1m.C3.ADt.C3.A1sok

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 10:35 PM
bzimport added a project: ParserFunctions.
bzimport set Reference to bz18233.
bzimport added a subscriber: Unknown Object (MLST).

In a php shell (PHP 5.2.6, Win XP) floatval("1000.33") - floatval("1000") evaluates exactly to 0.33.

(In reply to Tisza Gergő from comment #0)

{{#expr: 1000.33 - 1000}} sometimes evaluates to 0.33, other times to
0.33000000000004 (the second one happens far more often).

Does "sometimes" refer to the same wikipage / site? Or are results consistent on the same site? Can this still be seen?
Also getting 0.33000000000004 on mediawiki.org.

(In reply to Andre Klapper from comment #3)

Does "sometimes" refer to the same wikipage / site? Or are results
consistent on the same site? Can this still be seen?

According to the reporter, the same wikipage produced different results when reevaluated. I can't reproduce that now, though.

I am able to reproduce this for all the values such as 100.33 - 100, 10.33 - 10, 1.33 - 1 etc it gave the result as 0.33 but for the value 1000.33 - 1000 it is giving the result as 0.33000000000004 to be more accurate for the values greater than 1000 it is giving the output with almost with many digits but for values less than 1000 it is giving precision or accuracy of two digits after decimal point ( tested in my localhost)

@Tgr Is it better to add 17 digits to give exact precision or to round off them to the digits based on the given number decimal part?

Change 406573 had a related patch set uploaded (by Rammanojpotla; owner: Rammanoj):
[mediawiki/extensions/ParserFunctions@master] Fix #expr rounding nondeterministic behaviour

https://gerrit.wikimedia.org/r/406573

Note that the bug report was about the same calculation having nondeterministic results. 1000.33 - 1000 giving a float with lots of zeroes is expected behavior (that's just how floating-point arithmetic works) and I'm not sure it is worth trying to change it. IMO this task should be closed as invalid.
(Sorry for failing to point that out before you started working on it.)

Change 406573 abandoned by Rammanojpotla:
Fix #expr rounding nondeterministic behaviour

https://gerrit.wikimedia.org/r/406573