* long->double->long goes negative
@ 2000-09-29 0:44 Uncle George
2000-09-29 4:09 ` Brad Lucier
0 siblings, 1 reply; 10+ messages in thread
From: Uncle George @ 2000-09-29 0:44 UTC (permalink / raw)
To: axp-list
What is wrong with this picture: Somehow I expected a positive number &
a result like 0x7fffffffffffffffL
Am I expecting too much: signed - floating point blues.
results are: lb=9223372036854775807,
db=(9.223372036854775808e+18, 43e0000000000000),
la=-9223372036854775808
main()
{
long lb,la;
double db,da;
union { long q; double d; } u;
lb =0x7fffffffffffffffL;
db = (double) lb;
la = (long) db;
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
2000-09-29 0:44 Uncle George
@ 2000-09-29 4:09 ` Brad Lucier
2000-09-29 9:41 ` Uncle George
0 siblings, 1 reply; 10+ messages in thread
From: Brad Lucier @ 2000-09-29 4:09 UTC (permalink / raw)
To: axp-list
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
@ 2000-09-29 7:49 Phil Carmody
0 siblings, 0 replies; 10+ messages in thread
From: Phil Carmody @ 2000-09-29 7:49 UTC (permalink / raw)
To: axp-list
The correct answer is of course "Scott Nudds flies out of your nose" (comp.lang.c.moderated and comp.lang.asm.x86 reference frm years back)
<<<
An rvalue of a floating point type can be converted to an rvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
>>>
_Undefined_ behaviour means that electrocuting the user is one of the other options. Just don't do it. If it formatted your hard disk that's because you invoked undefined behaviour.
Phil
(with language lawyer hat on)
> > lb =0x7fffffffffffffffL;
> > db = (double) lb;
> > la = (long) db;
Judge Lewis Kaplan's idiocy finally proved August 17, 2000:
"Defendants, on the other hand, are adherents of a movement
that believes that information should be available without
charge to anyone clever enough to break into the computer
systems or data storage media in which it is located."
_______________________________________________________________________
Free Unlimited Internet Access! Try it now!
http://www.zdnet.com/downloads/altavista/index.html
_______________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
2000-09-29 4:09 ` Brad Lucier
@ 2000-09-29 9:41 ` Uncle George
2000-09-29 9:56 ` Dr. David Gilbert
0 siblings, 1 reply; 10+ messages in thread
From: Uncle George @ 2000-09-29 9:41 UTC (permalink / raw)
To: axp-list
so u expect +int -> +float -> -int to be an A-OK situation. Gee what other
positive float numbers would u expect to give u negative results.
it should be either a 0, or a long.MAX value. going neg ( or long.MIN ) is
not reasonable or rational
/gat
In my opinion, the results would be ok if it was converted to an unsigned
long. Then the high order bit would not be representative of a sign bit.
Brad Lucier wrote:
> 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
>
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
2000-09-29 9:41 ` Uncle George
@ 2000-09-29 9:56 ` Dr. David Gilbert
2000-09-29 10:57 ` Uncle George
0 siblings, 1 reply; 10+ messages in thread
From: Dr. David Gilbert @ 2000-09-29 9:56 UTC (permalink / raw)
To: axp-list
On Fri, 29 Sep 2000, Uncle George wrote:
> so u expect +int -> +float -> -int to be an A-OK situation. Gee what other
> positive float numbers would u expect to give u negative results.
I'm more surprised by the fact it didn't through a SIGFPE; heck so much as
breathing in the wrong way on an Alpha's floating point normally does....
Dave
/------------------------------------------------------------------\
| Dr. David Alan Gilbert | Work:dg@px.uk.com +44-161-286-2000 Ex258|
| -------- G7FHJ --------|---------------------------------------- |
| Home: dave@treblig.org http://www.treblig.org |
\------------------------------------------------------------------/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
2000-09-29 9:56 ` Dr. David Gilbert
@ 2000-09-29 10:57 ` Uncle George
2000-09-29 11:55 ` Dr. David Gilbert
0 siblings, 1 reply; 10+ messages in thread
From: Uncle George @ 2000-09-29 10:57 UTC (permalink / raw)
To: axp-list
actuall, in this case it does cause a fault - and its the kernel/math fixup
routines that returns the results u see :-/
/gat
"Dr. David Gilbert" wrote:
> On Fri, 29 Sep 2000, Uncle George wrote:
>
> > so u expect +int -> +float -> -int to be an A-OK situation. Gee what other
> > positive float numbers would u expect to give u negative results.
>
> I'm more surprised by the fact it didn't through a SIGFPE; heck so much as
> breathing in the wrong way on an Alpha's floating point normally does....
>
> Dave
>
> /------------------------------------------------------------------\
> | Dr. David Alan Gilbert | Work:dg@px.uk.com +44-161-286-2000 Ex258|
> | -------- G7FHJ --------|---------------------------------------- |
> | Home: dave@treblig.org http://www.treblig.org |
> \------------------------------------------------------------------/
>
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
2000-09-29 10:57 ` Uncle George
@ 2000-09-29 11:55 ` Dr. David Gilbert
0 siblings, 0 replies; 10+ messages in thread
From: Dr. David Gilbert @ 2000-09-29 11:55 UTC (permalink / raw)
To: axp-list
On Fri, 29 Sep 2000, Uncle George wrote:
> actuall, in this case it does cause a fault - and its the kernel/math fixup
> routines that returns the results u see :-/
So shouldn't they throw a SIGFPE when they realise that they can't fit the
integer into the float?
Dave
/------------------------------------------------------------------\
| Dr. David Alan Gilbert | Work:dg@px.uk.com +44-161-286-2000 Ex258|
| -------- G7FHJ --------|---------------------------------------- |
| Home: dave@treblig.org http://www.treblig.org |
\------------------------------------------------------------------/
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: long->double->long goes negative
@ 2000-09-29 11:58 Rick Gorton
2000-09-29 14:43 ` Uncle George
0 siblings, 1 reply; 10+ messages in thread
From: Rick Gorton @ 2000-09-29 11:58 UTC (permalink / raw)
To: 'axp-list@redhat.com'
I suggest that you peruse a copy of ieee754-1985, the ieee floating
point specification for a detailed description of why some of these
things can and do happen. You're working on the edge cases of IEEE,
and those are fairly tricky. A quick rule-of-thumb is that it is fair
to assume that any floating point value can vary by an amount equal to
the value of the least significant bit for any operation.
/-- 0x7fffffff fffffffe
0x7fffffff ffffffff
\-- 0x80000000 00000000
/-- 0x3fefffff ffffffff
0x3ff00000 00000000
\-- 0x3ff00000 00000001
Input Input + 0.0e0
There are other things which affect the result, such as an odd-even
when to add, and when not to.
Another thing you can look at is paranoia.c (from someplace on the net)
which I think has commentary in the code about what's happening.
Yeah, sometimes it's counter-intuitive.
Rick
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
2000-09-29 11:58 Rick Gorton
@ 2000-09-29 14:43 ` Uncle George
0 siblings, 0 replies; 10+ messages in thread
From: Uncle George @ 2000-09-29 14:43 UTC (permalink / raw)
To: axp-list
I believe u missed the point. The conversion of a float to long/int should
have the same sign. There does not appear to be any reason for it
whatsoever. It just seems that there is a small set of +FP numbers that
will result in negative integers.
/gat
Rick Gorton wrote:
> I suggest that you peruse a copy of ieee754-1985, the ieee floating
> point specification for a detailed description of why some of these
> things can and do happen. You're working on the edge cases of IEEE,
> and those are fairly tricky. A quick rule-of-thumb is that it is fair
> to assume that any floating point value can vary by an amount equal to
> the value of the least significant bit for any operation.
>
> /-- 0x7fffffff fffffffe
> 0x7fffffff ffffffff
> \-- 0x80000000 00000000
>
> /-- 0x3fefffff ffffffff
> 0x3ff00000 00000000
> \-- 0x3ff00000 00000001
>
> Input Input + 0.0e0
>
> There are other things which affect the result, such as an odd-even
> when to add, and when not to.
> Another thing you can look at is paranoia.c (from someplace on the net)
> which I think has commentary in the code about what's happening.
> Yeah, sometimes it's counter-intuitive.
>
> Rick
>
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: long->double->long goes negative
@ 2000-09-29 16:36 Brad Lucier
0 siblings, 0 replies; 10+ messages in thread
From: Brad Lucier @ 2000-09-29 16:36 UTC (permalink / raw)
To: axp-list
> so u expect +int -> +float -> -int to be an A-OK situation. Gee what other
> positive float numbers would u expect to give u negative results.
>
> it should be either a 0, or a long.MAX value. going neg ( or long.MIN ) is
> not reasonable or rational
> /gat
>
> In my opinion, the results would be ok if it was converted to an unsigned
> long. Then the high order bit would not be representative of a sign bit.
Let me make a suggestion: when you post to this list, say what bothers
you explicitly. Here, you don't seem to like how the Alpha kernel
implements behavior that is undefined according to the C standard; if
you had said this up front, then I wouldn't have replied, because there
is nothing sensible that I could have added.
Brad
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2000-09-29 16:36 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-09-29 16:36 long->double->long goes negative Brad Lucier
-- strict thread matches above, loose matches on Subject: below --
2000-09-29 11:58 Rick Gorton
2000-09-29 14:43 ` Uncle George
2000-09-29 7:49 Phil Carmody
2000-09-29 0:44 Uncle George
2000-09-29 4:09 ` Brad Lucier
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox