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 = k + 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:

1) 123456789 -> 123456792 ("...92" ?)
2) 987654321 -> 987654336 ("...36" ?)
3) Why is storing the result of k + n into p different than the evaluation
of k + n (apparently)?

SuSE Linux 8.2, gcc 3.3 20030226 (prerelease)

It's been years since I've used float. I'm wading through Harbinson &
Steele, but a link to a germane reference (or discussion) would be a big
help, too.
 
M

Martin Ambuhl

Nobody wrote:

[A verbatum repost]
You sent this only 20 minutes after your first post, and 7 minutes after I
answered it. Please learn how to use newsgroups. There is no reason for
such behavior.
 
N

Nobody

Martin said:
Nobody wrote:

[A verbatum repost]
You sent this only 20 minutes after your first post, and 7 minutes after I
answered it. Please learn how to use newsgroups. There is no reason for
such behavior.

I cancelled the original article to correct the code listing. Chill out.
 
Y

Yusuf Motiwala

Results are due to limited precision of float. Try double and you will
not see this problem for numbers you mentioned. Refer IEEE floating
point format (ieee 754),

987654321 -> 1 110 1011 0111 1001 1010 0010 1100 01

Mantissa -> (1) 110 1011 0111 1001 1010 0010
Round up -> (1) 110 1011 0111 1001 1010 0011

Convert back -> 1 110 1011 0111 1001 1010 0011 0000 00 == 987654336

Regards,
Yusuf
 
P

pete

Nobody said:
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 = k + 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:

1) 123456789 -> 123456792 ("...92" ?)
2) 987654321 -> 987654336 ("...36" ?)
3) Why is storing the result of k + n into p
different than the evaluation of k + n (apparently)?

SuSE Linux 8.2, gcc 3.3 20030226 (prerelease)

It's been years since I've used float.
I'm wading through Harbinson & Steele,
but a link to a germane reference (or discussion)
would be a big help, too.

Here's a maybe:

float arguments get promoted to double.
p is already what it is,
but the argument promotion may have been applied to the operands
of the addition operator, k and n,
instead of to the result of the addition (k + n).
 
M

Mark McIntyre

Martin said:
Nobody wrote:

[A verbatum repost]
You sent this only 20 minutes after your first post, and 7 minutes after I
answered it. Please learn how to use newsgroups. There is no reason for
such behavior.

I cancelled the original article to correct the code listing. Chill out.

Most news servers these days ignore cancels, after there was some sort
of cancelbot war a few years back.
 
P

Peter Nilsson

Nobody said:
Martin said:
Nobody wrote:

[A verbatum repost]
You sent this only 20 minutes after your first post, and 7 minutes after I
answered it. Please learn how to use newsgroups. There is no reason for
such behavior.

I cancelled the original article to correct the code listing.

Thoroughly cancelling usenet posts is virtually impossible, hence
basically pointless. You're much better off simply replying to the
original post, pointing out your corrections.
 
A

August Derleth

Mark said:
Martin Ambuhl wrote:

Nobody wrote:

[A verbatum repost]
You sent this only 20 minutes after your first post, and 7 minutes after I
answered it. Please learn how to use newsgroups. There is no reason for
such behavior.

I cancelled the original article to correct the code listing. Chill out.


Most news servers these days ignore cancels, after there was some sort
of cancelbot war a few years back.

Wasn't that connected with the infamous Cancelpoodle in
alt.religion.scientology? Or is it something different?
 

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

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top