asin.go 884 Bytes
Newer Older
Ken Thompson's avatar
Ken Thompson committed
1 2 3 4
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5 6 7 8
package math

import	math "atan"
import	math "sqrt"
Ken Thompson's avatar
Ken Thompson committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

export	asin, acos

/*
 * asin(arg) and acos(arg) return the arcsin, arccos,
 * respectively of their arguments.
 *
 * Arctan is called after appropriate range reduction.
 */

const
(
	pio2	= .15707963267948966192313216e1;
)

func
asin(arg double)double
{
	var temp, x double;
	var sign bool;

	sign = false;
	x = arg;
	if x < 0 {
		x = -x;
		sign = true;
	}
	if arg > 1 {
37
		panic "return sys.NaN()";
Ken Thompson's avatar
Ken Thompson committed
38 39
	}

40
	temp = sqrt(1 - x*x);
Ken Thompson's avatar
Ken Thompson committed
41
	if x > 0.7 {
42
		temp = pio2 - atan(temp/x);
Ken Thompson's avatar
Ken Thompson committed
43
	} else {
44
		temp = atan(x/temp);
Ken Thompson's avatar
Ken Thompson committed
45 46 47 48 49 50 51 52 53 54 55 56
	}

	if sign {
		temp = -temp;
	}
	return temp;
}

func
acos(arg double)double
{
	if(arg > 1 || arg < -1) {
57
		panic "return sys.NaN()";
Ken Thompson's avatar
Ken Thompson committed
58 59 60
	}
	return pio2 - asin(arg);
}