Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looking for the greatest negative float value

3,538 views
Skip to first unread message

Gilles Lenfant

unread,
Jun 11, 2003, 12:52:17 PM6/11/03
to
Hi,

I need the same as...

import sys
negmaxint = - sys.maxint -1

... but for float data

any hint ?

Thanks in advance.

--Gilles

Mike C. Fletcher

unread,
Jun 11, 2003, 1:20:26 PM6/11/03
to
The maximum negative float is probably somewhere close to:
-1.79769313e+308

but it could be different on other platforms (I'm on a Win2K machine).

HTH,
Mike

Gilles Lenfant wrote:

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Fernando Perez

unread,
Jun 11, 2003, 4:13:00 PM6/11/03
to
Gilles Lenfant wrote:

> I need the same as...
>
> import sys
> negmaxint = - sys.maxint -1
>
> ... but for float data
>
> any hint ?
>

You want the kinds package, part of Numeric:


In [2]: import kinds

In [3]: kinds.default_float_kind.M
kinds.default_float_kind.MAX kinds.default_float_kind.MIN
kinds.default_float_kind.MAX_10_EXP kinds.default_float_kind.MIN_10_EXP
kinds.default_float_kind.MAX_EXP kinds.default_float_kind.MIN_EXP

In [3]: kinds.default_float_kind.MIN
Out[3]: 2.2250738585072014e-308

Best,

f.

Bengt Richter

unread,
Jun 11, 2003, 4:36:38 PM6/11/03
to
On Wed, 11 Jun 2003 18:52:17 +0200, "Gilles Lenfant" <glen...@NOSPAM.bigfoot.com> wrote:

>Hi,
>
>I need the same as...
>
>import sys
>negmaxint = - sys.maxint -1
>
>... but for float data
>
>any hint ?
>

To get what you want (wyw) maybe you have to compute it. E.g.,

>>> fmin = -1.0
>>> while 1:
... fnext = fmin*2.0-1.0
... if fnext==fmin: break
... wyw = fmin
... fmin = fnext
...
>>> `wyw`
'-8.9884656743115795e+307'

I'm not sure how portable this is, but the actual value
can surely vary according to platform. I suppose you could set up a site-specific
initialization to put such a value as sys.minfloat if you wanted to.

The timbot will have the best info ;-)

Regards,
Bengt Richter

Mike C. Fletcher

unread,
Jun 11, 2003, 4:48:07 PM6/11/03
to
Fernando Perez wrote:
... question: how to get largest negative float number snipped ...

>You want the kinds package, part of Numeric:
>
>

...

>In [3]: kinds.default_float_kind.MIN
>Out[3]: 2.2250738585072014e-308
>

Uh, that's a really-really small number that's vanishingly larger than
0.0, negative exponents make fractions, not large negative numbers...

Not sure what it's trying to give you,
Mike

Erik Max Francis

unread,
Jun 11, 2003, 5:41:00 PM6/11/03
to
"Mike C. Fletcher" wrote:

> Uh, that's a really-really small number that's vanishingly larger than
> 0.0, negative exponents make fractions, not large negative numbers...

Well, the original poster _did_ ask for the "greatest negative float
value." That would be -epsilon.

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ The public is a fool.
\__/ Alexander Pope

Gerrit Holl

unread,
Jun 11, 2003, 5:59:02 PM6/11/03
to
Gilles Lenfant wrote:
> Subject: Looking for the greatest negative float value

Hmm. I think you are looking for -float('inf'). That is
the greatest negative float value, since it is <n for each
n.

Gerrit.

--
2. If any one bring an accusation against a man, and the accused go to
the river and leap into the river, if he sink in the river his accuser
shall take possession of his house. But if the river prove that the
accused is not guilty, and he escape unhurt, then he who had brought the
accusation shall be put to death, while he who leaped into the river shall
take possession of the house that had belonged to his accuser.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

Mel Wilson

unread,
Jun 11, 2003, 5:10:48 PM6/11/03
to
In article <bc7lvb$gm3$1...@news-reader12.wanadoo.fr>,

"Gilles Lenfant" <glen...@NOSPAM.bigfoot.com> wrote:
>Hi,
>
>I need the same as...
>
>import sys
>negmaxint = - sys.maxint -1
>
>... but for float data
>
>any hint ?

I'm not completely sure this is right, but..


v, w, x = -1.0, 0.0, 0.0
i = 0
while v < w and i < 2000:
x, w, v = w, v, v*2.0
i += 1
print i
print x, w, v

d = -x / 2.0
y = x
while x > w:
y, x = x, x-d
d /= 2.0
print repr (y)


