www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Negative infinity?

reply Sean Kelly <sean f4.ca> writes:
Am I correct in assuming that D does not support negative infinity?  That was an
assumption I made in unFormat and I thought I'd ask.  Assuming I was wrong and
it does, what about negative zero?


Sean
Sep 22 2004
next sibling parent reply Nick <Nick_member pathlink.com> writes:
In article <cisidv$2shn$1 digitaldaemon.com>, Sean Kelly says...
Am I correct in assuming that D does not support negative infinity? 

By the looks of it, no. See example below.
Assuming I was wrong and it does, what about negative zero?

From what I can see, a float can have the value -0 but it is treated as equal to 0 (which of course is correct behaviour). Consider this example: # import std.stdio; # void main() # { # // Produce negative zero # float f=-1; # for(int i = 0; i < 100; i++) # f /= 1000; # writefln(f); # assert(f==-f); # # // Produce negative infinity # f=1/f; # writefln(f); # } Output is: -0 -inf Nick
Sep 22 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <cismki$2uqa$1 digitaldaemon.com>, Nick says...
In article <cisidv$2shn$1 digitaldaemon.com>, Sean Kelly says...
Am I correct in assuming that D does not support negative infinity? 

By the looks of it, no. See example below.
Assuming I was wrong and it does, what about negative zero?

From what I can see, a float can have the value -0 but it is treated as equal to 0 (which of course is correct behaviour). Consider this example: # import std.stdio; # void main() # { # // Produce negative zero # float f=-1; # for(int i = 0; i < 100; i++) # f /= 1000; # writefln(f); # assert(f==-f); # # // Produce negative infinity # f=1/f; # writefln(f); # } Output is: -0 -inf

Hrm... so I tred this: # import std.stdio; # # void main() # { # float f = -0; # writefln( f ); # f = f.infinity * -1; # writefln( f ); # } And it printed: 0 -inf So while there seems to be an easy way to get negative infinity, I don't know of one to get negative zero. I'll update unFormat to handle negative infinity, but unless someone has a brilliant idea I'm not going to bother with negative zero. Sean
Sep 22 2004
next sibling parent reply Nick <Nick_member pathlink.com> writes:
In article <cisnr9$2vfj$1 digitaldaemon.com>, Sean Kelly says...
So while there seems to be an easy way to get negative infinity, I don't know of
one to get negative zero.  I'll update unFormat to handle negative infinity, but
unless someone has a brilliant idea I'm not going to bother with negative zero.

Try -1e-1000000000. Maybe there should be a float.negzero property? Nick
Sep 22 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <cisp9c$30a6$1 digitaldaemon.com>, Nick says...
In article <cisnr9$2vfj$1 digitaldaemon.com>, Sean Kelly says...
So while there seems to be an easy way to get negative infinity, I don't know of
one to get negative zero.  I'll update unFormat to handle negative infinity, but
unless someone has a brilliant idea I'm not going to bother with negative zero.

Try -1e-1000000000. Maybe there should be a float.negzero property?

No can do. The compiler errors out with "number is not representable." A negzero property would be nice, unless Walter can suggest an alternate method that doesn't involve a loop? Sean
Sep 22 2004
parent Nick <Nick_member pathlink.com> writes:
In article <cisrr5$31gf$1 digitaldaemon.com>, Sean Kelly says...
Try -1e-1000000000. Maybe there should be a float.negzero property?

No can do. The compiler errors out with "number is not representable." A negzero property would be nice, unless Walter can suggest an alternate method that doesn't involve a loop?

Strange, worked for me... Nick
Sep 23 2004
prev sibling next sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <cisnr9$2vfj$1 digitaldaemon.com>, Sean Kelly says...

So while there seems to be an easy way to get negative infinity, I don't know of
one to get negative zero.  I'll update unFormat to handle negative infinity, but
unless someone has a brilliant idea I'm not going to bother with negative zero.

Sean

How about (1/-f.infinity)? Or ((-1)/f.infinity)? Or -(1/f.infinity)? Jill
Sep 23 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <cittdg$k81$1 digitaldaemon.com>, Arcane Jill says...
How about (1/-f.infinity)?
Or ((-1)/f.infinity)?
Or -(1/f.infinity)?

Darn. There I go forgetting basic mathematics. I hope this doesn't mean I'm over the hill ;) Thanks Jill! Sean
Sep 23 2004
parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <ciur0c$1ii1$1 digitaldaemon.com>, Sean Kelly says...

Darn.  There I go forgetting basic mathematics.

The behavior of IEEE floats is pretty bizarre as far as mathematics goes, so I wouldn't worry about it. In /real/ mathematics, of course, there is only one zero, and infinity is not even a real number (though the concept has its uses in limits, set theory and so on).
I hope this doesn't mean I'm
over the hill ;)  Thanks Jill!

You're /never/ over the hill. Have fun. Jill
Sep 23 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:cisnr9$2vfj$1 digitaldaemon.com...
 Hrm... so I tred this:

 # import std.stdio;
 #
 # void main()
 # {
 #     float f = -0;
 #     writefln( f );
 #     f = f.infinity * -1;
 #     writefln( f );
 # }

 And it printed:

 0
 -inf

 So while there seems to be an easy way to get negative infinity, I don't

 one to get negative zero.  I'll update unFormat to handle negative

 unless someone has a brilliant idea I'm not going to bother with negative

Negative 0 does print as just "0". Also, the way to generate a negative 0 is to write: -0.0 writing -0 will not, since the negation happens before the conversion to double.
Sep 23 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:cisidv$2shn$1 digitaldaemon.com...
 Am I correct in assuming that D does not support negative infinity?  That

 assumption I made in unFormat and I thought I'd ask.  Assuming I was wrong

 it does, what about negative zero?

Negative infinity as well as negative zero are fully supported in D.
Sep 22 2004