import numpy as np
ar_rad = np.array([1.57079633, 2.0943951, 2.7925268, 3.14159265])
ar_deg = np.rad2deg(ar_rad)
print(ar_deg) # [ 90.00000018 119.99999986 159.99999982 179.99999979]
numpy.rad2deg(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) The rad2deg() function multiplies each input element in radians by 180 / π to produce degree equivalents. For the inverse operation converting degrees to radians, see deg2rad() or radians(). For trigonometric calculations, review Math & ufuncs.
| Parameter | Description |
|---|---|
x | Input array or angle values in radians |
out | Optional, ndarray to store the result; must match expected output shape |
where | Optional, boolean array mask; elements where True are evaluated, False left untouched |
dtype | Optional, data-type override for output |
Passing radian angle arrays converts them to degrees. Due to floating-point precision in representing π, exact multiples like 90° or 180° may show slight fractional offsets (e.g., 90.00000018), which can be cleaned with np.round():
import numpy as np
ar_rad = np.array([1.57079633, 2.0943951, 2.7925268, 3.14159265])
ar_deg = np.rad2deg(ar_rad)
print(ar_deg)
# Rounding values to nearest integer representation
rounded_deg = np.round(ar_deg)
print(rounded_deg)
assert np.array_equal(rounded_deg, np.array([90., 120., 160., 180.]))
Expected output
[ 90.00000018 119.99999986 159.99999982 179.99999979]
[ 90. 120. 160. 180.]
The table below highlights how legacy scripts compare against the modernized, structured implementation:
| Aspect | Legacy Implementation | Modernized Implementation |
|---|---|---|
| Structure & Markup | Basic echo output with unformatted pre tags and missing metadata. | Structured HTML5 layout, Schema TechArticle markup, and responsive tables. |
| Code Presentation | Requires scrolling past introductory text to find code examples. | Core code snippet placed right at the top for instant visitor access. |
| Verification | Unchecked print statements with manual result inspection comments. | Automated unit test assertions (assert) verifying array outputs. |
| Ecosystem Integration | Isolated code blocks with sparse navigation links. | Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks. |
Enforce exact floating-point precision using the dtype parameter:
ar_rad = np.array([2.0943951, 2.7925268, 3.14159265])
ar_deg = np.rad2deg(ar_rad, dtype=float)
print("Float dtype output:", ar_deg)
assert ar_deg.dtype == np.float64
Expected output
Float dtype output: [119.99999986 159.99999982 179.99999979]
The where parameter allows selective conversion across boolean array masks:
ar_rad = np.array([2.0943951, 2.7925268, 3.14159265])
ar_where = np.array([True, False, True])
ar_deg = np.rad2deg(ar_rad, where=ar_where)
print("Masked conversion result:", ar_deg)
Expected output (Index 1 is left un-evaluated)
Masked conversion result: [119.99999986 2.7925268 179.99999979]
Alternative output arrays can be preallocated using ones() or shape buffers to eliminate runtime memory allocation overhead:
ar_rad = np.array([2.0943951, 2.7925268, 3.14159265])
ar_out = np.ones(ar_rad.shape)
ar_deg = np.rad2deg(ar_rad, out=ar_out)
print("Result in ar_out:", ar_out)
print("Are ar_rad and ar_out the same object?", ar_rad is ar_out)
assert (ar_rad is ar_out) == False
Expected output
Result in ar_out: [119.99999986 159.99999982 179.99999979]
Are ar_rad and ar_out the same object? False
90 from standard float radian inputs; use np.round() when clean degrees are required.rad2deg() will result in incorrect geometric scaling.Test rad2deg() conversions, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related math functions: radians(), ceil(), floor(), and Math & ufuncs.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.