473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Float confusion

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.
Nov 14 '05 #1
7 2029
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.
--
Martin Ambuhl
Nov 14 '05 #2
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.

Nov 14 '05 #3
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
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.

Nov 14 '05 #4
Nobody wrote:

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).

--
pete
Nov 14 '05 #5
On Sun, 18 Jan 2004 07:31:11 GMT, in comp.lang.c , Nobody
<no****@nowhere.invalid> wrote:
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.

--
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>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #6
Nobody <no****@nowhere.invalid> wrote in message news:<26****************@soyshake-tastes-great.com>...
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.


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.

--
Peter
Nov 14 '05 #7
Mark McIntyre wrote:
On Sun, 18 Jan 2004 07:31:11 GMT, in comp.lang.c , Nobody
<no****@nowhere.invalid> wrote:

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?

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ian Watts | last post by:
I am trying without success to understand what is going on. I am developing a site at http://www.ianwatts.info/edensor/ http://www.ianwatts.info/edensor/edensor.css and I have two problems....
12
by: Michael Rozdoba | last post by:
http://digitalrat.co.uk/test/index.html I float a div left in body prior to a div with top margin set. I expected the float to rise to the top of body, but instead in Firefox & Opera its top...
12
by: Nobody | last post by:
I'm trying to understand why this program... #include <stdio.h> #include <float.h> int main(void) { int i = 123456789 ; int j = 987654321 ;
10
by: Alexander Malkis | last post by:
What is the biggest float number f so that -f doesn't overflow and is still representable as a float. -- Best regards, Alex. PS. To email me, remove "loeschedies" from the email address...
2
by: pango | last post by:
I write below code in my program: float f=0.371f; int i=(int)(f*1000.0f); I think the result of "i" should be "371",but in fact it is "370",why?How to solve it?
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
37
by: The87Boy | last post by:
Hey all I have a problem with float I should write a program, where you are getting some numbers from the command-line and try to find the maximum and minimum-values of these numbers. I need...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.