# round function

I think I would use the round function when I wanted to turn a number with a decimal into an integer, but it's not exactly a rounding function. Specifically, it is as follows.

``````>>> round(3.5) #I want you to be 4
4              #Become 4
>>> round(2.5) #I want you to be 3
2              #Become 2(<-??)
>>> round(2.50000001) #I want you to be 3
3              #Become 3
``````

This is also mentioned in the Official Documentation (https://docs.python.org/ja/3/library/functions.html?highlight=round#round), to (even) +0.5 when rounding to an integer. When it is, it is not rounded up and is rounded down. This is due to the nature of the bank rounding algorithm, and other cases do not seem to change when rounding to an integer. (Corrected after being pointed out by @shiracamus at 12:21 on 9/5/2020)

# numpy round

If the built-in function round is not good, you should use numpy round, but numpy round gives the same result. This is also described in the Official Documentation.

``````>>> import numpy as np
>>> np.round(3.5)   #I want you to be 4
4.0                 #Become 4
>>> np.round(2.5)   #I want you to be 3
2.0                 #Become 2
``````

# approach

There is no problem because you can use the if statement to multiply round by one number, but if you turn the for statement for numpy's ndarray and turn it one by one, the code becomes redundant. If you do the following, 2.5 and 4.5 will also be rounded correctly.

#### `my_Definition of round function`

``````
def my_round(a:np.ndarray) -> np.ndarray:
rounded_a = np.round(a)+(((a%1)==0.5)*(a//1%2==0))
return rounded_a
``````

When I run it, it looks like this

``````>>> a = np.arange(-3,3,0.5)
>>>print(a)
[-3.  -2.5 -2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2.   2.5]
>>>print(np.round(a))
[-3. -2. -2. -2. -1. -0.  0.  0.  1.  2.  2.  2.]
>>>print(my_round(a))
[-3. -2. -2. -1. -1.  0.  0.  1.  1.  2.  2.  3.]
``````

You can see that it is rounded correctly.

### What you are doing

What `((a% 1) == 0.5) * (a // 1% 2 == 0)` is doing is` (a% 1) == 0.5` in the form of `* .5` By creating an array where 1 stands only where it is, and by creating an array where 1 stands only where the integer part is even at ʻa // 1% 2 == 0` and multiplying them, ( It's a straightforward way to add 1 only where it's even) .5. This method doesn't use a for statement (which is said to be slow in python), so it's expected to be a bit faster than doing it straightforwardly with for.