Skip to content

Commit

Permalink
Add constants for Angle and increase precision
Browse files Browse the repository at this point in the history
  • Loading branch information
rhannequin committed Feb 19, 2024
1 parent 9111314 commit d308371
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
32 changes: 20 additions & 12 deletions lib/astronoby/angle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
)

Expand All @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions spec/astronoby/mean_obliquity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d308371

Please sign in to comment.