Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2006

Tip: Looking for answers? Try searching our database.

XPath calculation problem 100.02 + 0.02 is not 100.04

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MSDNAndi - 20 Jan 2006 02:43 GMT
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,
Luke Zhang [MSFT] - 20 Jan 2006 07:27 GMT
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
MSDNAndi - 20 Jan 2006 08:22 GMT
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,

> Hello,
>
[quoted text clipped - 3 lines]
>
> Luke

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.