Issue with Double 
Author Message
 Issue with Double

I am trying to do a simple thing and I don't understand what is going
wrong. I am reading something from database and assigning it to a host
variable which has data type 'double'. Inside the database, this field
has value 1002.82 (I have verified this by querying the database.),
however, when I read it, my host variable gets the value
1002.8200000000001. My program does some accumulation of such values
and some other computations and finally converts to EBCDIC. I do not
get expected result.

So I tried the following -

        double HostAmount;
        char   szTemp[31];

        strcpy(szTemp, "1002.82");
        HostAmount = 0.00;
        HostAmount = atof(szTemp);

The moment I execute this, HostAmount becomes 1002.8200000000001 and I
can't get rid of those zeroz followed by 1. I don't want them because
they are causing erroneous results.

Can somebody tell me what is going on and what can I do to solve this
problem?

Any help is appreciated.
Thanks in advance.
- PP



Wed, 09 Feb 2005 04:48:14 GMT  
 Issue with Double

Quote:

> I am trying to do a simple thing and I don't understand what is going
> wrong. I am reading something from database and assigning it to a host
> variable which has data type 'double'. Inside the database, this field
> has value 1002.82 (I have verified this by querying the database.),
> however, when I read it, my host variable gets the value
> 1002.8200000000001. My program does some accumulation of such values
> and some other computations and finally converts to EBCDIC. I do not
> get expected result.

> So I tried the following -

>         double HostAmount;
>         char   szTemp[31];

> strcpy(szTemp, "1002.82");
> HostAmount = 0.00;
> HostAmount = atof(szTemp);

> The moment I execute this, HostAmount becomes 1002.8200000000001 and I
> can't get rid of those zeroz followed by 1. I don't want them because
> they are causing erroneous results.

> Can somebody tell me what is going on and what can I do to solve this
> problem?

> Any help is appreciated.

The easiest thing to do is to assume that floats and doubles have a small
random error. This is because decimal points of base ten cannot be
represented entirely accurately in binary floating point. (Advanced users
can improve on this assumption by using the macros in the floating point
library, but a random error is close enough to the truth for our puroposes).

Before you use the result, round it off

double x = floor(x * 100.0 + 0.5).

Now x is accurately 100 * the value you want.



Wed, 09 Feb 2005 05:14:10 GMT  
 Issue with Double

Quote:

> I am trying to do a simple thing and I don't understand what is going
> wrong. I am reading something from database and assigning it to a host
> variable which has data type 'double'. Inside the database, this field
> has value 1002.82 (I have verified this by querying the database.),
> however, when I read it, my host variable gets the value
> 1002.8200000000001. [...]

    This is Question 14.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

        http://www.eskimo.com/~scs/C-faq/top.html

--



Wed, 09 Feb 2005 05:19:27 GMT  
 Issue with Double

Quote:

>I am trying to do a simple thing and I don't understand what is going
>wrong. I am reading something from database and assigning it to a host
>variable which has data type 'double'. Inside the database, this field
>has value 1002.82 (I have verified this by querying the database.),
>however, when I read it, my host variable gets the value
>1002.8200000000001.

This is completely normal because floating point numbers are not
exact. Please read the FAQ starting from 14.1.

<snippage>

Quote:
>The moment I execute this, HostAmount becomes 1002.8200000000001 and I
>can't get rid of those zeroz followed by 1. I don't want them because
>they are causing erroneous

Then you have to stop using floating point numbers, and use fixed
point ones. The easiest way is to use long integers and multiply
everything by some scaling factor, but be careful about ranges.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Wed, 09 Feb 2005 05:55:00 GMT  
 Issue with Double

Quote:

> I am trying to do a simple thing and I don't understand what is going
> wrong. I am reading something from database and assigning it to a host
> variable which has data type 'double'. Inside the database, this field
> has value 1002.82 (I have verified this by querying the database.),
> however, when I read it, my host variable gets the value
> 1002.8200000000001.

There is no guarantee that a double has 17 decimal digits of precision.
The FAQ has adequate discussion of FP issues; consult it before posting.


Wed, 09 Feb 2005 15:44:27 GMT  
 Issue with Double

Quote:


> > I am trying to do a simple thing and I don't understand what is going
> > wrong. I am reading something from database and assigning it to a host
> > variable which has data type 'double'. Inside the database, this field
> > has value 1002.82 (I have verified this by querying the database.),
> > however, when I read it, my host variable gets the value
> > 1002.8200000000001.

> There is no guarantee that a double has 17 decimal digits of precision.
> The FAQ has adequate discussion of FP issues; consult it before posting.

Thanks to everybody who has sincerely tried to help. Malcolm, I will
try what you have suggested. I hope it helps me. Thanks.

As for referring to FAQ, it is not exactly right to assume that
questions are posted without doing any research. All postings are not
by inexperienced programmers and all questions are not trivial. FAQ
identify the problem but do not always have a solution, as in this
case. Also just because something is discussed in FAQ does not mean
that nobody has tried and found a workaround. It doesn't harm to ask
for help. If anything that is known as an issue 'as per FAQ' should
not be posted here, it would greatly help to have this warning
displayed prominently on the main index page.

Having said this, I do admire and use FAQ's frequently and appreciate
all the good work that has gone into compiling them.

I wish I had the option of not using floats and doubles. I don't and
hence I needed help.

Thanks,
PP.



Thu, 10 Feb 2005 07:32:26 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Double Byte Issues

2. result of double*double is no double

3. Serialization issue or Socket comm issue??

4. can long double be less precise than double?

5. Problem with Double.IsNan() and Double.Nan

6. epsilon for float, double long double

7. Difference between double and long double?

8. Converting long double to double

9. double != double best way [Q]

10. 80bit double from mac to 64bit double to pc

11. View double* as array of double?

12. Internal structure of long double and double

 

 
Powered by phpBB® Forum Software