basic math functions - strange result (newbie in xslt)

Discussion in 'XML' started by Nicolas Stern, Aug 13, 2003.

  1. Hi

    Can anyone explain me the following behavior with basic math functions
    ?

    I'm doing additions and substractions in an xsl sheet, and don't get
    the expected results.

    my xml file: (dummy.xml)
    -------------------------------
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="t2.xslt"?>
    <ROWSET>
    <ROW>dummy</ROW>
    </ROWSET>
    -------------------------------
    the stylesheet: (t2.xslt)
    -------------------------------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="var1" select="2.4" />
    <xsl:variable name="var2" select="0.2" />
    <xsl:template match="/">
    <html>
    <ul>
    <li><xsl:value-of select="$var1 + $var2" /></li>
    <li><xsl:value-of select="$var1 - $var2" /></li>
    </ul>
    <hr />

    </html>
    </xsl:template>
    </xsl:stylesheet>
    -------------------------------
    the output: (using msxsl dummy.xml t2.xslt
    -------------------------------
    < h t m l >
    < u l >
    < l i > 2 . 6 < / l i >
    < l i > 2 . 1 9 9 9 9 9 9 9 9 9 9 9 9 9 9 7 < / l i >
    < / u l >
    < h r >
    < / h t m l >
    -------------------------------

    I would need the result of $var1 - $var2 to be = 2.2, as expected

    The behavior is identical when using the parser in xmlspy.

    Any hint ?

    thanks in advance

    Nicolas
    Nicolas Stern, Aug 13, 2003
    #1
    1. Advertising

  2. > <xsl:variable name="var1" select="2.4" />
    > <xsl:variable name="var2" select="0.2" />
    > <li><xsl:value-of select="$var1 + $var2" /></li>
    > <li><xsl:value-of select="$var1 - $var2" /></li>
    > the output: (using msxsl dummy.xml t2.xslt
    > -------------------------------
    > < l i > 2 . 6 < / l i >
    > < l i > 2 . 1 9 9 9 9 9 9 9 9 9 9 9 9 9 9 7 < / l i >


    This kind of behaviour is very common. Answer is nearly correct, but not
    exactly, because XSLT uses double precision (64-bit) floating point
    numbers to do the calculation. For more details on floating point
    rounding errors, see: http://www.python.org/doc/tut/node14.html

    If you need exact results, you need to use fixed point arithmetic. I
    don't know if this is possible (in any easy way) in XSLT.

    But... According to my experience it is very strange that this error
    happens with such small numbers. Double precision is after all a darn
    lot of precision, it really should not produce this kind of error. Is
    there something flawed with the implementation of those transformers?
    Aleksi Kallio, Aug 13, 2003
    #2
    1. Advertising

  3. "Aleksi Kallio" <aleksi.kallio@_DONT_SPAM_csc.fi> wrote in message
    news:bhd0c0$oem$...
    > > <xsl:variable name="var1" select="2.4" />
    > > <xsl:variable name="var2" select="0.2" />
    > > <li><xsl:value-of select="$var1 + $var2" /></li>
    > > <li><xsl:value-of select="$var1 - $var2" /></li>
    > > the output: (using msxsl dummy.xml t2.xslt
    > > -------------------------------
    > > < l i > 2 . 6 < / l i >
    > > < l i > 2 . 1 9 9 9 9 9 9 9 9 9 9 9 9 9 9 7 < / l i >

    >
    > This kind of behaviour is very common. Answer is nearly correct, but not
    > exactly, because XSLT uses double precision (64-bit) floating point
    > numbers to do the calculation. For more details on floating point
    > rounding errors, see: http://www.python.org/doc/tut/node14.html
    >
    > If you need exact results, you need to use fixed point arithmetic. I
    > don't know if this is possible (in any easy way) in XSLT.
    >
    > But... According to my experience it is very strange that this error
    > happens with such small numbers. Double precision is after all a darn
    > lot of precision, it really should not produce this kind of error. Is
    > there something flawed with the implementation of those transformers?


    Using any programming language on any computer you get:

    0.33333333....

    for 1/3


    Is there anything flawed with all computers?

    No, this is a "flaw" with their (binary) numeric system or our (decimal)
    numeric system.


    The answer is simple math -- there are numbers that cannot be represented
    with finite number of digits in any (or most) given number system.

    Therefore, the number representation is an approximation of the exact value,
    so are results of calculations.

    In order to get a better looking result, one can use the format-number()
    function and thus round the number to the required precision, e.g.:

    format-number(2.4 - 0.2, "#0.00")



    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
    Dimitre Novatchev, Aug 13, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?VmVkcmFuIEZ1cmHEjQ==?=

    Strange result with math.atan2()

    =?UTF-8?B?VmVkcmFuIEZ1cmHEjQ==?=, May 2, 2006, in forum: Python
    Replies:
    5
    Views:
    462
    =?UTF-8?B?VmVkcmFuIEZ1cmHEjQ==?=
    May 2, 2006
  2. Mark Healey
    Replies:
    7
    Views:
    1,479
    Tim Prince
    May 22, 2006
  3. C.W.Holeman II
    Replies:
    7
    Views:
    684
    C.W.Holeman II
    May 30, 2007
  4. Michael Tan
    Replies:
    32
    Views:
    965
    Ara.T.Howard
    Jul 21, 2005
  5. VK
    Replies:
    15
    Views:
    1,159
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page