# [Numpy-discussion] Large numbers into float128

Charles R Harris charlesr.harris@gmail....
Sat Oct 29 22:02:37 CDT 2011

```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/e18e94c5b0f54775c46b1c690491b8bd6f07eb49/nibabel/floating.py
>
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111029/4a021966/attachment.html
```