Math.asin
func Math.asin(x: float64): float64Math.asin returns the arcsine of x, in radians.
Details
- The result is an angle in radians, from
-Math.PI / 2.0toMath.PI / 2.0. - An
xoutside-1.0 ..= 1.0, an infinity, orNaNgivesNaN.Math.asinnever panics.
Basic examples
Math.asin(0.5) // 0.5235987755982989
Math.asin(1.0) // 1.5707963267948966
Math.asin(-1.0) // -1.5707963267948966The arcsine of 0.5 is 30 degrees:
Math.asin(0.5) * 180.0 / Math.PI // 30.000000000000004Scope
Zero keeps its sign:
Math.asin(0.0) // 0.0
Math.asin(-0.0) // -0.0A value outside the domain gives NaN, as does NaN itself:
Math.asin(2.0) // NaN
Math.asin(1.0000001) // NaN
Math.asin(Math.Infinity) // NaN
Math.asin(Math.NaN) // NaNPass it as a function value, or call it through a pipe:
[0.0, 0.5, 1.0].map(Math.asin) // [0.0, 0.5235987755982989, 1.5707963267948966]
0.5 | Math.asin() // 0.5235987755982989Applications
The angle of a ramp from its rise and its length, in degrees:
func slope(rise: float64, length: float64): float64 {
Math.asin(rise / length) * 180.0 / Math.PI
}
String.fromFloat(slope(1.0, 2.0), 1) // "30.0"
String.fromFloat(slope(1.0, 12.0), 2) // "4.78"Snell's law gives the angle of a ray of light after it crosses into another medium. When the sine comes out above 1, there is no refracted ray, and NaN signals total internal reflection:
func refract(angle: float64, from: float64, to: float64): float64 | null {
let r = Math.asin(from / to * Math.sin(angle))
if Math.isNaN(r) { null } else { r }
}
refract(0.5, 1.0, 1.33) // 0.3687720730730495
refract(1.2, 1.33, 1.0) // nullProperties and relations
Math.asin is odd: it maps -x to the negation of Math.asin(x):
for x in [0.0, 0.3, 0.6, 1.0] {
assert(Math.asin(-x) == -Math.asin(x))
}Math.sin undoes Math.asin up to rounding, and Math.asin(x) + Math.acos(x) is a right angle. Compare floats with a tolerance:
let eps = 1e-15
for x in [-1.0, -0.6, 0.0, 0.25, 0.9] {
assert(Math.abs(Math.sin(Math.asin(x)) - x) < eps)
assert(Math.abs(Math.asin(x) + Math.acos(x) - Math.PI / 2.0) < eps)
}Possible issues
Math.asin undoes Math.sin only for angles from -Math.PI / 2.0 to Math.PI / 2.0. For any other angle it returns the angle in that range with the same sine, up to rounding:
Math.asin(Math.sin(2.0)) // 1.141592653589793
Math.PI - 2.0 // 1.1415926535897931A sine computed with rounding can land just past 1. Clamp it first, or the result is NaN:
let s = 0.1 * 3.0 / 0.3
s // 1.0000000000000002
Math.asin(s) // NaN
Math.asin(Math.clamp(s, -1.0, 1.0)) // 1.5707963267948966The argument is a float64. An integer literal is an int, so convert it or write 1.0:
Math.asin(1) // error: cannot use int where float64 is expectedSee also
Math.acos, Math.atan, Math.atan2, Math.sin, Math.isNaN, Math.clamp
