Q:

Write a NumPy program to calculate round, floor, ceiling, truncated and round (to the given number of decimals) of the input, element-wise of a given array

0

Write a NumPy program to calculate round, floor, ceiling, truncated and round (to the given number of decimals) of the input, element-wise of a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
x = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print("Original array: ")
print(x)
r1 = np.around(x)
r2 = np.floor(x)
r3 = np.ceil(x)
r4 = np.trunc(x)
r5 = [round(elem) for elem in x]

print("\naround:   ", r1)
print("floor:    ",r2)
print("ceil:     ",r3)
print("trunc:    ",r4)
print("round:    ",r5)

Sample Output:

Original array: 
[ 3.1  3.5  4.5  2.9 -3.1 -3.5 -5.9]

around:    [ 3.  4.  4.  3. -3. -4. -6.]
floor:     [ 3.  3.  4.  2. -4. -4. -6.]
ceil:      [ 4.  4.  5.  3. -3. -3. -5.]
trunc:     [ 3.  3.  4.  2. -3. -3. -5.]
round:     [3.0, 4.0, 4.0, 3.0, -3.0, -4.0, -6.0]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now