Skip to content

Rounding error in a sum #29

@fagertjan

Description

@fagertjan

Hi,

I am busy with a peppol/ubl implementation and am using your command line tool to verify my generated xml against the schematron. It works fine in most cases but for one file I get a failure caused by a rounding error. The rule that fails checks if some sum is the same as a value stated elsewhere in the file. See: https://docs.peppol.eu/poacc/billing/3.0/rules/ubl-tc434/BR-AE-08/

The schematron states that the values are xs:decimal, but it seems that native javascript is being used. If I do 24.63 + 62.16 in the console of my browser I also get a rounding error: 86.78999999999999.

I created a sample that does the same calculation.

Sample xml:

<?xml version="1.0"?>
<Invoice>
  <Total>
    86.79
  </Total>
  <Lines>
    <Line>
      <SubTotal>
        24.63
      </SubTotal>
    </Line>
    <Line>
      <SubTotal>
        62.16
      </SubTotal>
    </Line>
  </Lines>
</Invoice>

Sample schematron:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
  <ns prefix="xs"  uri="http://www.w3.org/2001/XMLSchema"/>

  <pattern id="ubl-model">
    <rule context="/*">
      <assert test="(xs:decimal(Total) = sum(Lines/Line/xs:decimal(SubTotal)))">
        <value-of select="xs:decimal(Total)" /> is not the same as <value-of select="sum(Lines/Line/xs:decimal(SubTotal))" />
      </assert>
    </rule>
  </pattern>
</schema>

The failed assertion of my sample says:
asrt ✘ 86.79 is not the same as 86.78999999999999

Regards,
Gert Jan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions