This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: linux i-86 problem with rounding (gcc-3.1.1 & 3.2)


>===== Original Message From Martin Dickopp <firefly-mail@gmx.net> =====

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

Actually, I think it would have to be u = (unsigned int)(floor(i*f+0.5));
just to make sure that the default round is not to the nearest integer.

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

The question was not that I assume that the rounding was infinitely precise, 
but that the rounding from the same floating point value was inconsistent only 
on one platform.  Additionally, the rounding from u = (unsigned int) (i*f); 
was not the same as the rounding from g = (i*f); u = (unsigned int)g;

Additionally, the -O option in the compilation caused the rounding error to 
disappear, something that does not seem reasonable.

Had this been a problem of always rounding down, then the inconsistency would 
not have appeared.

In the actual program being maintained, there appear to be too many locations 
in the code to find every placy where the rounding function could be applied.

-- 
Said the fox to the fish, "Join me ashore".
 The fish are the Jews, Torah is our water

Hillel (Sabba) Markowitz - sabbahem@bcpl.net


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]