liveBook · Manning

Cosine function

As explained in Julia as a Second Language

cos is a Julia function that computes the cosine of an angle or numeric value.

The cosine function computes a trigonometric value from an angle. It turns measurements such as a lighthouse sighting into a value that can support navigation calculations, replacing printed tables with an executable calculation. section 3.1

Julia’s cosine workflow becomes safer when angle units are represented explicitly:

  • Represent angles with Radian or DMS types instead of leaving the unit implicit. section 9.3
  • Convert DMS values to radians before applying the underlying cosine calculation. section 9.3.5
  • Define cos methods for both Radian and DMS values so callers cannot silently confuse degrees and radians. section 9.3.5

Cosine with explicit angle units

A unit-aware implementation delegates radians to Julia’s standard calculation and converts DMS values first: `cos(rad::Radian) = Base.cos(rad.radians)` and `cos(dms::DMS) = cos(Radian(dms))`. With these methods, cosine can accept `Radian(π/2)` or a DMS angle while keeping the unit visible. section 9.3.5

Figure 3.1 A captain on a ship could use a sextant to measure angle θ between the horizon and top of a lighthouse.
03-01

Use the lighthouse-and-sextant figure to connect cosine calculations with the practical problem of locating a ship from an observed angle. section 3.1

How Julia as a Second Language covers it

How deep it goes
Explains cosine through navigation, then implements unit-aware methods and conversions without developing a cosine approximation.
Examples
Uses lighthouse navigation, sextant angle measurement, Radian and DMS types, and unit-aware cos calls.
What it assumes
Assumes basic triangle terminology and angle measurement while introducing radians, DMS values, and conversions.
What you can do after
Apply it via Julia’s cos methods to calculate values while making radians or degrees explicit.
How it connects
Connects cosine with trigonometric functions, angle units, radians, degrees, and type conversion.

Also explained in

Compare how 3 books explain cosine function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.