asin.mx raw

   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  //
  18  //	Asin(±0) = ±0
  19  //	Asin(x) = NaN if x < -1 or x > 1
  20  func Asin(x float64) float64 {
  21  	if haveArchAsin {
  22  		return archAsin(x)
  23  	}
  24  	return asin(x)
  25  }
  26  
  27  func asin(x float64) float64 {
  28  	if x == 0 {
  29  		return x // special case
  30  	}
  31  	sign := false
  32  	if x < 0 {
  33  		x = -x
  34  		sign = true
  35  	}
  36  	if x > 1 {
  37  		return NaN() // special case
  38  	}
  39  
  40  	temp := Sqrt(1 - x*x)
  41  	if x > 0.7 {
  42  		temp = Pi/2 - satan(temp/x)
  43  	} else {
  44  		temp = satan(x / temp)
  45  	}
  46  
  47  	if sign {
  48  		temp = -temp
  49  	}
  50  	return temp
  51  }
  52  
  53  // Acos returns the arccosine, in radians, of x.
  54  //
  55  // Special case is:
  56  //
  57  //	Acos(x) = NaN if x < -1 or x > 1
  58  func Acos(x float64) float64 {
  59  	if haveArchAcos {
  60  		return archAcos(x)
  61  	}
  62  	return acos(x)
  63  }
  64  
  65  func acos(x float64) float64 {
  66  	return Pi/2 - Asin(x)
  67  }
  68