NumPy rad2deg(): Convert Angles from Radians to Degrees

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])
rad2deg() is a universal function (ufunc) that converts angle measurements from radians to degrees element-wise.

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.

ParameterDescription
xInput array or angle values in radians
outOptional, ndarray to store the result; must match expected output shape
whereOptional, boolean array mask; elements where True are evaluated, False left untouched
dtypeOptional, data-type override for output

Basic rad2deg() Examples 🔝

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.]

Legacy vs Modernized Code Comparison 🔝

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.

Specifying dtype 🔝

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]

Using where Mask 🔝

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]

Using out Buffer 🔝

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

Common Pitfalls 🔝

  • Floating-Point Precision Offsets: Do not expect exact integer outputs like 90 from standard float radian inputs; use np.round() when clean degrees are required.
  • Radians vs Degrees Confusion: Passing degree values into rad2deg() will result in incorrect geometric scaling.

Practice in Google Colab 🔝

Test rad2deg() conversions, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related math functions: radians(), ceil(), floor(), and Math & ufuncs.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer