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. 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
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.
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.
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
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 =---
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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 ;
|
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...
|
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?
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
| |