Bug 141848 - Weird rounding of floats
Summary: Weird rounding of floats
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
(Show other bugs)
Version: 3
Hardware: All Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Keywords:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2004-12-04 00:19 UTC by Bastien Nocera
Modified: 2007-11-30 22:10 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2004-12-06 07:04:05 UTC
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

Description Bastien Nocera 2004-12-04 00:19:48 UTC
gcc-3.4.2-6.fc3
Also happens with gcc 3.2 and 4.0-pre (as shipped with FC3)

$ cat test.c

void
test (float zoom)
{
        printf ("%d %f\n", (int) (zoom * 100.0), (zoom * 100));
}

int main (int argc, char **argv)
{
        test (1.01);
        return 0;
}

$ gcc -o test test.c
$ ./test
100 100.999999

It's supposed to print:
100 101.000000

Comment 1 Jakub Jelinek 2004-12-06 07:04:05 UTC
There is nothing wrong on this, that's how floating point works.
printf does no rounding.  1.01f is rounded when stored in float and the result
is inexact too.
E.g. 1.01 rounded to fit into float and double is:
float 0x1.028f5cp+0
double 0x1.028f5c28f5c29p+0
and the exact number is actually 0x1.028f5c with period 0x28f5c.
As you can see, when rounded to float it is slightly smaller and double is
slightly bigger than the exact value.  Therefore it is no surpise if this
0x0.0000028f5c28f5c28f5c28f5c28f5c28f5c28f5c28f5c... difference multiplied
by 100 already shows up among the important bits.



Note You need to log in before you can comment on or make changes to this bug.