Fix custom float dtype f-string formatting truncating exponent notation#369
Fix custom float dtype f-string formatting truncating exponent notation#369xhmelon wants to merge 2 commits intojax-ml:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hi @jakevdp, would you please activate CI workflows and review this PR? |
| // Round to 6 significant digits to match PyCustomFloat_Str/Repr, which | ||
| // use std::ostringstream with its default precision of 6. This avoids | ||
| // exposing false precision from the float64 expansion. | ||
| char buf[14]; // max %.6g output: "-9.98378e+38" + '\0' = 14 |
There was a problem hiding this comment.
I'm finding this part strange. For any floating point type, numpy appears to do the following: https://github.com/numpy/numpy/blob/f105cf2d7c20c9829b431c2ae0cdb1f07efaccf2/numpy/_core/src/multiarray/scalartypes.c.src#L633
i.e., convert the type to a python float using nb_format and then call PyObject_Format on the result.
Is there a reason for us to deviate here?
This PR fixes #341.
Formatting custom float scalars (e.g.
bfloat16) would fallback tonp.generic.__format__and be truncated by string length.python -c "from ml_dtypes import bfloat16; print(f'{bfloat16(1e-6):.8}')"This PR adds a
__format__method to all custom float types that:std::ostringstreamfromPyCustomFloat_Str/Reprmethods.float.__format__for the actual formatting.After the fix:
python -c "from ml_dtypes import bfloat16; print(f'{bfloat16(1e-6):.8}')"python -c "from ml_dtypes import bfloat16; print(f'{bfloat16(1e-6):.8e}')"