[Numpy-discussion] Large numbers into float128

Berthold Höllmann berthold@xn--hllmanns-n4a...
Sun Oct 30 04:38:40 CDT 2011


Matthew Brett <matthew.brett@gmail.com> writes:

> Hi,
>
> Can anyone think of a good way to set a float128 value to an
> arbitrarily large number?
>
> As in
>
> v = int_to_float128(some_value)
>
> ?
>
> I'm trying things like
>
> v = np.float128(2**64+2)
>
> but, because (in other threads) the float128 seems to be going through
> float64 on assignment, this loses precision, so although 2**64+2 is
> representable in float128, in fact I get:
>
> In [35]: np.float128(2**64+2)
> Out[35]: 18446744073709551616.0
>
> In [36]: 2**64+2
> Out[36]: 18446744073709551618L
>
> So - can anyone think of another way to assign values to float128 that
> will keep the precision?

Just use float128 all the was through, and avoid casting to float in
between:

.>>> "%20.1f"%float(2**64+2)
'18446744073709551616.0'
.>>> np.float128(np.float128(2)**64+2)
18446744073709551618.0

Regards
Berthold
>
> Thanks a lot,
>
> Matthew

-- 
A: Weil es die Lesbarkeit des Textes verschlechtert.
F: Warum ist TOFU so schlimm?
A: TOFU
F: Was ist das größte Ärgernis im Usenet?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111030/e0328db6/attachment.bin 


More information about the NumPy-Discussion mailing list