public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: linux i-86 problem with rounding (gcc-3.1.1 & 3.2)
@ 2002-09-18 11:37 Hillel (Sabba) Markowitz
  2002-09-18 11:56 ` Martin Dickopp
  0 siblings, 1 reply; 7+ messages in thread
From: Hillel (Sabba) Markowitz @ 2002-09-18 11:37 UTC (permalink / raw)
  To: Martin Dickopp; +Cc: GCC Help

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

^ permalink raw reply	[flat|nested] 7+ messages in thread
* linux i-86 problem with rounding (gcc-3.1.1 & 3.2)
@ 2002-09-18  7:04 Hillel (Sabba) Markowitz
  2002-09-18  9:46 ` Martin Dickopp
  0 siblings, 1 reply; 7+ messages in thread
From: Hillel (Sabba) Markowitz @ 2002-09-18  7:04 UTC (permalink / raw)
  To: GCC Help

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

%gcc testfloat.c -o testfloat -O
%./testfloat

unsigned int result of i*f: 960
double result: 960
unsigned int result: 960


This error occurs only on the linux i-86 (redhat 7.2 or 7.3).  However, the 
results come out correctly on Solaris, DEC (4.0 and 5.1) and linux alpha 
(redhat 7.2).

Does anyone have an idea for a fix or workaround?  Is this an artifact of the 
Assembler or the floating point co-processor?  Is this something to do with 
gcc?

Please e-mail me with the answer as well as the mailing list.

Thanks,

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

Hillel (Sabba) Markowitz - sabbahem@bcpl.net

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-09-18 20:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-18 11:37 linux i-86 problem with rounding (gcc-3.1.1 & 3.2) Hillel (Sabba) Markowitz
2002-09-18 11:56 ` Martin Dickopp
2002-09-18 12:58   ` Buddy Lott
2002-09-18 13:35     ` Martin Dickopp
  -- strict thread matches above, loose matches on Subject: below --
2002-09-18  7:04 Hillel (Sabba) Markowitz
2002-09-18  9:46 ` Martin Dickopp
2002-09-18  9:59   ` Gokhan Kisacikoglu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).