-
Problems with ... Convert ?
Hi guys
I am getting a very strange error.
I have a C# application that has this code :
double a = 0;
double b = 0;
double c = 0;
In the code, the values for "a", "b" and "c" changes and gets these values:
a = 1716
b = Convert.ToDouble(and gets a value from the database, in this case the value in the field is 0) so b will look like this :
b = Convert.ToDouble(0)
and c is like b but the value in the database field is 1716 so:
b = Convert.ToDouble(1716)
Now we have this situation :
a = 1716
b = 0
c = 1716
Later in the code there's a formula like this :
a -= (b + c)
Theorically I would get a value of 0 (zero) for "a" because :
a = 1716 - (0 + 1716)
so
a = 0
The problem is that I'm not getting 0 but a very strange value like this :
-2,273456E-13
that is equal to : 0,00000000556453 (or somethink like that)
And in the code if value of "a" is different from 0 it does not allow you to continue with other options, but I need to continue with those options because for me, "a" is equal to 0.
The questions are :
how is it possible that 1716 - (0 + 1716) is not equal to 0 but equal to -2,273456E-13 ?!?!?!?!?!?!?
What's going on ?, what's wrong ?, how can I solve this problem ? is it a problem related to the Convert.ToDouble ?
At this point I can't check if the value of "a" is <= 0 to set the value for "a" to 0 because "a" can contain negative values, so I don't know how to solve this problem.
Any Ideas ?
Thank you all
-
The binary representation is not the same as an integer, when the convert to double happens and then is used in the calculation it gives a double which is reasonable, but in your calculation as an example you expectthe result to be a integer on your check you will have to compare by converting it back to an integer.. Or compare the value with a double
I have come across a small marginal difference when converting integers to double
Sri
Similar Threads
-
By Force Flow in forum Java
Replies: 4
Last Post: 06-29-2006, 01:01 PM
-
By Sandra in forum VB Classic
Replies: 0
Last Post: 07-19-2002, 01:41 AM
-
By Anthony Meo in forum Enterprise
Replies: 0
Last Post: 01-15-2002, 04:44 PM
-
By Ted Young in forum Database
Replies: 7
Last Post: 08-07-2001, 03:53 PM
-
By Sarel j Smit in forum Database
Replies: 1
Last Post: 05-24-2001, 10:35 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks