Skip to content

Commit d07fc76

Browse files
committed
NF - add best_float function
Function to select floating point type with highest precision, avoiding useless np.longdouble type on windows.
1 parent 107705a commit d07fc76

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

nibabel/casting.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,21 @@ def floor_log2(x):
451451
ip += 1
452452
rem //= 2
453453
return ip
454+
455+
456+
def best_float():
457+
""" Floating point type with best precision
458+
459+
This is nearly always np.longdouble, except on Windows, where np.longdouble
460+
is Intel80 storage, but with float64 precision for calculations. In that
461+
case we return float64 on the basis it's the fastest and smallest at the
462+
highest precision.
463+
464+
Returns
465+
-------
466+
best_type : numpy type
467+
floating point type with highest precision
468+
"""
469+
if type_info(np.longdouble)['nmant'] > type_info(np.float64)['nmant']:
470+
return np.longdouble
471+
return np.float64

nibabel/tests/test_casting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from ..casting import (float_to_int, shared_range, CastingError, int_to_float,
7-
as_int, int_abs)
7+
as_int, int_abs, best_float)
88

99
from numpy.testing import (assert_array_almost_equal, assert_array_equal)
1010

0 commit comments

Comments
 (0)