NumPy Rounding and Trigonometric Functions
NumPy provides functions for rounding numerical values and performing trigonometric calculations on arrays. Rounding functions help control numerical precision, while trigonometric functions such as sine, cosine, and tangent are useful in geometry, physics, engineering, and scientific computing. In this lesson, you will learn the main functions, understand angle units, and apply these operations to practical examples.
1. Introduction to Rounding Functions
Rounding changes the precision or integer direction of a numerical value. NumPy provides functions to round to a specified number of decimal places, round down, round up, or remove the fractional portion.
Common Rounding Functions
np.round()rounds to the nearest value.np.floor()rounds toward negative infinity.np.ceil()rounds toward positive infinity.np.trunc()removes the fractional part toward zero.np.rint()rounds to the nearest integer-valued float, using ties-to-even.
Output:
2. Rounding to Decimal Places
The np.round() function accepts an array and an optional
number of decimal places. A positive value rounds to decimal places,
while a negative value rounds to positions to the left of the decimal
point.
Output:
Rounding to Tens and Hundreds
Output:
3. Understanding Floor, Ceiling, and Truncation
These functions can produce different results for negative values. Understanding their direction is important when working with financial calculations, measurements, and numerical data.
| Function | Behavior | Example for -3.7 |
|---|---|---|
np.floor() |
Moves toward negative infinity. | -4.0 |
np.ceil() |
Moves toward positive infinity. | -3.0 |
np.trunc() |
Moves toward zero. | -3.0 |
np.round() |
Rounds to the nearest value, with ties-to-even for exact halfway cases. | -4.0 |
Negative values make the difference between floor and truncation easy to see.
4. Introduction to Trigonometric Functions
Trigonometric functions describe relationships between angles and sides of triangles. NumPy provides vectorized functions that calculate these values for individual numbers or entire arrays.
| Function | Description |
|---|---|
np.sin() |
Calculates the sine of each angle. |
np.cos() |
Calculates the cosine of each angle. |
np.tan() |
Calculates the tangent of each angle. |
np.arcsin() |
Calculates the inverse sine. |
np.arccos() |
Calculates the inverse cosine. |
np.arctan() |
Calculates the inverse tangent. |
np.sin(), np.cos(), or np.tan().
5. Converting Degrees to Radians
Degrees and radians are two units for measuring angles. A full circle
contains 360 degrees or \(2\pi\) radians. NumPy provides
np.deg2rad() and np.radians() for converting
degrees into radians.
Output:
Convert Radians Back to Degrees
Output:
np.deg2rad() when your input
angles are in degrees and use np.rad2deg() when converting
radian results to degrees.
6. Sine, Cosine, and Tangent
The sine, cosine, and tangent functions can be applied directly to arrays of angles expressed in radians.
Output:
The tangent of 90 degrees is mathematically undefined. Because computers use floating-point approximations, NumPy may return a very large finite number instead of infinity for this input.
Values shown are rounded approximations of common trigonometric results.
7. Inverse Trigonometric Functions
Inverse trigonometric functions calculate an angle from a trigonometric ratio. Their results are expressed in radians.
Output:
Converting an Inverse Result to Degrees
Output:
np.arcsin() and np.arccos() is from -1 to 1.
Values outside this interval produce NaN results for real-valued
calculations.
8. Hyperbolic Functions
NumPy also provides hyperbolic functions. These are related to exponential functions and are used in mathematical models, physics, engineering, and machine learning.
| Function | Description |
|---|---|
np.sinh() |
Hyperbolic sine. |
np.cosh() |
Hyperbolic cosine. |
np.tanh() |
Hyperbolic tangent. |
Output:
9. Practical Project: Angle Calculations
In this project, we will convert a collection of angles from degrees to radians and calculate their sine and cosine values.
Output:
The calculations are performed using NumPy arrays. The loop formats the results into readable rows for display.
10. Practical Project: Rounding Measurements
Scientific measurements often contain more decimal places than are needed in a report. Rounding can make results easier to read while retaining the original values for further calculations.
Output:
11. Common Mistakes and Best Practices
Passing Degrees Directly to Trigonometric Functions
NumPy expects radians. Convert degree values using
np.deg2rad() before calculating sine, cosine, or tangent.
Assuming Rounding Always Goes Up
np.round() rounds to the nearest value and uses ties-to-even
for exact halfway cases. Use np.ceil() when you specifically
need rounding toward positive infinity.
Confusing Floor and Truncation
Floor moves toward negative infinity, while truncation moves toward zero. Their results can differ for negative decimal values.
Ignoring Floating-Point Precision
Results such as cosine of 90 degrees may be extremely close to zero rather than exactly zero. Round for presentation when appropriate.
Using Inverse Functions Outside Their Domain
Real-valued arcsine and arccosine accept inputs between -1 and 1. Validate values before applying these functions.
12. Practice Exercises
Exercise 1: Decimal Rounding
Create an array of decimal values. Round each value to two decimal
places using np.round().
Exercise 2: Negative Rounding
Apply np.floor(), np.ceil(), and
np.trunc() to an array containing positive and negative
decimal values. Compare the results.
Exercise 3: Degree Conversion
Convert 0, 45, 90, 135, and 180 degrees into radians using
np.deg2rad().
Exercise 4: Trigonometric Calculations
Calculate sine, cosine, and tangent for 0, 30, 45, and 60 degrees. Display the results rounded to four decimal places.
Exercise 5: Inverse Trigonometry
Calculate the inverse sine and inverse cosine for
[0, 0.5, 1]. Convert the results into degrees.
Exercise 6: Measurement Processing
Create an array of measurement values and produce rounded, floor, and ceiling results without changing the original array.
Summary
In this lesson, you learned to round numerical arrays using
np.round(), np.floor(), np.ceil(),
and np.trunc(). You also practiced controlling decimal
precision and rounding values to tens and hundreds.
You explored trigonometric functions such as np.sin(),
np.cos(), and np.tan(), along with inverse
and hyperbolic functions. You also learned to convert angles between
degrees and radians and apply these functions to practical numerical
calculations.