Files
astro/tools/math.go
T

73 lines
1.9 KiB
Go
Raw Permalink Normal View History

2019-10-24 10:44:21 +08:00
package tools
import (
"math"
)
// Sin 正弦,角度参数单位为度 / sine; the angle is in degrees.
2019-10-24 10:44:21 +08:00
func Sin(x float64) float64 {
return (math.Sin(x * math.Pi / 180.00000))
}
// Cos 余弦,角度参数单位为度 / cosine; the angle is in degrees.
2019-10-24 10:44:21 +08:00
func Cos(x float64) float64 {
return (math.Cos(x * math.Pi / 180.00000))
}
// Tan 正切,角度参数单位为度 / tangent; the angle is in degrees.
2019-10-24 10:44:21 +08:00
func Tan(x float64) float64 {
return (math.Tan(x * math.Pi / 180.00000))
}
// ArcSin 反正弦,结果单位为度 / arc sine; the result is in degrees.
2019-10-24 10:44:21 +08:00
func ArcSin(x float64) float64 {
return (math.Asin(x) / math.Pi * 180.00000)
}
// ArcCos 反余弦,结果单位为度 / arc cosine; the result is in degrees.
2019-10-24 10:44:21 +08:00
func ArcCos(x float64) float64 {
return (math.Acos(x) / math.Pi * 180.00000)
}
// ArcTan 反正切,结果单位为度 / arc tangent; the result is in degrees.
2019-10-24 10:44:21 +08:00
func ArcTan(x float64) float64 {
return (math.Atan(x) / math.Pi * 180.00000)
}
// ArcTan2 两变量反正切,结果单位为度并归一到 [0,360) / two-argument arc tangent; the result is in degrees and normalized to [0,360).
2025-09-18 13:16:04 +08:00
func ArcTan2(y, x float64) float64 {
angle := math.Atan2(y, x) * 180.0 / math.Pi
if angle < 0 {
angle += 360.0
}
return angle
}
// FloatRound 四舍五入到 n 位小数,实现为 floor(f*10^n+0.5) / round to n decimal places via floor(f*10^n+0.5).
2019-10-24 10:44:21 +08:00
func FloatRound(f float64, n int) float64 {
p := math.Pow10(n)
return math.Floor(f*p+0.5) / p
2019-10-24 10:44:21 +08:00
}
// Limit360 把角度归一到 [0,360) / normalize an angle into [0,360).
2019-10-24 10:44:21 +08:00
func Limit360(x float64) float64 {
2025-01-31 15:35:36 +08:00
x = math.Mod(x, 360)
if x < 0 {
2019-10-24 10:44:21 +08:00
x += 360
}
return x
}
// FR 四舍五入到 14 位小数 / round to 14 decimal places.
2019-10-24 10:44:21 +08:00
func FR(f float64) float64 {
return FloatRound(f, 14)
}
// Abs 取 int 的绝对值,只接受 int,与 math.Abs 同名且不支持浮点 / absolute value of an int; same name as math.Abs but accepts only int.
2019-10-24 10:44:21 +08:00
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}