Xpath Mod operator confusion

A

annoyedtuna

When I perform the following:

<xsl:value-of select="( 182.88 mod 30.48 )"/>

I get 30.479999999999993 as a result

By my calculations, the result should actually be 0


Does anyone know what's going on here?
 
D

Dimitre Novatchev

When I perform the following:

<xsl:value-of select="( 182.88 mod 30.48 )"/>

I get 30.479999999999993 as a result

By my calculations, the result should actually be 0


Does anyone know what's going on here?


Yes. The "number" type in XPath is actually "double". (Decimal) Numbers
are expressed as double precision and thus there are cases when this results
in small truncating errors (the same as e.g. 1/3 = 0.33333... gets truncated
when represented as a decimal number).

Not commenting on the really strange idea to use the "mod" operator for
non-integer operands, you really got what you asked for ...

The situation's changed in XPath 2.0, where there's the xs:decimal datatype.
As the the type of the above numbers is xs:decimal,

182.88 mod 30.48

is evaluated to 0.

Workaround for XPath 1.0:

Use:

(100*182.88) mod (100*30.48)


Cheers,

Dimitre Novatchev
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,841
Latest member
WilmerBelg

Latest Threads

Top