Looking for the greatest negative float value
Bengt Richter
bokr at oz.net
Wed Jun 11 18:49:24 EDT 2003
On 11 Jun 2003 20:36:38 GMT, bokr at oz.net (Bengt Richter) wrote:
>On Wed, 11 Jun 2003 18:52:17 +0200, "Gilles Lenfant" <glenfant at 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
More information about the Python-list
mailing list