Code Example: test_arcos.py

#!/usr/bin/python3
# ===================================================================
# demonstrate numpy.arccos
# -------------------------------------------------------------------
# input : to   arccos is x-coordinate on the unit circle
# output: from arccos is the inverse cos in radians and in the
#         closed interval [-1, 1] radians
#                         [180, 0] degrees
# -------------------------------------------------------------------
# output of program:
#  deg      rad        cos        arccos_rad    arccos_deg
#    0      0.000      1.000      0.000           0.00
#   45      0.785      0.707      0.785          45.00
#   90      1.571      0.000      1.571          90.00
#  135      2.356     -0.707      2.356         135.00
#  180      3.142     -1.000      3.142         180.00
#  225      3.927     -0.707      2.356         135.00
#  270      4.712     -0.000      1.571          90.00
#  315      5.498      0.707      0.785          45.00
#  360      6.283      1.000      0.000           0.00
# ===================================================================

import numpy as np

print('deg      rad        cos        arccos_rad    arccos_deg')

for deg in range(0,361,45):
    fdeg = float(deg)
    rad  = np.deg2rad(fdeg)
    rcos = np.cos(rad)

    r = np.arccos(np.cos(rad))     # arccos returns radians
    d = np.rad2deg(r)              # convert arccos radians
                                   # to degrees

    print(f'{deg:3}     {rad:6.3f}     {rcos:6.3f}     {r:6.3f}' +
          f'         {d:6.2f}')