StefH / XPath2.Net

Lightweight XPath2 for .NET
Microsoft Public License
36 stars 14 forks source link

Arithmetic error #21

Closed nja closed 5 years ago

nja commented 5 years ago
/total/@taxableAmount + /total/@taxAmount = /total/@netAmountIncTax

<total taxableAmount="1.23" taxAmount="7.89" netAmountIncTax="9.12" />
=> True

<total taxableAmount="1441.64" taxAmount="273.91" netAmountIncTax="1715.55" />
=> False
martin-honnen commented 5 years ago

That is working as defined, if you convert from your attribute value implicitly to a number through the addition operator the type xs:double is used and it has such inaccuracies, like other programming languages as Javascript with its Number type or C# with its double type also have.

With XPath 2.0 for exact computations use xs:decimals explicitly: xs:decimal(/total/@taxableAmount) + xs:decimal(/total/@taxAmount) = xs:decimal(/total/@netAmountIncTax).

StefH commented 5 years ago

It because of double conversion. The result from second test is 1715.5500000000002.

nja commented 5 years ago

You're right of course. I tried the plain equality in C# before submitting, but the details about how literals are evaluated bit me...