## digitalmars.D - Negative infinity?

• Sean Kelly (4/4) Sep 22 2004 Am I correct in assuming that D does not support negative infinity? Tha...
• Nick (22/24) Sep 22 2004 From what I can see, a float can have the value -0 but it is treated as ...
• Sean Kelly (18/42) Sep 22 2004 Hrm... so I tred this:
• Nick (3/6) Sep 22 2004 Try -1e-1000000000. Maybe there should be a float.negzero property?
• Sean Kelly (5/10) Sep 22 2004 No can do. The compiler errors out with "number is not representable." ...
• Nick (3/7) Sep 23 2004 Strange, worked for me...
• Arcane Jill (5/9) Sep 23 2004 How about (1/-f.infinity)?
• Sean Kelly (4/7) Sep 23 2004 Darn. There I go forgetting basic mathematics. I hope this doesn't mea...
• Arcane Jill (8/11) Sep 23 2004 The behavior of IEEE floats is pretty bizarre as far as mathematics goes...
• Walter (10/26) Sep 23 2004 know of
• Walter (5/8) Sep 22 2004 was an
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
```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
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
```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
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
```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
```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

Or ((-1)/f.infinity)?
Or -(1/f.infinity)?

Jill
```
Sep 23 2004
Sean Kelly <sean f4.ca> writes:
```In article <cittdg\$k81\$1 digitaldaemon.com>, Arcane Jill says...
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
```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
"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

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.

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

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

and
it does, what about negative zero?

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