winds up printing


1025
-8.98846567431e+307 -1.#INF -1.#INF
-1.7976931348623157e+308

and that last number seems credible.

Regards. Mel.

Erik Max Francis

unread,
Jun 11, 2003, 6:30:12 PM6/11/03
to
Gerrit Holl wrote:

> Hmm. I think you are looking for -float('inf'). That is
> the greatest negative float value, since it is <n for each
> n.

Then we get into discussions about whether the IEEE infinity really
warrants being described as a "value." :-)

Besides, I'm pretty sure that whether or not IEEE is the chosen
implementation or indeed whether it's known as 'inf' (thus making
float('inf') legitimate) is dependent on the interpreter. Python itself
makes no guarantees about such things.

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE

/ \ If I had another face, do you think I'd wear this one?
\__/ Abraham Lincoln

Bengt Richter

unread,
Jun 11, 2003, 6:49:24 PM6/11/03
to
On 11 Jun 2003 20:36:38 GMT, bo...@oz.net (Bengt Richter) wrote:

>On Wed, 11 Jun 2003 18:52:17 +0200, "Gilles Lenfant" <glen...@NOSPAM.bigfoot.com> wrote:
>
>>Hi,
>>
>>I need the same as...
>>
>>import sys
>>negmaxint = - sys.maxint -1
>>
>>... but for float data
>>
>>any hint ?
>>
>
>To get what you want (wyw) maybe you have to compute it. E.g.,
>
> >>> fmin = -1.0
> >>> while 1:
> ... fnext = fmin*2.0-1.0
> ... if fnext==fmin: break
> ... wyw = fmin
> ... fmin = fnext
> ...
> >>> `wyw`
> '-8.9884656743115795e+307'
>

Actually, that didn't do it right.

Better (depending on timbot's long-float conversion ;-):
(There's may be more bits temporarily internally, but the double
you get back loses them if so. The last n in float(n) has all ones
below the first zero after 53 bits, I believe, but that's not the
number you care about, I think.)

====< maxneg.py >================
def maxneg():
n=-1L
try:
while 1:
x = float(n*2L)
n = n*2L
except:
pass
bit = -n/2L
while bit:
try:
float(n-bit)
n -= bit
except: pass
bit = bit/2
return float(n)

fn = maxneg()
print repr(fn)
print hex(long(fn))
print long(fn)
print hex(((-1L<<53)+1)<<(1024-53))
=================================

[15:45] C:\pywk\clp>maxneg.py
-1.7976931348623157e+308
-0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000L
-17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276
687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932
894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988
1250404026184124858368
-0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000L


>I'm not sure how portable this is, but the actual value
>can surely vary according to platform. I suppose you could set up a site-specific
>initialization to put such a value as sys.minfloat if you wanted to.
>
>The timbot will have the best info ;-)
>

Still applies ;-)

Regards,
Bengt Richter

Mike C. Fletcher

unread,
Jun 11, 2003, 6:10:02 PM6/11/03
to
Erik Max Francis wrote:

>"Mike C. Fletcher" wrote:
>
>
>>Uh, that's a really-really small number that's vanishingly larger than
>>0.0, negative exponents make fractions, not large negative numbers...
>>
>>
>
>Well, the original poster _did_ ask for the "greatest negative float
>value." That would be -epsilon.
>
>

Obviously I just do too much 3-D work, somehow when I see "greatest" I
interpret it as "largest magnitude" (as in a vector), rather than
"compares as > all others". In my defense ;) , the original poster was
giving the example of:

-sys.maxint-1

as the analog of the number they were seeking in integers, instead of -1
:) .

Have fun,

Erik Max Francis

unread,
Jun 11, 2003, 7:28:24 PM6/11/03
to
"Mike C. Fletcher" wrote:

> In my defense ;) , the original poster
> was
> giving the example of:
>
> -sys.maxint-1
>
> as the analog of the number they were seeking in integers, instead of
> -1
> :) .

Sorry, I forgot to include a smiley face. It was indeed clear that the
original poster was asking about the most negative negative floating
point value, I was just nitpicking a (quite common) bad choice of
mathematical terminology he chose to use.

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE

/ \ We grow in time to trust the future for our answers.
\__/ Ruth Benedict

Dan Bishop

unread,
Jun 12, 2003, 1:01:43 AM6/12/03
to
mwi...@the-wire.com (Mel Wilson) wrote in message news:<Yr55+ks/KTSM...@the-wire.com>...

