Math.tan
func Math.tan(x: float64): float64Math.tan returns the tangent of x, in radians.
Details
- The angle is in radians. Multiply degrees by
Math.PI / 180.0first. Math.tannever panics:NaNand both infinities giveNaN, and-0.0gives-0.0.
Basic examples
The tangent of a few angles, in radians:
Math.tan(0.0) // 0.0
Math.tan(1.0) // 1.557407724654902
Math.tan(Math.PI / 3.0) // 1.7320508075688767Convert degrees to radians before the call:
let degrees = 30.0
Math.tan(degrees * Math.PI / 180.0) // 0.5773502691896257Scope
Negative angles give the negated tangent, and the sign of a zero is kept:
Math.tan(-1.0) // -1.557407724654902
Math.tan(-0.0) // -0.0The tangent repeats every π, so adding Math.PI gives the same value within rounding:
Math.tan(1.0 + Math.PI) // 1.5574077246549016NaN and the infinities give NaN:
Math.tan(Math.NaN) // NaN
Math.tan(Math.Infinity) // NaN
Math.tan(-Math.Infinity) // NaNCall it in a pipe, or pass it as a function value:
Math.PI / 3.0 | Math.tan() // 1.7320508075688767
[0.0, 1.0].map(Math.tan) // [0.0, 1.557407724654902]Applications
The height of a tree from the distance to its base and the angle up to its top:
func treeHeight(distance: float64, degrees: float64): float64 {
distance * Math.tan(degrees * Math.PI / 180.0)
}
treeHeight(20.0, 30.0) // 11.547005383792515The grade of a road, in percent, from its angle in degrees:
type Road = { name: string, degrees: float64 }
func grade(road: Road): float64 {
Math.round(1000.0 * Math.tan(road.degrees * Math.PI / 180.0)) / 10.0
}
let roads = [Road{ name: "Main", degrees: 2.0 }, Road{ name: "Hill", degrees: 5.0 }]
for road in roads {
println("${road.name}: ${grade(road)}%")
}Main: 3.5%
Hill: 8.7%Properties and relations
Math.tan is odd: it maps -x to minus its value at x:
for x in [0.25, 1.0, 1.5, 100.0] {
assert(Math.tan(-x) == -Math.tan(x))
}The tangent is the sine over the cosine, within rounding:
for x in [0.1, 1.0, 2.5, 10.0] {
let ratio = Math.sin(x) / Math.cos(x)
assert(Math.abs(Math.tan(x) - ratio) < 1e-12 * Math.abs(ratio))
}Math.atan undoes Math.tan for angles between -Math.PI / 2 and Math.PI / 2, within rounding:
for x in [-1.2, -0.5, 0.0, 0.5, 1.2] {
assert(Math.abs(Math.atan(Math.tan(x)) - x) < 1e-15)
}Possible issues
An angle in degrees is read as radians:
Math.tan(45.0) // 1.6197751905438615
Math.abs(Math.tan(45.0 * Math.PI / 180.0) - 1.0) < 1e-12 // trueMath.PI is not exactly π, so a tangent that is exact in arithmetic comes out a little off, and the last digit can differ between platforms: Math.tan(Math.PI / 4.0) is 1.0 on one machine and 0.9999999999999998 on another. Compare with a tolerance:
Math.abs(Math.tan(Math.PI / 4.0) - 1.0) < 1e-12 // true
Math.tan(Math.PI) // -1.2246467991473515e-16The tangent at a right angle is not an infinity but a large finite number, so a test with Math.isFinite does not catch it:
Math.tan(Math.PI / 2.0) // 16331239353195392.0
Math.isFinite(Math.tan(Math.PI / 2.0)) // trueMath.atan returns an angle between -Math.PI / 2 and Math.PI / 2, so it does not undo Math.tan outside it:
Math.atan(Math.tan(2.0)) // -1.1415926535897931An integer is not a float64. Write a float literal, or convert:
let n = 1
Math.tan(n) // error: cannot use int where float64 is expectedlet n = 1
Math.tan(float64(n)) // 1.557407724654902A float64 has no namespace of its own, so x.tan() does not resolve. Call Math.tan(x) or pipe the value:
let x = 1.0
x.tan() // error: float64 has no field or function tanSee also
Math.sin · Math.cos · Math.atan · Math.atan2 · Math.PI · float64
