ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> edu-sig
edu-sig
RE: [Edu-sig] More re: Advanced or beginner?
by Tim Peters other posts by this author
Aug 1 2001 9:57PM messages near this date
[Edu-sig] More re: Advanced or beginner? | [Edu-sig] Re: Changing the Division Operator -- PEP 238, rev 1.12
[Kirby Urner]
>  ...
>  Question:
> 
>  Why does float -> long work like this:
> 
>      >>> long(1.0826786300000142e+045)
>      1082678630000014218353234260713996413124476928L
> 
>  and not like this?
> 
>      >>> long(1.0826786300000142e+045)
>      1082678630000014200000000000000000000000000000L
> 
>  Kirby

Because your machine floating-point isn't decimal, it's binary:

> >> x = 2.**100
> >> print x
1.26765060023e+030
> >> print long(x)
1267650600228229401496703205376
> >> print 2L ** 100
1267650600228229401496703205376
> >>

So making long(2.**100)

1267650600230000000000000000000L

instead would be plain wrong.  The same thing happens in your example, but
is harder to picture because, offhand, I bet you don't know the exact
IEEE-754 bit pattern corresponding to 1.0826786300000142e+045 <wink> .

> >> x = 1.0826786300000142e+045
> >> import math
> >> mantissa, exponent = math.frexp(x)
> >> print mantissa
0.758577950788
> >> print exponent
150
> >> print math.ldexp(mantissa, 53)
6.832662753e+015
> >> mantissa_as_long = long(math.ldexp(mantissa, 53))
> >> mantissa_as_long
6832662753002049L
> >> print mantissa_as_long << (exponent-53)
1082678630000014218353234260713996413124476928
> >> print long(x)
1082678630000014218353234260713996413124476928
> >>

IOW, long(some_huge_integer_in_float_format) *does* have a large number of
trailing zeroes, but in base 2, not necessarily in base 10.

> >> print hex(long(x))
0x308C8A88868482000000000000000000000000L
> >>


_______________________________________________
Edu-sig mailing list
Edu-sig@[...].org
http://mail.python.org/mailman/listinfo/edu-sig
Thread:
Kirby Urner
Kirby Urner
Tim Peters

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved