Large numbers into float128

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? Thanks a lot, Matthew

Hi, On Sat, Oct 29, 2011 at 3:55 PM, Matthew Brett <matthew.brett@gmail.com> wrote:
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?
To answer my own question - I found an unpleasant way of doing this. Basically it is this: def int_to_float128(val): f64 = np.float64(val) res = val - int(f64) return np.float128(f64) + np.float128(res) Used in various places here: https://github.com/matthew-brett/nibabel/blob/e18e94c5b0f54775c46b1c690491b8... Best, Matthew

On Sat, Oct 29, 2011 at 8:49 PM, Matthew Brett <matthew.brett@gmail.com>wrote:
Hi,
On Sat, Oct 29, 2011 at 3:55 PM, Matthew Brett <matthew.brett@gmail.com> wrote:
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?
To answer my own question - I found an unpleasant way of doing this.
Basically it is this:
def int_to_float128(val): f64 = np.float64(val) res = val - int(f64) return np.float128(f64) + np.float128(res)
Used in various places here:
https://github.com/matthew-brett/nibabel/blob/e18e94c5b0f54775c46b1c690491b8...
Best,
It might be useful to look into mpmath. I didn't see any way to export mp values into long double, but they do offer a number of resources for working with arbitrary precision. We could maybe even borrow some of their stuff for parsing values from strings Chuck

A quick and dirty cython code is attached Use:
import Float128 a = Float128.Float128('1E500')
array([ 1e+500], dtype=float128) or
b = np.float128(1.34) * np.float128(10)**2500 b 1.3400000000000000779e+2500
Maybe there is also a way to do it in a pure python code via ctypes? Nadav ________________________________ From: numpy-discussion-bounces@scipy.org [numpy-discussion-bounces@scipy.org] On Behalf Of Charles R Harris [charlesr.harris@gmail.com] Sent: 30 October 2011 05:02 To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Large numbers into float128 On Sat, Oct 29, 2011 at 8:49 PM, Matthew Brett <matthew.brett@gmail.com<mailto:matthew.brett@gmail.com>> wrote: Hi, On Sat, Oct 29, 2011 at 3:55 PM, Matthew Brett <matthew.brett@gmail.com<mailto:matthew.brett@gmail.com>> wrote:
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?
To answer my own question - I found an unpleasant way of doing this. Basically it is this: def int_to_float128(val): f64 = np.float64(val) res = val - int(f64) return np.float128(f64) + np.float128(res) Used in various places here: https://github.com/matthew-brett/nibabel/blob/e18e94c5b0f54775c46b1c690491b8... Best, It might be useful to look into mpmath. I didn't see any way to export mp values into long double, but they do offer a number of resources for working with arbitrary precision. We could maybe even borrow some of their stuff for parsing values from strings Chuck

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?

Hi, On Sun, Oct 30, 2011 at 2:38 AM, Berthold Höllmann <berthold@xn--hllmanns-n4a.de> wrote:
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
Ah yes - sorry - that would work in this example where I know the component parts of the number, but I was thinking in the general case where I have been given any int. I think my code works for that, by casting to float64 to break up the number into parts: In [35]: def int_to_float128(val): ....: f64 = np.float64(val) ....: res = val - int(f64) ....: return np.float128(f64) + np.float128(res) ....: In [36]: int_to_float128(2**64) Out[36]: 18446744073709551616.0 In [37]: int_to_float128(2**64+2) Out[37]: 18446744073709551618.0 Thanks, Matthew
participants (4)
-
berthold@xn--hllmanns-n4a.de
-
Charles R Harris
-
Matthew Brett
-
Nadav Horesh