summaryrefslogtreecommitdiff
path: root/doc/source/reference
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2020-12-28 10:16:18 -0700
committerGitHub <noreply@github.com>2020-12-28 10:16:18 -0700
commiteb66994093be5b57324580cf389b0ec1b41e9e1f (patch)
tree9df422ac03e27fc51a2307bbd9a7b5b4bc7a4c5b /doc/source/reference
parent504fd7b2eedb90dd3aa0b326ac8c3120118b5f2d (diff)
parent4832edc55ffa7ba66ced716bfebbec05d93f8097 (diff)
downloadnumpy-eb66994093be5b57324580cf389b0ec1b41e9e1f.tar.gz
Merge pull request #18007 from eric-wieser/float-printing-doc
DOC: Add a brief explanation of float printing
Diffstat (limited to 'doc/source/reference')
-rw-r--r--doc/source/reference/arrays.scalars.rst39
1 files changed, 39 insertions, 0 deletions
diff --git a/doc/source/reference/arrays.scalars.rst b/doc/source/reference/arrays.scalars.rst
index 29438f70f..227d12d4d 100644
--- a/doc/source/reference/arrays.scalars.rst
+++ b/doc/source/reference/arrays.scalars.rst
@@ -108,6 +108,11 @@ Integer types
:members: __init__
:exclude-members: __init__
+.. note::
+
+ The numpy integer types mirror the behavior of C integers, and can therefore
+ be subject to :ref:`overflow-errors`.
+
Signed integer types
++++++++++++++++++++
@@ -169,6 +174,40 @@ Inexact types
:members: __init__
:exclude-members: __init__
+.. note::
+
+ Inexact scalars are printed using the fewest decimal digits needed to
+ distinguish their value from other values of the same datatype,
+ by judicious rounding. See the ``unique`` parameter of
+ `format_float_positional` and `format_float_scientific`.
+
+ This means that variables with equal binary values but whose datatypes are of
+ different precisions may display differently::
+
+ >>> f16 = np.float16("0.1")
+ >>> f32 = np.float32(f16)
+ >>> f64 = np.float64(f32)
+ >>> f16 == f32 == f64
+ True
+ >>> f16, f32, f64
+ (0.1, 0.099975586, 0.0999755859375)
+
+ Note that none of these floats hold the exact value :math:`\frac{1}{10}`;
+ ``f16`` prints as ``0.1`` because it is as close to that value as possible,
+ whereas the other types do not as they have more precision and therefore have
+ closer values.
+
+ Conversely, floating-point scalars of different precisions which approximate
+ the same decimal value may compare unequal despite printing identically:
+
+ >>> f16 = np.float16("0.1")
+ >>> f32 = np.float32("0.1")
+ >>> f64 = np.float64("0.1")
+ >>> f16 == f32 == f64
+ False
+ >>> f16, f32, f64
+ (0.1, 0.1, 0.1)
+
Floating-point types
++++++++++++++++++++