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
- برمجة بايثون
- 2021-09-13
- mhanasmh00489829403
الأجوبة
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]
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال