1

According to tooltip (at least in Spyder) for function round, if second argument is omitted or Nonethe returned result is an integer. However:

round(np.pi)
Out[214]: 3

round(np.sqrt(2))
Out[215]: 1.0

In the first case it's an integer but in the second it's not and also wrong if interpreted as a float with one digit; the natural way to interpret a number looking like that if one doesn't know the details behind the code presenting it.

I don't see the logic behind using an additional .0 when presenting a number supposedly rounded to an integer.

Oortone
  • 175
  • 7
  • 2
    can't reproduce, both return type int here. – Christian Sloper Sep 24 '21 at 07:33
  • 5
    This might be useful: https://github.com/numpy/numpy/issues/11810 – Riccardo Bucco Sep 24 '21 at 07:34
  • 1
    what version of _numpy_ are you using (what is the output of `np.__version__`)? – ronpi Sep 24 '21 at 07:40
  • 1
    I can't reproduce your issue. I'm using numpy 1.20.3 – Riccardo Bucco Sep 24 '21 at 07:46
  • 2
    I'm running Python 3.9.7 and numpy 1.21.1 and the output from both *round()* calls returns ****. Having said that, I can't see why the version of numpy might be relevant unless you're using its *round()* function in which case the return types would be **** –  Sep 24 '21 at 07:49
  • 1
    @DarkKnight It might actually be relevant. The built-in `round` function may rely on the numpy specific `__round__` function – Riccardo Bucco Sep 24 '21 at 07:55
  • The difference is occuring due to representation in numpy this is a similar issue [numpy-float64-vs-python-float](https://stackoverflow.com/questions/27098529/numpy-float64-vs-python-float) – Sangeerththan Balachandran Sep 24 '21 at 08:40
  • 1
    @DarkKnight: The version of NumPy is relevant because when Python's `round` function is called for a NumPy `float64` value, the actual rounding computation is delegated to the `float64.__round__` method. The implementation of that method was changed in NumPy 1.19.0 to be consistent with `float.__round__`. See https://numpy.org/devdocs/release/1.19.0-notes.html#change-output-of-round-on-scalars-to-be-consistent-with-python – Mark Dickinson Sep 24 '21 at 11:01
  • 2
    The issue is compounded by the fact that numpy.sqrt() returns a numpy.float64 type whereas numpy.pi is a traditional Python float –  Sep 24 '21 at 12:46
  • @DarkKnight: Yep, good point. – Mark Dickinson Sep 24 '21 at 13:50
  • @MarkDickinson I was running Numpy 1.17 and now I'm on 1.19.5 and rounding is consistent. – Oortone Sep 28 '21 at 12:27

0 Answers0