Page MenuHomePhabricator

Avoid floating point errors when manipulating decimal values
Closed, ResolvedPublic

Description

Floating point errors should be avoided when handling/manipulating QueryValue and DecimalValue objects. One situation where such errors frequently show is when adding an uncertainty margin to a base amount to comput the upper and lower bounds of a quantity.

The errors are introduced because DecimalMath, a class that implements basic arithmetical operation on DecimalValue objects, uses float numbers to perform the calculations.

To avoid this, we could use the bcmath library, if it is installed.

Also, smart rounding rules could be employed to reduce the ill effects of floating point errors.


Version: unspecified
Severity: normal
Whiteboard: quantityvalue

Details

Reference
bz56682

Event Timeline

bzimport raised the priority of this task from to Needs Triage.
bzimport set Reference to bz56682.
bzimport added a subscriber: Unknown Object (MLST).
daniel created this task.Nov 6 2013, 7:05 PM
daniel added a comment.Nov 6 2013, 7:07 PM

Example:

"raw": "3+/-0.01\u00b0",
 "value": {
     "amount": "+3",
     "unit": "\u00b0",
     "upperBound": "+3.0099999999999997868371792719699442386627197265625",
     "lowerBound": "+2.9900000000000002131628207280300557613372802734375"
 },
 "type": "quantity"

Change 94355 had a related patch set uploaded by Daniel Kinzler:
(bug 56682) DecimalMath now uses bcmath if available

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

Change 94367 had a related patch set uploaded by Daniel Kinzler:
(bug #56682) Use DecimalMath for getUncertaintyMargin

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

Change 94355 merged by jenkins-bot:
(bug 56682) DecimalMath now uses bcmath if available

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

Change 94367 merged by jenkins-bot:
(bug #56682) Use DecimalMath for getUncertaintyMargin.

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

Verified in Wikidata demo time.