XPath calculation problem 100.02 + 0.02 is not 100.04

G

Guest

Hi,

in an application that evaluates XPath against XML I had a sum and a
comparison to a node...
boolean(sum(/Path2Nodes/toAddUp)=/Path2Node/toCompare2)
evaluates to "false" sometimes even though the sum seems correct.
I found out that there are some problems with evaluating XPath itself...

The problem occurs in .NET 1.1 and 2.0 (maybe also .NET 1.0) accross several
operating systems (XP, Windows 2003 Server) and several processors (AMD64,
different PIV-models, XEON).

Here the example code with example output.


Where can I get a FIX for it... please do not refer me to changing my
XPath... I mean, those are really simple math-operations... there is no
excuse for them not to work like I presented.

Note that the "string()" in this example is meant to simulate what the XPath
processor will do anyways when comparing values that it does not specifically
know they are numbers... maybe it is always used, even though there are only
numbers involved (by encapsulating with the "number()" function).




using System;
using System.Text;
using System.Xml;

namespace XPthEval
{
class Program
{
static void Main(string[] args)
{
String sXPath;
sXPath = "100.02+0.02";
if (args.Length > 0)
{
sXPath = args[0];
}

Console.WriteLine("Evaluate "+sXPath+":");
Console.WriteLine(new
XmlDocument().CreateNavigator().Evaluate(sXPath));
Console.WriteLine();

sXPath = "string(" + sXPath+")";
Console.WriteLine("Evaluate " + sXPath + ":");
Console.WriteLine(new
XmlDocument().CreateNavigator().Evaluate(sXPath));
Console.WriteLine();

}
}
}



I:\>xpathevl11 "100.02+0.02"
Evaluate 100.02+0.02:
100,04

Evaluate string(100.02+0.02):
100.03999999999999




FIX needed badly!


Regards,
 
L

Luke Zhang [MSFT]

Hello,

Whe you use string() or comparing with a string, it will convert the float
value to string which means a precision lost here. You need a Round()
function here is ignore the precision lost.

Luke
 
G

Guest

Hi Luke,

XML allows the type "decimal". I would expect a parser to consider the
subtype of float in a way that it knows that operations can be done witout
losing precision.

Regards,
 

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

Top