Black Lives Matter. Support the Equal Justice Initiative.

Source file src/math/asin.go

Documentation: math

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  /*
     8  	Floating-point arcsine and arccosine.
     9  
    10  	They are implemented by computing the arctangent
    11  	after appropriate range reduction.
    12  */
    13  
    14  // Asin returns the arcsine, in radians, of x.
    15  //
    16  // Special cases are:
    17  //	Asin(±0) = ±0
    18  //	Asin(x) = NaN if x < -1 or x > 1
    19  func Asin(x float64) float64 {
    20  	if haveArchAsin {
    21  		return archAsin(x)
    22  	}
    23  	return asin(x)
    24  }
    25  
    26  func asin(x float64) float64 {
    27  	if x == 0 {
    28  		return x // special case
    29  	}
    30  	sign := false
    31  	if x < 0 {
    32  		x = -x
    33  		sign = true
    34  	}
    35  	if x > 1 {
    36  		return NaN() // special case
    37  	}
    38  
    39  	temp := Sqrt(1 - x*x)
    40  	if x > 0.7 {
    41  		temp = Pi/2 - satan(temp/x)
    42  	} else {
    43  		temp = satan(x / temp)
    44  	}
    45  
    46  	if sign {
    47  		temp = -temp
    48  	}
    49  	return temp
    50  }
    51  
    52  // Acos returns the arccosine, in radians, of x.
    53  //
    54  // Special case is:
    55  //	Acos(x) = NaN if x < -1 or x > 1
    56  func Acos(x float64) float64 {
    57  	if haveArchAcos {
    58  		return archAcos(x)
    59  	}
    60  	return acos(x)
    61  }
    62  
    63  func acos(x float64) float64 {
    64  	return Pi/2 - Asin(x)
    65  }
    66  

View as plain text