473,811 Members | 3,924 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 2655
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
6095
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
3695
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
5051
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
3201
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
9651
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
4772
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
4591
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
9604
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
10644
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
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
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
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.