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)) 
1

1 Answer 1

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]) 
Sign up to request clarification or add additional context in comments.

2 Comments

But why would they even produce an array of floats? I don't get the point
Perhaps to make the API more generic, and leave round-up / round-down to the user

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.