Huge rounding errors when converting floats to doubles 
Author Message
 Huge rounding errors when converting floats to doubles

Hi,

I have a piece of code which looks like this

 float f1 = 2166.7f;
 double d = f1;

 FILE *MyFile = ::fopen("file.txt","w");
 ::fprintf(MyFile,"f1 %f d %lf\n",float(f1),double(d));
  ::fclose(MyFile);

I would expect the file to look like this:

2166.7 2166.7

But it looks like this:

f1 2166.699951 d 2166.699951

Does anybody have an explanation for this HUGE rounding error?

Hans Merkl



Fri, 31 May 2002 03:00:00 GMT  
 Huge rounding errors when converting floats to doubles
Hey!

You are getting 8 good didgits from a float. It's really good! If you want
more, use doubles and forget about floats. Your program will also run much
faster. Intel FPU can work only on doubles (10 byte floating point is its'
native format actually), so every opertion with a float involves conversion
float->double for arguments and double->float for the result.

Have fun and don't try to save couple bytes on floats!

Oleg


Quote:
> Hi,

> I have a piece of code which looks like this

>  float f1 = 2166.7f;
>  double d = f1;

>  FILE *MyFile = ::fopen("file.txt","w");
>  ::fprintf(MyFile,"f1 %f d %lf\n",float(f1),double(d));
>   ::fclose(MyFile);

> I would expect the file to look like this:

> 2166.7 2166.7

> But it looks like this:

> f1 2166.699951 d 2166.699951

> Does anybody have an explanation for this HUGE rounding error?

> Hans Merkl



Fri, 31 May 2002 03:00:00 GMT  
 Huge rounding errors when converting floats to doubles
The rounding error your getting is not huge.  if you need floating point
numbers to be represented exactly then you will need to get a Binary Coded
Decimal library.

see:
http://support.microsoft.com/support/kb/articles/Q145/8/89.asp
http://support.microsoft.com/support/kb/articles/Q36/0/68.ASP

--
Visit: http://ourworld.compuserve.com/homepages/jjmarshall/



Fri, 31 May 2002 03:00:00 GMT  
 Huge rounding errors when converting floats to doubles

Quote:
> Hi,

> I have a piece of code which looks like this

>  float f1 = 2166.7f;
>  double d = f1;

>  FILE *MyFile = ::fopen("file.txt","w");
>  ::fprintf(MyFile,"f1 %f d %lf\n",float(f1),double(d));
>   ::fclose(MyFile);

> I would expect the file to look like this:

> 2166.7 2166.7

> But it looks like this:

> f1 2166.699951 d 2166.699951

> Does anybody have an explanation for this HUGE rounding error?

You're using floating point, which is an inexact representation. The
behavior you see is expected. Read the following paper for lots more
information:

http://www.acm.org/pubs/citations/journals/surveys/1991-23-1/p5-goldb...



Sat, 01 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how to round a float or double in C++

2. Rounding of floats and doubles

3. rounding floats and doubles

4. how to round a float or double in C++

5. Convert double to string without rounding

6. 'double' rounding errors

7. Rounding errors in FLOAT values

8. Convert a String to float/double

9. Convert double to D-float format

10. Converting double to float

11. Need function to convert float/double/long to string using a specified format

12. converting int to float (or double)

 

 
Powered by phpBB® Forum Software