From d308371beaf0c34d6cee305ebb62341ff296e173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Hannequin?= Date: Mon, 19 Feb 2024 21:36:51 +0100 Subject: [PATCH] Add constants for Angle and increase precision --- lib/astronoby/angle.rb | 32 +++++++++++++++++---------- spec/astronoby/mean_obliquity_spec.rb | 4 ++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/astronoby/angle.rb b/lib/astronoby/angle.rb index 8489d5e..0248e1b 100644 --- a/lib/astronoby/angle.rb +++ b/lib/astronoby/angle.rb @@ -6,6 +6,14 @@ module Astronoby class Angle PRECISION = 14 PI = BigMath.PI(PRECISION) + PI_IN_DEGREES = BigDecimal("180") + + RADIAN_PER_HOUR = PI / BigDecimal("12") + MINUTES_PER_DEGREE = BigDecimal("60") + MINUTES_PER_HOUR = BigDecimal("60") + SECONDS_PER_MINUTE = BigDecimal("60") + SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE + FORMATS = %i[dms hms].freeze class << self @@ -18,23 +26,23 @@ def as_radians(radians) end def as_degrees(degrees) - radians = degrees / BigDecimal("180") * PI + radians = degrees / PI_IN_DEGREES * PI new(radians) end def as_hours(hours) - radians = hours * (PI / BigDecimal("12")) + radians = hours * RADIAN_PER_HOUR new(radians) end def as_hms(hour, minute, second) - hours = hour + minute / 60.0 + second / 3600.0 + hours = hour + minute / MINUTES_PER_HOUR + second / SECONDS_PER_HOUR as_hours(hours) end def as_dms(degree, minute, second) sign = degree.negative? ? -1 : 1 - degrees = degree.abs + minute / 60.0 + second / 3600.0 + degrees = degree.abs + minute / MINUTES_PER_HOUR + second / SECONDS_PER_HOUR as_degrees(sign * degrees) end end @@ -44,11 +52,11 @@ def radians end def degrees - @angle * BigDecimal("180") / PI + @angle * PI_IN_DEGREES / PI end def hours - @angle / (PI / BigDecimal("12")) + @angle / RADIAN_PER_HOUR end def initialize(angle) @@ -74,12 +82,12 @@ def to_dms(deg) sign = deg.negative? ? "-" : "+" absolute_degrees = deg.abs degrees = absolute_degrees.floor - decimal_minutes = BigDecimal("60") * (absolute_degrees - degrees) + decimal_minutes = MINUTES_PER_DEGREE * (absolute_degrees - degrees) absolute_decimal_minutes = ( - BigDecimal("60") * (absolute_degrees - degrees) + MINUTES_PER_DEGREE * (absolute_degrees - degrees) ).abs minutes = decimal_minutes.floor - seconds = BigDecimal("60") * ( + seconds = SECONDS_PER_MINUTE * ( absolute_decimal_minutes - absolute_decimal_minutes.floor ) @@ -89,12 +97,12 @@ def to_dms(deg) def to_hms(hrs) absolute_hours = hrs.abs hours = absolute_hours.floor - decimal_minutes = BigDecimal("60") * (absolute_hours - hours) + decimal_minutes = MINUTES_PER_HOUR * (absolute_hours - hours) absolute_decimal_minutes = ( - BigDecimal("60") * (absolute_hours - hours) + MINUTES_PER_HOUR * (absolute_hours - hours) ).abs minutes = decimal_minutes.floor - seconds = BigDecimal("60") * ( + seconds = SECONDS_PER_MINUTE * ( absolute_decimal_minutes - absolute_decimal_minutes.floor ) diff --git a/spec/astronoby/mean_obliquity_spec.rb b/spec/astronoby/mean_obliquity_spec.rb index 1b8c8aa..6c10170 100644 --- a/spec/astronoby/mean_obliquity_spec.rb +++ b/spec/astronoby/mean_obliquity_spec.rb @@ -11,13 +11,13 @@ it "returns the obliquity angle for standard epoch" do obliquity = described_class.for_epoch(Astronoby::Epoch::J2000).value - expect(obliquity.degrees).to eq(23.43929166666666) + expect(obliquity.degrees.to_f).to eq(23.439291666666666) end it "returns the obliquity angle for epoch 1950" do obliquity = described_class.for_epoch(Astronoby::Epoch::J1950).value - expect(obliquity.degrees.to_f).to eq 23.445793854513884 + expect(obliquity.degrees.to_f).to eq 23.445793854513887 end # Source: