Skip to content

Commit 5f0f716

Browse files
committed
MAINT: Improve float16 and float32 printing
1 parent 67fb349 commit 5f0f716

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

numpy/_core/arrayprint.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,19 @@ def fillFormat(self, data):
990990

991991
# choose exponential mode based on the non-zero finite values:
992992
abs_non_zero = absolute(finite_vals[finite_vals != 0])
993+
994+
# consider data type while deciding the max cutoff for exp format
995+
exp_cutoff_max = 1.e8
996+
if data.dtype == "float16":
997+
exp_cutoff_max = 1.e3
998+
elif data.dtype == "float32":
999+
exp_cutoff_max = 1.e7
1000+
9931001
if len(abs_non_zero) != 0:
9941002
max_val = np.max(abs_non_zero)
9951003
min_val = np.min(abs_non_zero)
9961004
with errstate(over='ignore'): # division can overflow
997-
if max_val >= 1.e8 or (not self.suppress_small and
1005+
if max_val >= exp_cutoff_max or (not self.suppress_small and
9981006
(min_val < 0.0001 or max_val / min_val > 1000.)):
9991007
self.exp_format = True
10001008

numpy/_core/src/multiarray/scalartypes.c.src

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,12 @@ legacy_@name@_format@kind@(npy_@name@ val){
13041304

13051305
#define IS_@kind@
13061306

1307+
#define SINGLE_TYPE 0
1308+
#define DOUBLE_TYPE 1
1309+
#define LONG_TYPE 2
13071310
/**begin repeat1
13081311
* #name = float, double, longdouble#
1312+
* #dtype = 0, 1, 2#
13091313
* #Name = Float, Double, LongDouble#
13101314
* #NAME = FLOAT, DOUBLE, LONGDOUBLE#
13111315
* #n = f, , l#
@@ -1336,7 +1340,11 @@ static PyObject *
13361340
}
13371341
else {
13381342
npy_@name@ absval = val < 0 ? -val : val;
1339-
use_positional = absval < 1.e16L && absval >= 1.e-4L;
1343+
#if @dtype@ == SINGLE_TYPE
1344+
use_positional = absval < 1.e7L && absval >= 1.e-4L;
1345+
#else
1346+
use_positional = absval < 1.e16L && absval >= 1.e-4L;
1347+
#endif
13401348
}
13411349

13421350
if (use_positional) {
@@ -1485,7 +1493,7 @@ halftype_@kind@(PyObject *self)
14851493
absval = floatval < 0 ? -floatval : floatval;
14861494

14871495
PyObject *string;
1488-
if (absval == 0 || (absval < 1.e16 && absval >= 1.e-4) ) {
1496+
if (absval == 0 || (absval < 1.e3 && absval >= 1.e-4) ) {
14891497
string = format_half(val, 0, -1, 0, TrimMode_LeaveOneZero, -1, -1, -1);
14901498
}
14911499
else {

numpy/_core/tests/test_arrayprint.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,21 @@ def test_floatmode(self):
919919
a = np.float64.fromhex('-1p-97')
920920
assert_equal(np.float64(np.array2string(a, floatmode='unique')), a)
921921

922+
def test_gh_28679(self):
923+
# test cutoff to exponent notation for half, single, and complex64
924+
a = np.half([999, 999])
925+
b = np.half([999, 1000])
926+
c = np.single([9999999, 9999999])
927+
d = np.single([9999999, -10000000])
928+
e = np.complex64([9999999 + 9999999j, 9999999 + 9999999j])
929+
f = np.complex64([9999999 + 9999999j, 9999999 + -10000000j])
930+
assert_equal(str(a), "[999. 999.]")
931+
assert_equal(str(b), "[9.99e+02 1.00e+03]")
932+
assert_equal(str(c), "[9999999. 9999999.]")
933+
assert_equal(str(d), "[ 9.999999e+06 -1.000000e+07]")
934+
assert_equal(str(e), "[9999999.+9999999.j 9999999.+9999999.j]")
935+
assert_equal(str(f), "[9999999.+9.999999e+06j 9999999.-1.000000e+07j]")
936+
922937
def test_legacy_mode_scalars(self):
923938
# in legacy mode, str of floats get truncated, and complex scalars
924939
# use * for non-finite imaginary part

numpy/_core/tests/test_scalarprint.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ def check(v):
4848
check(1e15)
4949
check(1e16)
5050

51+
def test_gh_28679(self):
52+
# test cutoff to exponent notation for half and single
53+
assert_equal(str(np.half(-0.000099)), "-9.9e-05")
54+
assert_equal(str(np.half(0.0001)), "0.0001")
55+
assert_equal(str(np.half(999)), "999.0")
56+
assert_equal(str(np.half(-1000)), "-1e+03")
57+
assert_equal(str(np.single(0.000099)), "9.9e-05")
58+
assert_equal(str(np.single(-0.000100001)), "-0.000100001")
59+
assert_equal(str(np.single(9999999)), "9999999.0")
60+
assert_equal(str(np.single(-10000000)), "-1e+07")
61+
5162
def test_dragon4(self):
5263
# these tests are adapted from Ryan Juckett's dragon4 implementation,
5364
# see dragon4.c for details.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy