19

So I have a problem with my numerical program, and I'm curious about whether it is a precision problem (i.e. round-off error). Is there a quick way to change all the float arrays in my program into float128 arrays, without going through my code and typing dtype='float128' all over the place. My arrays are all float64, but i never explicitly wrote dtype='float64', so i was hoping there was a way to change this default behavior.

Amir
  • 10,600
  • 9
  • 48
  • 75
Eskil
  • 3,385
  • 5
  • 28
  • 32
  • There is a `numpy.float_` constant set to `float64`, but changing it to `numpy.float128` and asking `numpy.array([1.1]).dtype` keeps returning `float64`. – eumiro Mar 18 '11 at 10:08
  • The support for float128 is sketchy, irc it won't work with windows. Try the same with float96. – tillsten Mar 18 '11 at 10:47
  • I'm working a Linux (Ubuntu) system. How is the support there? The fact that it won't work on windows doesn't really matter since it is just a check for myself. – Eskil Mar 18 '11 at 10:53
  • 1
    Late reply, but the better solution here is to use `numpy.longdouble`, which goes to `float128` on linux and `float96` on windows. – Henry Gomersall Mar 29 '13 at 09:01

1 Answers1

19

I don't think there is a central "configuration" you could change to achieve this. Some options what you could do:

  1. If you are creating arrays only by very few of NumPy's factory functions, substitute these functions by your own versions. If you import these functions like

    from numpy import empty
    

    you can just do

    from numpy import float128, empty as _empty
    def empty(*args, **kwargs):
        kwargs.update(dtype=float128)
        _empty(*args, **kwargs)
    

    If you are doing

    import numpy
    

    you can write a module mynumpy.py

    from numpy import *
    _empty = empty
    def empty(*args, **kwargs):
        kwargs.update(dtype=float128)
        _empty(*args, **kwargs)
    

    and import it like

    import mynumpy as numpy
    
  2. Refactor your code to always use dtype=myfloat. This will make such changes easy in the future. You can combine this approach with the use of numpy.empty_like(), numpy.zeros_like() and numpy.ones_like() wherever appropriate to have the actual data type hardcoded in as few places as possible.

  3. Sub-class numpy.ndarray and only use your custom constructors to create new arrays.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 2
    Trying these in order is probably best: 1-2-3. The last option (Sub-classing `numpy.ndarray`) is elegant, but there are pitfalls. For example, I accidentally broke array garbage collection by storing slices of the array as attributes, i.e. `self.myslice = self[0, :]`. My RAM would slowly fill up as I did more and more operations. – Will Martin Jul 27 '16 at 14:53