0

Can someone explain why some Numpy numbers aren't whole integers? When I run this:

print(np.sqrt(2.)**2)

I get:

 2.0000000000000004

And why is it that I get

[ 0.          1.11111111  2.22222222  3.33333333  4.44444444  5.55555556

 6.66666667  7.77777778  8.88888889 10.        ]

when I print

print(np.linspace(0, 10, 10))
mozway
  • 194,879
  • 13
  • 39
  • 75
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – mozway Nov 09 '21 at 11:52

1 Answers1

2

From numpy's documentation:

    dtype : dtype, optional
        The type of the output array.  If `dtype` is not given, the data type
        is inferred from `start` and `stop`. The inferred dtype will never be
        an integer; `float` is chosen even if the arguments would produce an
        array of integers.

This can be overridden by explicitly specifying dtype:

np.linspace(0, 10, 10, dtype=np.int64)
> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10])
Arnab De
  • 402
  • 4
  • 12