开发者

Strange results from the xquery ceiling function

I am working on a VB.Net app running on .Net framework 2.0.50727.3603

When I fire off this xquery statement I get 56, but would expect 55:

ceiling(.55*100)

I am only seeing weird results on .55 and .56 as you can see my results below:

ceiling(.50*100) = 50
ceiling(.51*100) = 51
ceiling(.52*100) = 52
ceiling(.53*100) = 53
ceiling(.54*100) = 54
ceiling(.55*100) = 56 (would expect 55)
ceiling(.56*100) = 57 (would expect 56)
ceiling(.57开发者_如何学C*100) = 57
ceiling(.58*100) = 58
ceiling(.59*100) = 59
ceiling(.64*100) = 64
ceiling(.65*100) = 65
ceiling(.66*100) = 66

Any ideas on what might be causing this? Is this a bug in xquery?

Here is the basic gist of my code:

Imports System.Xml
Dim objXPathNavigator As XPathNavigator
evaluateExpression = objXPathNavigator.Evaluate(objXPathExpression)


XSLT/XPath 1.0 has a single number type, a double precision floating point type that is the same as the VB.NET Double. If you run

Dim doubleArray As Double() = {0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59}
For Each d As Double In doubleArray
    Console.WriteLine("d: {0}; d * 100: {1}; Math.Ceil(d * 100): {2}", d, d * 100, Math.Ceiling(d * 100))
Next

with .NET you will find similar oddities to the one you find with your XPath evaluation.

So the problem is the limited precision of that number type in XPath 1.0 (or other languages), the decimal representation (e.g. 0.55) is finite but the binary representation might not be. If you need better precision you need to consider to move to XPath 2.0, currently only supported by third party products like Saxon 9 or XQSharp, there you can then use the xs:decimal type instead of the number type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