> In article <bc7lvb$gm3$1...@news-reader12.wanadoo.fr>,
> "Gilles Lenfant" <glen...@NOSPAM.bigfoot.com> wrote:
> >Hi,
> >
> >I need the same as...
> >
> >import sys
> >negmaxint = - sys.maxint -1
> >
> >... but for float data
[snip]

> -1.7976931348623157e+308
>
> and that last number seems credible.

Close. The exact value, per the IEEE 754 standard, is -(2 ** 1024 - 2
** 972), or -1.7976931348623155e+308. How did you get a *higher*
magnitude?

Gilles Lenfant

unread,
Jun 12, 2003, 7:22:53 AM6/12/03
to
Many thanks to all of you.
Finally, I made a constant that suits my needs :-)
Assuming that the target platforms uses standard IEEE floating.

--Gilles

mailto:gil...@pilotsystems.net
"Gilles Lenfant" <glen...@NOSPAM.bigfoot.com> a écrit dans le message de
news: bc7lvb$gm3$1...@news-reader12.wanadoo.fr...

Mel Wilson

unread,
Jun 12, 2003, 11:15:50 AM6/12/03
to
In article <ad052e5c.0306...@posting.google.com>,

I didn't get it, it was the code I posted. Maybe we
should look at repr.

Regards. Mel.

Bengt Richter

unread,
Jun 12, 2003, 12:11:22 PM6/12/03
to

Because UIAM the IEEE 754 standard allows 53 significant bits, not 52.
Note that your constant can be rewritten:

>>> -(2**1024 - 2**972) == -(2**52 - 1)*2**972
1

Yet 2**53-1 is eactly representable in a floating point double:

>>> float(2**53-1)
9007199254740991.0

and so of course is the single-bit 2**53

>>> float(2**53+0)
9007199254740992.0

but not the next number - it rounds down to 2**53

>>> float(2**53+1)
9007199254740992.0

starting with 2**53, the lsb is counts by 2

>>> float(2**53+2)
9007199254740994.0

So therefore your constant is a 52-bit number, and we can make one that has 53:

Your number:

>>> float(-(2**52 - 1)*2**972)
-1.7976931348623155e+308

The 53-bit number:

>>> float(-(2**53 - 1)*2**971)
-1.7976931348623157e+308

or like your original format:

>>> float(-(2**1024 - 2**971))
-1.7976931348623157e+308

Bottom line: UIAM the 972 should have been 971
(i.e., 2**971 is the lsb in that number, not 2**972).

Regards,
Bengt Richter

Dan Bishop

unread,
Jun 12, 2003, 7:53:08 PM6/12/03
to
bo...@oz.net (Bengt Richter) wrote in message news:<bca8na$4b6$0...@216.39.172.122>...

> On 11 Jun 2003 22:01:43 -0700, dan...@yahoo.com (Dan Bishop) wrote:
>
> >mwi...@the-wire.com (Mel Wilson) wrote in message news:<Yr55+ks/KTSM...@the-wire.com>...
> >> In article <bc7lvb$gm3$1...@news-reader12.wanadoo.fr>,
> >> "Gilles Lenfant" <glen...@NOSPAM.bigfoot.com> wrote:
> >> >Hi,
> >> >
> >> >I need the same as...
> >> >
> >> >import sys
> >> >negmaxint = - sys.maxint -1
> >> >
> >> >... but for float data
> [snip]
> >> -1.7976931348623157e+308
> >>
> >> and that last number seems credible.
> >
> >Close. The exact value, per the IEEE 754 standard, is -(2 ** 1024 - 2
> >** 972), or -1.7976931348623155e+308. How did you get a *higher*
> >magnitude?
>
> Because UIAM the IEEE 754 standard allows 53 significant bits, not 52.
> Note that your constant can be rewritten:
>
> >>> -(2**1024 - 2**972) == -(2**52 - 1)*2**972
> 1
>
> Yet 2**53-1 is eactly representable in a floating point double:
>
> >>> float(2**53-1)
> 9007199254740991.0

Ah! It's so much clearer when you write it that way.

I knew that doubles had 53 bits (and therefore, epsilon=2**-52), and
that maxfloat was slightly less than 2 ** 1024 (because 2 ** 1024's
bit pattern is used for infinity), and had erroneously calculated
maxfloat as

(1 - epsilon) * 2 ** 1024

instead of the correct

(1 + 1/2 + 1/4 + ... + epsilon) * 2 ** 1023
= (2 - epsilon) * 2 ** 1023
= (1 - epsilon/2) * 2 ** 1024

And sure enough, printf("%.17g", 0x7FEFFFFFFFFFFFFFLL) in C (plus
enough casting to bypass compiler warnings) prints
"1.7976931348623157e+308".

0 new messages