Skip to content

MAINT: Improve float16 and float32 printing #28703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review comments
  • Loading branch information
f2013519 committed Apr 15, 2025
commit b6797ca2be09d0fb05fc5f4fb17f17b866c2f8be
6 changes: 1 addition & 5 deletions numpy/_core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,11 +992,7 @@ def fillFormat(self, data):
abs_non_zero = absolute(finite_vals[finite_vals != 0])

# consider data type while deciding the max cutoff for exp format
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is another round, then might as put this in the if statement. Indeed, in principle one can do the assignment with the walrus,

if len(abs_non_zero := absolute(finite_vals[finite_vals != 0])) != 0:
    exp_cutoff_max = ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

exp_cutoff_max = 1.e8
if data.dtype == "float16":
exp_cutoff_max = 1.e3
elif data.dtype == "float32":
exp_cutoff_max = 1.e7
exp_cutoff_max = 10.**min(8, np.finfo(data.dtype).precision)

if len(abs_non_zero) != 0:
max_val = np.max(abs_non_zero)
Expand Down
11 changes: 2 additions & 9 deletions numpy/_core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1304,12 +1304,9 @@ legacy_@name@_format@kind@(npy_@name@ val){

#define IS_@kind@

#define SINGLE_TYPE 0
#define DOUBLE_TYPE 1
#define LONG_DOUBLE_TYPE 2
/**begin repeat1
* #name = float, double, longdouble#
* #dtype = 0, 1, 2#
* #max_positional = 1.e6L, 1.e16L, 1.e16L#
* #Name = Float, Double, LongDouble#
* #NAME = FLOAT, DOUBLE, LONGDOUBLE#
* #n = f, , l#
Expand Down Expand Up @@ -1340,11 +1337,7 @@ static PyObject *
}
else {
npy_@name@ absval = val < 0 ? -val : val;
#if @dtype@ == SINGLE_TYPE
use_positional = absval < 1.e7L && absval >= 1.e-4L;
#else
use_positional = absval < 1.e16L && absval >= 1.e-4L;
#endif
use_positional = absval < @max_positional@ && absval >= 1.e-4L;
}

if (use_positional) {
Expand Down
16 changes: 8 additions & 8 deletions numpy/_core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,16 +923,16 @@ def test_gh_28679(self):
# test cutoff to exponent notation for half, single, and complex64
a = np.half([999, 999])
b = np.half([999, 1000])
c = np.single([9999999, 9999999])
d = np.single([9999999, -10000000])
e = np.complex64([9999999 + 9999999j, 9999999 + 9999999j])
f = np.complex64([9999999 + 9999999j, 9999999 + -10000000j])
c = np.single([999999, 999999])
d = np.single([999999, -1000000])
e = np.complex64([999999 + 999999j, 999999 + 999999j])
f = np.complex64([999999 + 999999j, 999999 + -1000000j])
assert_equal(str(a), "[999. 999.]")
assert_equal(str(b), "[9.99e+02 1.00e+03]")
assert_equal(str(c), "[9999999. 9999999.]")
assert_equal(str(d), "[ 9.999999e+06 -1.000000e+07]")
assert_equal(str(e), "[9999999.+9999999.j 9999999.+9999999.j]")
assert_equal(str(f), "[9999999.+9.999999e+06j 9999999.-1.000000e+07j]")
assert_equal(str(c), "[999999. 999999.]")
assert_equal(str(d), "[ 9.99999e+05 -1.00000e+06]")
assert_equal(str(e), "[999999.+999999.j 999999.+999999.j]")
assert_equal(str(f), "[999999.+9.99999e+05j 999999.-1.00000e+06j]")

def test_legacy_mode_scalars(self):
# in legacy mode, str of floats get truncated, and complex scalars
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/tests/test_scalarprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_gh_28679(self):
assert_equal(str(np.half(-1000)), "-1e+03")
assert_equal(str(np.single(0.000099)), "9.9e-05")
assert_equal(str(np.single(-0.000100001)), "-0.000100001")
assert_equal(str(np.single(9999999)), "9999999.0")
assert_equal(str(np.single(-10000000)), "-1e+07")
assert_equal(str(np.single(999999)), "999999.0")
assert_equal(str(np.single(-1000000)), "-1e+06")

def test_dragon4(self):
# these tests are adapted from Ryan Juckett's dragon4 implementation,
Expand Down
Loading
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