473,799 Members | 3,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Double.P arse not accurate?

I've got a section of code in my app that keeps track of how much time an
employee has accrued, which equals out to 1.33 days of PTO per month. I'm
trying to store the time accrued so far in an XML file. I have no problems
writing to and reading from the XML file and all my values are correct. The
problem I'm having is my calculation never amounts to 1.33, it goes from
1.3299999999999 7
to
1.3743333333333 .
on the next run.

My calculation is
int TimePerDay = 1.33 /
DateTime.DaysIn Month(DateTime. Parse(DateTime. Now.ToShortDate String()).Year,
DateTime.Parse( DateTime.Now.To ShortDateString ()).Month);

I save the difference of that calculation in the XML file, and then retrieve
it again when the calculation is necessary. Here is the code:
int TimeCount =
System.Double.P arse(xmlDoc.Chi ldNodes.Item(1) .ChildNodes.Ite m(1).InnerText) ;
//the first time I retrieve this value, it will be initially set to
0.0443333333333 333
//the first time this code runs
TimeCount += 1.33 /
DateTime.DaysIn Month(DateTime. Parse(DateTime. Now.ToShortDate String()).Year,
DateTime.Parse( DateTime.Now.To ShortDateString ()).Month);
xmlDoc.Save(Pat h);

Like I said the problem is that calculation never amounts to 1.33 - I have
ran it many times in a for loop without ever converting it to a string value.
The for loop eventually calculates to 1.33, and it stops at 1.33 and
displays that fact in a MessageBox. I think I may be having a problem with
the System.Double.P arse parser . . . anybody got any idea's? :)
Nov 16 '05 #1
3 2654
slylos wrote:
I've got a section of code in my app that keeps track of how much
time an employee has accrued, which equals out to 1.33 days of PTO
per month. I'm trying to store the time accrued so far in an XML
file. I have no problems writing to and reading from the XML file
and all my values are correct. The problem I'm having is my
calculation never amounts to 1.33, it goes from
1.3299999999999 7
to
1.3743333333333 .
on the next run.


See
http://www.yoda.arachsys.com/csharp/floatingpoint.html

for the reasons floating point calculations come out "wrong"

Hans Kesting
Nov 16 '05 #2
The article you provided was very useful. Thank you very much for that
information. Now let me get back to my code and see what I come up with!

"Hans Kesting" wrote:
slylos wrote:
I've got a section of code in my app that keeps track of how much
time an employee has accrued, which equals out to 1.33 days of PTO
per month. I'm trying to store the time accrued so far in an XML
file. I have no problems writing to and reading from the XML file
and all my values are correct. The problem I'm having is my
calculation never amounts to 1.33, it goes from
1.3299999999999 7
to
1.3743333333333 .
on the next run.


See
http://www.yoda.arachsys.com/csharp/floatingpoint.html

for the reasons floating point calculations come out "wrong"

Hans Kesting

Nov 16 '05 #3
I found that if I take the
NewTime variable, which I read the latest floating point value into from the
XML file, if I modulo it by 1.33, I eventually get 1.33 exact when NewTime
actually sums to 1.3300000000001 2. I can then check for the modulo value to
get the desired result. Thanks for your help all!!

"Hans Kesting" wrote:

See
http://www.yoda.arachsys.com/csharp/floatingpoint.html

for the reasons floating point calculations come out "wrong"

Hans Kesting

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6092
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this purpose, that isn't enough. I was reading a book about some of the financial scandals of the 1990s called "Inventing Money: The Story of Long-Term Capital Management and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions that...
14
3693
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style of date formatting (RFC 822). I have been using a variation of RFC 1123 (just change the time zone to an offset, i.e. "-0800"). It seems to be writing okay, but it's failing to parse. I've tried changing the regional & language settings in my...
2
9469
by: Rod Brick | last post by:
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should be simple enough. The output I'm looking for is "0.00001", not "1E-05". I'm obviously baffled by the NumberFormatInfo class, I can't seem to make it do what I want. And, I've tried the following, all of which have failed to modify the original exponential form. The commented out "D7" statement throws a...
3
5050
by: Dan Vogel | last post by:
I'd like to find an elegant solution to the problem of calling a certain function based on the types of two parameters. In my case, the functions compute the distance between different types of geometric objects, and I want to call the function that gives me the most accurate distance. I have a base class called Shape with basic functionality: Shape
3
4230
by: davesNotHere | last post by:
Greetings, experts! I'm having a bit of a problem importing some working code from last night into Visual Studio. The code compiles correctly, but when I run the program, it doesn't seem to open the text window to ask for input. It simply errors on the ReadLine statement. (See the attached code.) When I use "Start Without Debugging", I received an exception window that says "An exception 'System.ArgumentNullException' has occurred in...
12
3198
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
14
9647
by: cj | last post by:
VB2003. I need a large positive integer. Which is larger int64 or double? I see int64 also apparently is known as long and will hold -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. But I'm not good with the E+??? notation so when I'm told double holds -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.
1
4769
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF THE PROGRAM PLEASE HELP ME Server Error in '/WebApplication1' Application. -------------------------------------------------------------------------------- Input string was not in a correct format. Description: An unhandled exception occurred...
5
4590
by: schlied | last post by:
Hello, i have the following problem in our project: If I try to parse a string value with the following line double x = double.Parse("39.95238", CultureInfo.InvariantCulture); x contains 39.9523799999.... instead of the correct one. What could be the reason for that? Thanks in advance
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.