feat: 完善日月食与月掩几何链路并扩展历法接口
- 新增日月食中心带、偏食带、阴影足迹、等时线、食分线及升落边界计算,支持极区与混合食拓扑 - 新增日食单时刻阴影求解器、站心状态查询、批量采样和 ΔT 覆盖接口 - 重构恒星与行星月掩路径,补充有限盘面接触、站心修正、掩带宽度、极区投影及升落边界 - 扩展 SVG 与 GeoJSON 输出,支持详细面板、全球/极区/地球投影、边界闭合、时间标记和拓扑签名 - 扩展日月食候选搜索、局地搜索、沙罗序列预计算与范围外推,补充系列锚点和成员一致性校验 - 补齐古历纪年、儒略历独有闰日、多公历候选、历法改革跨日及精确日期运算接口 - 优化 ΔT、章动、恒星时、月球地平线、事件根搜索和本地星历缓存,降低重复计算开销并提升边界稳定
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
// Package tools 以度为角度单位的三角函数、角度格式化和取整辅助。
|
||||
// Package tools provides degree-based trigonometry, angle formatting and rounding helpers.
|
||||
package tools
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Format 把角度格式化为度分秒;typed 为 0 时输出度分秒,非 0 时输出时分秒 / format an angle as degrees-minutes-seconds, or hours-minutes-seconds when typed is non-zero.
|
||||
func Format(val float64, typed uint8) string {
|
||||
belowZero := false
|
||||
if val < 0 {
|
||||
|
||||
+11
-1
@@ -4,31 +4,37 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Sin 正弦,角度参数单位为度 / sine; the angle is in degrees.
|
||||
func Sin(x float64) float64 {
|
||||
return (math.Sin(x * math.Pi / 180.00000))
|
||||
}
|
||||
|
||||
// Cos 余弦,角度参数单位为度 / cosine; the angle is in degrees.
|
||||
func Cos(x float64) float64 {
|
||||
return (math.Cos(x * math.Pi / 180.00000))
|
||||
}
|
||||
|
||||
// Tan 正切,角度参数单位为度 / tangent; the angle is in degrees.
|
||||
func Tan(x float64) float64 {
|
||||
return (math.Tan(x * math.Pi / 180.00000))
|
||||
}
|
||||
|
||||
// ArcSin 反正弦,结果单位为度 / arc sine; the result is in degrees.
|
||||
func ArcSin(x float64) float64 {
|
||||
return (math.Asin(x) / math.Pi * 180.00000)
|
||||
}
|
||||
|
||||
// ArcCos 反余弦,结果单位为度 / arc cosine; the result is in degrees.
|
||||
func ArcCos(x float64) float64 {
|
||||
return (math.Acos(x) / math.Pi * 180.00000)
|
||||
}
|
||||
|
||||
// ArcTan 反正切,结果单位为度 / arc tangent; the result is in degrees.
|
||||
func ArcTan(x float64) float64 {
|
||||
return (math.Atan(x) / math.Pi * 180.00000)
|
||||
}
|
||||
|
||||
// ArcTan2 计算两个变量的反正切并转换为角度,处理所有象限
|
||||
// ArcTan2 两变量反正切,结果单位为度并归一到 [0,360) / two-argument arc tangent; the result is in degrees and normalized to [0,360).
|
||||
func ArcTan2(y, x float64) float64 {
|
||||
angle := math.Atan2(y, x) * 180.0 / math.Pi
|
||||
if angle < 0 {
|
||||
@@ -37,11 +43,13 @@ func ArcTan2(y, x float64) float64 {
|
||||
return angle
|
||||
}
|
||||
|
||||
// FloatRound 四舍五入到 n 位小数,实现为 floor(f*10^n+0.5) / round to n decimal places via floor(f*10^n+0.5).
|
||||
func FloatRound(f float64, n int) float64 {
|
||||
p := math.Pow10(n)
|
||||
return math.Floor(f*p+0.5) / p
|
||||
}
|
||||
|
||||
// Limit360 把角度归一到 [0,360) / normalize an angle into [0,360).
|
||||
func Limit360(x float64) float64 {
|
||||
x = math.Mod(x, 360)
|
||||
if x < 0 {
|
||||
@@ -50,10 +58,12 @@ func Limit360(x float64) float64 {
|
||||
return x
|
||||
}
|
||||
|
||||
// FR 四舍五入到 14 位小数 / round to 14 decimal places.
|
||||
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.
|
||||
func Abs(x int) int {
|
||||
if x < 0 {
|
||||
return -x
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
// tools 的对外契约:三角函数以度为角度单位,反三角以度为结果单位,取整为 floor 语义。
|
||||
package tools_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"b612.me/astro/tools"
|
||||
)
|
||||
|
||||
// 度数约定:与 math 的弧度结果必须显著不同,否则说明换算被删掉了。
|
||||
func TestTrigTakesDegrees(t *testing.T) {
|
||||
if math.Abs(tools.Sin(1)-0.01745240643728351) > 1e-15 {
|
||||
t.Fatalf("Sin(1) 应为 sin 1° = 0.01745240643728351,得到 %v", tools.Sin(1))
|
||||
}
|
||||
if math.Abs(tools.Sin(1)-math.Sin(1)) < 0.8 {
|
||||
t.Fatal("Sin 的入参似乎被当成弧度")
|
||||
}
|
||||
if math.Abs(tools.ArcSin(0.5)-30) > 1e-12 {
|
||||
t.Fatalf("ArcSin(0.5) 应为 30 度,得到 %v", tools.ArcSin(0.5))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrigExactAngles(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
got float64
|
||||
want float64
|
||||
}{
|
||||
{"Sin(0)", tools.Sin(0), 0},
|
||||
{"Sin(30)", tools.Sin(30), 0.5},
|
||||
{"Sin(90)", tools.Sin(90), 1},
|
||||
{"Sin(180)", tools.Sin(180), 0},
|
||||
{"Sin(270)", tools.Sin(270), -1},
|
||||
{"Cos(0)", tools.Cos(0), 1},
|
||||
{"Cos(60)", tools.Cos(60), 0.5},
|
||||
{"Cos(90)", tools.Cos(90), 0},
|
||||
{"Cos(180)", tools.Cos(180), -1},
|
||||
{"Tan(0)", tools.Tan(0), 0},
|
||||
{"Tan(45)", tools.Tan(45), 1},
|
||||
{"Tan(135)", tools.Tan(135), -1},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if math.Abs(tc.got-tc.want) > 1e-14 {
|
||||
t.Fatalf("%s = %v,期望 %v", tc.name, tc.got, tc.want)
|
||||
}
|
||||
}
|
||||
if math.Abs(tools.Tan(90)) < 1e15 || math.IsInf(tools.Tan(90), 0) {
|
||||
t.Fatalf("Tan(90) 应为极大有限值,得到 %v", tools.Tan(90))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInverseTrigReturnsDegrees(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
got float64
|
||||
want float64
|
||||
}{
|
||||
{"ArcSin(-1)", tools.ArcSin(-1), -90},
|
||||
{"ArcSin(0)", tools.ArcSin(0), 0},
|
||||
{"ArcSin(1)", tools.ArcSin(1), 90},
|
||||
{"ArcCos(-1)", tools.ArcCos(-1), 180},
|
||||
{"ArcCos(0)", tools.ArcCos(0), 90},
|
||||
{"ArcCos(0.5)", tools.ArcCos(0.5), 60},
|
||||
{"ArcCos(1)", tools.ArcCos(1), 0},
|
||||
{"ArcTan(-1)", tools.ArcTan(-1), -45},
|
||||
{"ArcTan(0)", tools.ArcTan(0), 0},
|
||||
{"ArcTan(1)", tools.ArcTan(1), 45},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if math.Abs(tc.got-tc.want) > 1e-12 {
|
||||
t.Fatalf("%s = %v,期望 %v", tc.name, tc.got, tc.want)
|
||||
}
|
||||
}
|
||||
for _, deg := range []float64{-89.5, -45, -0.25, 0, 30, 44.9, 89.5} {
|
||||
if got := tools.ArcSin(tools.Sin(deg)); math.Abs(got-deg) > 1e-11 {
|
||||
t.Fatalf("ArcSin(Sin(%v)) = %v", deg, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestArcTan2Quadrants(t *testing.T) {
|
||||
cases := []struct {
|
||||
y, x float64
|
||||
want float64
|
||||
}{
|
||||
{0, 1, 0},
|
||||
{1, 1, 45},
|
||||
{1, 0, 90},
|
||||
{1, -1, 135},
|
||||
{0, -1, 180},
|
||||
{-1, -1, 225},
|
||||
{-1, 0, 270},
|
||||
{-1, 1, 315},
|
||||
{-0.0, -1, 180},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := tools.ArcTan2(tc.y, tc.x); math.Abs(got-tc.want) > 1e-12 {
|
||||
t.Fatalf("ArcTan2(%v, %v) = %v,期望 %v", tc.y, tc.x, got, tc.want)
|
||||
}
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := tools.ArcTan2(tc.y, tc.x)
|
||||
if got < 0 || got >= 360 {
|
||||
t.Fatalf("ArcTan2(%v, %v) = %v 未落在 [0,360)", tc.y, tc.x, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimit360(t *testing.T) {
|
||||
cases := []struct {
|
||||
in float64
|
||||
want float64
|
||||
}{
|
||||
{0, 0},
|
||||
{359.5, 359.5},
|
||||
{360, 0},
|
||||
{720, 0},
|
||||
{-1, 359},
|
||||
{-360, 0},
|
||||
{-720.5, 359.5},
|
||||
{1e9, 280},
|
||||
{-1e9, 80},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := tools.Limit360(tc.in); math.Abs(got-tc.want) > 1e-9 {
|
||||
t.Fatalf("Limit360(%v) = %v,期望 %v", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
if !math.IsNaN(tools.Limit360(math.NaN())) {
|
||||
t.Fatal("Limit360(NaN) 应为 NaN")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatRoundFloorSemantics(t *testing.T) {
|
||||
cases := []struct {
|
||||
f float64
|
||||
n int
|
||||
want float64
|
||||
}{
|
||||
{0.5, 0, 1},
|
||||
{1.5, 0, 2},
|
||||
{2.5, 0, 3},
|
||||
{-0.5, 0, 0},
|
||||
{-1.5, 0, -1},
|
||||
{-2.5, 0, -2},
|
||||
{1.25, 1, 1.3},
|
||||
{-1.25, 1, -1.2},
|
||||
{3.14159, 2, 3.14},
|
||||
{2.71828, 3, 2.718},
|
||||
{1, 5, 1},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := tools.FloatRound(tc.f, tc.n); got != tc.want {
|
||||
t.Fatalf("FloatRound(%v, %d) = %v,期望 %v", tc.f, tc.n, got, tc.want)
|
||||
}
|
||||
}
|
||||
if got := tools.FR(1.0 / 3.0); got != 0.33333333333333 {
|
||||
t.Fatalf("FR(1/3) = %v,期望 14 位", got)
|
||||
}
|
||||
if tools.FR(1.0/3.0) != tools.FloatRound(1.0/3.0, 14) {
|
||||
t.Fatal("FR 应为 FloatRound(f, 14)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbs(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
in int
|
||||
want int
|
||||
}{{0, 0}, {7, 7}, {-7, 7}, {-1, 1}} {
|
||||
if got := tools.Abs(tc.in); got != tc.want {
|
||||
t.Fatalf("Abs(%d) = %d,期望 %d", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatDegreesAndHours(t *testing.T) {
|
||||
cases := []struct {
|
||||
val float64
|
||||
typed uint8
|
||||
want string
|
||||
}{
|
||||
{0, 0, "0\u00b00\u20320.00\u2033"},
|
||||
{12.3456, 0, "12\u00b020\u203244.16\u2033"},
|
||||
{-12.3456, 0, "-12\u00b020\u203244.16\u2033"},
|
||||
{359.999, 0, "359\u00b059\u203256.40\u2033"},
|
||||
{12.3456, 1, "12h20m44.16s"},
|
||||
{23.5, 1, "23h30m0.00s"},
|
||||
{0, 1, "0h0m0.00s"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := tools.Format(tc.val, tc.typed); got != tc.want {
|
||||
t.Fatalf("Format(%v, %d) = %q,期望 %q", tc.val, tc.typed, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user