Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > basic math functions - strange result (newbie in xslt)

Reply
Thread Tools

basic math functions - strange result (newbie in xslt)

 
 
Nicolas Stern
Guest
Posts: n/a
 
      08-13-2003
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"
xmlnssl="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
 
Reply With Quote
 
 
 
 
Aleksi Kallio
Guest
Posts: n/a
 
      08-13-2003
> <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?

 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      08-13-2003

"Aleksi Kallio" <aleksi.kallio@_DONT_SPAM_csc.fi> wrote in message
news:bhd0c0$oem$(E-Mail Removed)...
> > <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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Strange result with math.atan2() =?UTF-8?B?VmVkcmFuIEZ1cmHEjQ==?= Python 5 05-02-2006 03:14 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM



Advertisments