Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Brad Lucier <lucier@math.purdue.edu>
To: axp-list@redhat.com
Subject: Re: long->double->long goes negative
Date: Thu, 28 Sep 2000 23:09:02 -0500 (EST)	[thread overview]
Message-ID: <200009290409.XAA20223@polya.math.purdue.edu> (raw)
In-Reply-To: <39D3E5FA.E6BB8539@voicenet.com> from "Uncle George" at Sep 28, 2000 07:44:42 PM

Well, I don't find your posting very clear, but it doesn't seem
that strange to me.  Perhaps I'm missing something.

> main()
> {
>         long lb,la;
>         double db,da;
>         union { long    q;  double  d;  } u;
>         lb =0x7fffffffffffffffL;
>         db = (double) lb;

db has only 53 bits mantissa, so lb cannot have all its 63 bits
represented exactly, and on conversion lb is rounded to
the nearest number, which happens to be 9.223372036854775808e+18

>         la = (long) db;

Here db does have an integral value, but that value is bigger than
the largest long, so it can't be represented exactly as a long;
the bit pattern of the converted result is  0x8000000000000000,
which doesn't seem such a bad choice in this situation.

>         u.d = db;
>         printf("lb=%ld, db=(%23.18e, %lx), la=%ld\n", lb, db,u.q, la);
> }
> 
> cc -g -mieee a.c
> ./a.out




  reply	other threads:[~2000-09-29  4:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-09-29  0:44 long->double->long goes negative Uncle George
2000-09-29  4:09 ` Brad Lucier [this message]
2000-09-29  9:41   ` Uncle George
2000-09-29  9:56     ` Dr. David Gilbert
2000-09-29 10:57       ` Uncle George
2000-09-29 11:55         ` Dr. David Gilbert
  -- strict thread matches above, loose matches on Subject: below --
2000-09-29  7:49 Phil Carmody
2000-09-29 11:58 Rick Gorton
2000-09-29 14:43 ` Uncle George
2000-09-29 16:36 Brad Lucier
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox