This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: linux i-86 problem with rounding (gcc-3.1.1 & 3.2)
- From: Martin Dickopp <firefly-mail at gmx dot net>
- To: "Hillel (Sabba) Markowitz" <sabbahem at bcpl dot net>
- Cc: GCC Help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 18 Sep 2002 18:46:48 +0200
- Subject: Re: linux i-86 problem with rounding (gcc-3.1.1 & 3.2)
- References: <3DA44C07@webmail.bcpl.net>
On Wed, Sep 18, 2002 at 10:06:51AM -0400, Hillel (Sabba) Markowitz wrote:
> The following test program give incorrect results in a cast from
> double to int when compiled without an optimization option but
> correct results when compiled with an optimization option.
>
> int main(int argc, char **argv)
> {
> int i = 128000;
> double f = 0.0075;
> double g;
> unsigned int u;
>
> u = (unsigned int) (i*f);
> printf("unsigned int result of i*f: %u\n", u);
>
> g = i*f;
> u = (unsigned int)g;
> printf("double result: %lf\n", g);
> printf("unsigned int result: %u\n", u);
>
> return(0);
> }
>
>
> %gcc testfloat.c -o testfloat
> %./testfloat
>
> unsigned int result of i*f: 959
> double result: 960
> unsigned int result: 960
You incorrectly assume that calculations involving floating-point
numbers are infinitely precise; in reality, rounding is inevitable.
The details of the rounding may depend, amongst other things, on the
processor type and optimization level.
Solution:
u = (unsigned int) (i*f + 0.5);
This rounds i*f to the nearest unsigned integer, instead of always
rounding down. Therefore, you obtain your expected result even if
i*f is slightly less than 960.0.
Martin