DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Problems with ... Convert ?

1. Registered User
Join Date
Dec 2003
Location
Italy
Posts
250

## 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

2. Registered User
Join Date
May 2005
Location
UK
Posts
278
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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise