Re: Rounding error while saving a recordset as XML



Hi All,

I am facing one rounding error while saving a recordset as XML. Is there
a way to get rid of this error without altering the datatype of QTY field
to string/variant.

No. Because you not understanding how floating-point works.

You know that the decimal fraction 2/3 is 0.66666666666666... in base 10,
forever. It is said to recur.
And no matter where the fraction is chopped, it will never represent 2/3
The only fractions that are exact in base 10 are multiples of powers of 1/5
and 1/2 combined because 5 and 2 are divisors of 10.
So 3/40, 7/25, 9/16 are exact but 1/7, 1/13 are not.

Well floating-point uses base 2 internally,
And in base 2, only fractions that are powers of 1/2 are exact, all others
recur.
In particular 0.1 and 0.01 will never have an exact representation, they are
not multiples of powers of 1/2
So that means there is no exact 3.01 representation, when you convert from
text to double, it will choose the nearest binary fp number and when you
convert back, this what you see.

Programmers usually go through shock then rage then depression then
realisation on how to work with fp.
You should leave the XML text as it is. When it is read back and converted
to a double, you can output to 2 decimal places, rounded and at that point,
you will see 3.01
When manipulating fp numbers, adding, subtracting, mutiplying - all effort
should bent on minimising scaling error.

Stephen Howe


.



Relevant Pages

  • Re: how to specify power of number
    ... TCP/IP addresses into 4 byte integers): ... with using powto find exact powers of 2. ... The OP should keep in mind that inet_atondoes NOT support IPv6 and ...
    (comp.lang.c)
  • Re: lisp and security
    ... gNOSPAMat@jpl.nasa.gov (Erann Gat) wrote: ... > XML, which ... > the exact ... >> Go back to the original post. ...
    (comp.lang.lisp)
  • Re: Really basic problem
    ... Zentrader wrote: ... exact enough ... This is a misleading statement. ... precise in the sense that smaller fractions are representable, ...
    (comp.lang.python)
  • Re: XML - Find and replace text
    ... know the exact "location" as in the text is always going to be at: ... Cleanest is probably using XSLT with the tdom package. ... so assuming the XML structure is: ... set doc ...
    (comp.lang.tcl)
  • Re: Abolish Fractions?
    ... terms of decimal fractions for the purposes of the layperson. ... For practical purposes, integers suffice for almost all computations ... that must be exact, and there are decimals for the ... were cases where using fractions would be misleadingly exact anyway. ...
    (sci.math)