float confusion

N

Nobody

I'm trying to understand why this program...

#include <stdio.h>
#include <float.h>

int
main(void)
{
int i = 123456789 ;
int j = 987654321 ;

float k = i ;
float m = j ;
float n ;
float p ;

n = m / 1000000000 ;

p = i + n ;

printf( "%d %d %f %f %f %f %f\n", i, j, k, m, n, k + n, p ) ;

return 0 ;
}

....generates this output:

123456789 987654321 123456792.000000 987654336.000000 0.987654
123456792.987654 123456792.000000

Things confusing me, in order:

1) 123456789 -> 123456792.000000 ? ("...92" ?)
2) 987654321 -> 987654336.000000 ? ("...36" ?)
3) Where'd the .987654 go after storing the addition into p ?

I'm wading through Harbison & Steele currently, but a link or
reference to this specific topic someplace (or an explanation) would
be very helpful.

Suse Linux 8.2, gcc 3.3 20030226 (prerelease)
 
M

Martin Ambuhl

Nobody said:
I'm trying to understand why this program...

#include <stdio.h>
#include <float.h>

You don't use this header for anything, so you needn't #include it.
int
main(void)
{
int i = 123456789 ;
int j = 987654321 ;

float k = i ;
float m = j ;
float n ;
float p ;

n = m / 1000000000 ;

p = i + n ;

printf( "%d %d %f %f %f %f %f\n", i, j, k, m, n, k + n, p ) ;

return 0 ;
}

...generates this output:

123456789 987654321 123456792.000000 987654336.000000 0.987654
123456792.987654 123456792.000000

There is plenty on floating point problems in the FAQ, so check there
first. Since floats need not have more than 6 significant digits, I fail
to see your problem.
 
N

Nobody

Martin said:
You don't use this header for anything, so you needn't #include it.


There is plenty on floating point problems in the FAQ, so check there
first.

Already did. Sorry I didn't mention it.
Since floats need not have more than 6 significant digits, I fail
to see your problem.

Well, hush my mouth! I'll just use double, then.
 
S

Sidney Cadot

As far as I know, there is no guarantee for floats to have at least six
significant digits in standard C.
Well, hush my mouth! I'll just use double, then.

As far as I know, there is no guarantee for doubles to have at least six
significant digits in standard C, either.

In many modern implementations, 'float' and 'double' translate to the
IEEE-754 'single' and 'double' formats.

For IEEE-754 single precision numbers:

16777216 is representable, while 16777217 is not representable; this is
the smallest integer case where this occurs --> significant digits = 7

For IEEE-754 double precision numbers:

9007199254740992 is representable, while 9007199254740993 is not
representable; this is the smallest integer case where this occurs -->
significant digits = 15.

So contrary to popular belief, the double precision IEEE type cannot
store 16 significant decimal digits.

Best regards, Sidney
 
P

Peter Nilsson

Sidney Cadot said:
As far as I know, there is no guarantee for floats to have at least six
significant digits in standard C.

Look for FLT_DIG and DBL_DIG (and calculations thereof) in the standards.
 
J

Joe Wright

Sidney said:
As far as I know, there is no guarantee for floats to have at least six
significant digits in standard C.


As far as I know, there is no guarantee for doubles to have at least six
significant digits in standard C, either.

In many modern implementations, 'float' and 'double' translate to the
IEEE-754 'single' and 'double' formats.

For IEEE-754 single precision numbers:

16777216 is representable, while 16777217 is not representable; this is
the smallest integer case where this occurs --> significant digits = 7

For IEEE-754 double precision numbers:

9007199254740992 is representable, while 9007199254740993 is not
representable; this is the smallest integer case where this occurs -->
significant digits = 15.

So contrary to popular belief, the double precision IEEE type cannot
store 16 significant decimal digits.

Best regards, Sidney

FLT_DIG is 6 and DBL_DIG is 15 on my IEEE system. Where did you get 16?
#define FLT_MAX_INT (1.6777215e+07)
#define DBL_MAX_INT (9.007199254740991e+15)
 
M

Martin Ambuhl

Sidney said:
As far as I know, there is no guarantee for floats to have at least six
significant digits in standard C.

You don't know much, do you? FLT_DIG, representing the number of
significant digits in a float, must be at least 6.

As far as I know, there is no guarantee for doubles to have at least six
significant digits in standard C, either.

You don't know much, do you? DBL_DIG, representing the number of
significant digits in a double, must be at leat 10.
 
S

Sidney Cadot

Peter said:
Look for FLT_DIG and DBL_DIG (and calculations thereof) in the standards.

Ah yes.... (bangs head against wall). Thanks for the correction.

Regards, Sidney
 
S

Sidney Cadot

Martin said:
You don't know much, do you? FLT_DIG, representing the number of
significant digits in a float, must be at least 6.




You don't know much, do you? DBL_DIG, representing the number of
significant digits in a double, must be at leat 10.

Tarring and feathering myself right now. Will write 1000 lines of "don't
talk about stuff you don't know" on the chalkboard when done.

Best regards,

Sidney
 
C

CBFalconer

Martin said:
You don't know much, do you? FLT_DIG, representing the number of
significant digits in a float, must be at least 6.


You don't know much, do you? DBL_DIG, representing the number of
least six significant digits in a double, must be at leat 10.

Are you a student at the Dan Pop School of Diplomacy?
 
T

Tak-Shing Chan

Martin Ambuhl wrote:
... snip ...

Are you a student at the Dan Pop School of Diplomacy?

<joke>Dan Pop School of Diplomacy (DPSD) is founded by Dan
Pop's associates on 19 Jan 2004. DPSD is a prestigious
degree-granting institution with highly visible research in
diplomatic relations and pervasive communications. With classes
taught by leading experts in the field, this school offers the
M.D. (Master of Diplomacy) and Ph.D. (Philosophy of Dan) degrees.
Scholarships are available. Apply now!</joke>

Tak-Shing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top