Am 17.06.2020 um 10:37 schrieb Jay Straw:
I'm building a budgeting system for home use, using XSLTForms and --
ready for it? -- BaseX!
sum() has been behaving weird. I'm adding USD, which means only 2
decimals of precision. But depending on the combination of elements I
add, I'll get a long double that's inaccurate. Sometimes it returns what
I expect, sometimes it doesn't, but either case is repeatable.
Take these 3 receipts:
Start Capacitor (for old rockwell saw/jointer motor)
Music of the Spheres
Magic Mirror of M. C. Escher
Daumier 120 Great Lithographs
When I sum the elements:
sum 1 & 2 = 16.367
sum 1 & 3 = 27.35
sum 2 & 3 = 30.72
What makes certain combinations return such crazy numbers?
I did find one other thread in the mailing list about this, it got an
answer but didn't really explain much. I have a schema for prices in the
XForm, should I be loading that somehow? The only workaround I have now
is like, "let $grosses:= for $gross in $receipts//gross return
xs:decimal($gross)" but I'm hoping there's a more straightforward way.
Not sure if this is implementation specific or if it's not defined or
what. If I should ask elsewhere, please let me know :-)
The "default" xs:double data type used in XPath and XQuery is known to
see similar results), yes, you need to use `xs:decimal` explicitly to
avoid such problems.
Whether you do that with a long `for .. return` or simply with
is up to you.
Schema-aware XQuery could also help of the `gross` elements were
declared as `xs:decimal`, but I don't think XQuery supports schema-aware