Exact invoice totals and floating measurements

Does 0.1 plus 0.2 make exactly 0.3?

exact-decimals-srl.srl · output · proof · check · playground


The question

Does 0.1 plus 0.2 make exactly 0.3?


Historical background

XML Schema distinguished exact decimal values from the finite binary floating-point values of double. SPARQL adopted numeric operations over these datatypes. Keeping the distinction matters when an invoice total should add up in decimal and a measurement arrives as a double. W3C, XML Schema Part 2 · SPARQL 1.1 operators


What the program says

# An invoice adds decimal prices without binary rounding, even above the
# exact integer range of a double. It also shows the binary64 double sum
# 0.1e0 + 0.2e0, infinities, NaN, exact division and rounded decimal division.
PREFIX : <https://example.org/numbers/>

RULE {
  :invoice :total ?total ; :largeTotal ?large ; :eighth ?eighth ; :third ?third .
  :measurement :doubleSum ?double ; :overflow ?overflow ; :invalid ?nan ; :negativeInfinity ?negative .
} WHERE {
  SET(?total := 0.1 + 0.2)
  SET(?large := 9007199254740993.1 + 0.2)
  SET(?eighth := 1 / 8)
  SET(?third := 1 / 3)
  SET(?double := 0.1e0 + 0.2e0)
  SET(?overflow := 1e300 * 1e300)
  SET(?nan := 0e0 / 0e0)
  SET(?negative := 1e0 / -0e0)
}

These functions and numeric promotions follow SPARQL 1.2 RL, with the expression functions defined by SPARQL.


What eyemind concludes

PREFIX : <https://example.org/numbers/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

:invoice :total 0.3 .
:invoice :largeTotal 9007199254740993.3 .
:invoice :eighth 0.125 .
:invoice :third 0.3333333333333333333333333333333333 .
:measurement :doubleSum "0.30000000000000004"^^xsd:double .
:measurement :overflow "INF"^^xsd:double .
:measurement :invalid "NaN"^^xsd:double .
:measurement :negativeInfinity "-INF"^^xsd:double .

The decimal total is exactly 0.3, and the larger total retains digits beyond the exact integer range of a double. The eighth is exact. The third is rounded to 34 significant digits. Double addition produces 0.30000000000000004; double overflow and division by zero produce the displayed IEEE-754 infinities and NaN.

The check report records these claims:

// Checked answers: these follow from the input program and are every answer of its questions.
inf(iri('https://example.org/numbers/invoice'), iri('https://example.org/numbers/total'), literal('0.3', datatype('http://www.w3.org/2001/XMLSchema#decimal')))
inf(iri('https://example.org/numbers/invoice'), iri('https://example.org/numbers/largeTotal'), literal('9007199254740993.3', datatype('http://www.w3.org/2001/XMLSchema#decimal')))
inf(iri('https://example.org/numbers/invoice'), iri('https://example.org/numbers/eighth'), literal('0.125', datatype('http://www.w3.org/2001/XMLSchema#decimal')))
inf(iri('https://example.org/numbers/invoice'), iri('https://example.org/numbers/third'), literal('0.3333333333333333333333333333333333', datatype('http://www.w3.org/2001/XMLSchema#decimal')))
inf(iri('https://example.org/numbers/measurement'), iri('https://example.org/numbers/doubleSum'), literal('0.30000000000000004', datatype('http://www.w3.org/2001/XMLSchema#double')))
inf(iri('https://example.org/numbers/measurement'), iri('https://example.org/numbers/overflow'), literal('INF', datatype('http://www.w3.org/2001/XMLSchema#double')))
inf(iri('https://example.org/numbers/measurement'), iri('https://example.org/numbers/invalid'), literal('NaN', datatype('http://www.w3.org/2001/XMLSchema#double')))
inf(iri('https://example.org/numbers/measurement'), iri('https://example.org/numbers/negativeInfinity'), literal('-INF', datatype('http://www.w3.org/2001/XMLSchema#double')))

What the proof establishes

The decimal calculations are ordinary rules over exact integer coefficients and scales. The proof cites the program compiled from the rule set: one step for each of the eight inferred triples, and 168 call tables holding every answer of the decimal and numeric helper calls the questions make, such as reading 0.1 as decimal(1, 1) or promoting an integer to a decimal.

The saved report ends with verdict('checked'). The checker follows the certificate without calling the reasoning engine: it checks each call table by applying the compiled rules one level, computing their integer arithmetic and the float64 primitive used for double results, including the exceptional values, itself. It requires the listing to be closed under the rules and replays each listed triple’s firing. The verdict establishes that the claims follow from the program and are every answer of its questions.


Try it

node bin/eyemind.js examples/exact-decimals-srl.srl
node bin/eyemind.js --proof examples/exact-decimals-srl.srl
node bin/eyemind.js --check-proof examples/proof/exact-decimals-srl.js examples/exact-decimals-srl.srl