diff --git a/README.md b/README.md index 94db64b..03f0596 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ horizontal_coordinates = sun.horizontal_coordinates( longitude: longitude ) -horizontal_coordinates.altitude.value.to_f -# => 27.50236513017543 +horizontal_coordinates.altitude.degrees.to_f +# => 27.502365130176567 -horizontal_coordinates.altitude.to_dms.format +horizontal_coordinates.altitude.str(:dms) # => "+27° 30′ 8.5144″" ``` diff --git a/lib/astronoby.rb b/lib/astronoby.rb index ef078d6..84695f4 100644 --- a/lib/astronoby.rb +++ b/lib/astronoby.rb @@ -1,11 +1,8 @@ # frozen_string_literal: true require "astronoby/angle" -require "astronoby/angles/degree" require "astronoby/angles/dms" require "astronoby/angles/hms" -require "astronoby/angles/hour" -require "astronoby/angles/radian" require "astronoby/epoch" require "astronoby/body" require "astronoby/bodies/sun" diff --git a/lib/astronoby/aberration.rb b/lib/astronoby/aberration.rb index fcc3b75..43dd4a5 100644 --- a/lib/astronoby/aberration.rb +++ b/lib/astronoby/aberration.rb @@ -22,27 +22,22 @@ def apply delta_longitude = Astronoby::Angle.as_degrees( -20.5 * Math.cos( - @sun_longitude.to_radians.value - @coordinates.longitude.to_radians.value - ) / Math.cos(@coordinates.latitude.to_radians.value) / 3600 + @sun_longitude.radians - @coordinates.longitude.radians + ) / Math.cos(@coordinates.latitude.radians) / 3600 ) delta_latitude = Astronoby::Angle.as_degrees( -20.5 * - Math.sin( - @sun_longitude.to_radians.value - - @coordinates.longitude.to_radians.value - ) * - Math.sin(@coordinates.latitude.to_radians.value) / 3600 + Math.sin(@sun_longitude.radians - @coordinates.longitude.radians) * + Math.sin(@coordinates.latitude.radians) / 3600 ) Astronoby::Coordinates::Ecliptic.new( latitude: Astronoby::Angle.as_degrees( - @coordinates.latitude.to_degrees.value + - delta_latitude.value + @coordinates.latitude.degrees + delta_latitude.degrees ), longitude: Astronoby::Angle.as_degrees( - @coordinates.longitude.to_degrees.value + - delta_longitude.value + @coordinates.longitude.degrees + delta_longitude.degrees ) ) end diff --git a/lib/astronoby/angle.rb b/lib/astronoby/angle.rb index c71107d..3945cfb 100644 --- a/lib/astronoby/angle.rb +++ b/lib/astronoby/angle.rb @@ -4,57 +4,97 @@ module Astronoby class Angle - UNITS = [ - DEGREES = :degrees, - HOURS = :hours, - RADIANS = :radians - ].freeze - - UNIT_CLASS_NAMES = { - DEGREES => "Astronoby::Degree", - HOURS => "Astronoby::Hour", - RADIANS => "Astronoby::Radian" - } - PRECISION = 14 - PI = BigMath.PI(10) + PI = BigMath.PI(PRECISION) + FORMATS = %i[dms hms].freeze class << self - UNIT_CLASS_NAMES.each do |unit, class_name| - define_method("as_#{unit}") do |angle| - Kernel.const_get(class_name).new(angle) - end + def as_radians(radians) + new(radians) + end + + def as_degrees(degrees) + radians = degrees / BigDecimal("180") * PI + new(radians) + end + + def as_hours(hours) + radians = hours * (PI / BigDecimal("12")) + new(radians) end def as_hms(hour, minute, second) - angle = hour + minute / 60.0 + second / 3600.0 - Kernel.const_get(UNIT_CLASS_NAMES[HOURS]).new(angle) + hours = hour + minute / 60.0 + second / 3600.0 + as_hours(hours) end def as_dms(degree, minute, second) sign = degree.negative? ? -1 : 1 - angle = degree.abs + minute / 60.0 + second / 3600.0 - Kernel.const_get(UNIT_CLASS_NAMES[DEGREES]).new(sign * angle) + degrees = degree.abs + minute / 60.0 + second / 3600.0 + as_degrees(sign * degrees) end end - UNITS.each do |unit| - define_method("to_#{unit}") do - raise NotImplementedError, "#{self.class} must implement #to_#{unit} method." + def radians + @angle + end + + def degrees + @angle * BigDecimal("180") / PI + end + + def hours + @angle / (PI / BigDecimal("12")) + end + + def initialize(angle) + @angle = if angle.is_a?(Integer) || angle.is_a?(BigDecimal) + BigDecimal(angle) + else + BigDecimal(angle, PRECISION) end end - def initialize(angle, unit:) - @angle = BigDecimal(angle, PRECISION).ceil(PRECISION) - @unit = unit + def str(format) + case format + when :dms then to_dms(degrees).format + when :hms then to_hms(hours).format + else + raise UnsupportedFormatError.new( + "Expected a format between #{FORMATS.join(", ")}, got #{format}" + ) + end end - def value - @angle + def to_dms(deg) + sign = deg.negative? ? "-" : "+" + absolute_degrees = deg.abs + degrees = absolute_degrees.floor + decimal_minutes = BigDecimal("60") * (absolute_degrees - degrees) + absolute_decimal_minutes = ( + BigDecimal("60") * (absolute_degrees - degrees) + ).abs + minutes = decimal_minutes.floor + seconds = BigDecimal("60") * ( + absolute_decimal_minutes - absolute_decimal_minutes.floor + ) + + Dms.new(sign, degrees, minutes, seconds.to_f.floor(4)) end - def ==(other) - value == other.value && self.class == other.class + def to_hms(hrs) + absolute_hours = hrs.abs + hours = absolute_hours.floor + decimal_minutes = BigDecimal("60") * (absolute_hours - hours) + absolute_decimal_minutes = ( + BigDecimal("60") * (absolute_hours - hours) + ).abs + minutes = decimal_minutes.floor + seconds = BigDecimal("60") * ( + absolute_decimal_minutes - absolute_decimal_minutes.floor + ) + + Hms.new(hours, minutes, seconds.to_f.floor(4)) end end end diff --git a/lib/astronoby/angles/degree.rb b/lib/astronoby/angles/degree.rb deleted file mode 100644 index a12ceeb..0000000 --- a/lib/astronoby/angles/degree.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -module Astronoby - class Degree < Angle - def initialize(angle) - super(angle, unit: DEGREES) - end - - def to_degrees - self - end - - def to_hours - self.class.as_hours(@angle / BigDecimal("15")) - end - - def to_radians - self.class.as_radians(@angle / BigDecimal("180") * PI) - end - - def to_dms - sign = @angle.negative? ? "-" : "+" - absolute_degrees = @angle.abs - degrees = absolute_degrees.floor - decimal_minutes = BigDecimal("60") * (absolute_degrees - degrees) - absolute_decimal_minutes = ( - BigDecimal("60") * (absolute_degrees - degrees) - ).abs - minutes = decimal_minutes.floor - seconds = BigDecimal("60") * ( - absolute_decimal_minutes - absolute_decimal_minutes.floor - ) - - Dms.new(sign, degrees, minutes, seconds.to_f.floor(4)) - end - - def to_hms - absolute_hours = @angle.abs - hours = absolute_hours.floor - decimal_minutes = BigDecimal("60") * (absolute_hours - hours) - absolute_decimal_minutes = ( - BigDecimal("60") * (absolute_hours - hours) - ).abs - minutes = decimal_minutes.floor - seconds = BigDecimal("60") * ( - absolute_decimal_minutes - absolute_decimal_minutes.floor - ) - - Hms.new(hours, minutes, seconds.to_f.floor(4)) - end - end -end diff --git a/lib/astronoby/angles/hour.rb b/lib/astronoby/angles/hour.rb deleted file mode 100644 index f166785..0000000 --- a/lib/astronoby/angles/hour.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -module Astronoby - class Hour < Angle - def initialize(angle) - super(angle, unit: HOURS) - end - - def to_degrees - self.class.as_degrees(@angle * BigDecimal("15")) - end - - def to_hours - self - end - - def to_radians - self.class.as_radians(@angle * (PI / BigDecimal("12"))) - end - - def to_hms - absolute_hours = @angle.abs - hours = absolute_hours.floor - decimal_minutes = BigDecimal("60") * (absolute_hours - hours) - absolute_decimal_minutes = ( - BigDecimal("60") * (absolute_hours - hours) - ).abs - minutes = decimal_minutes.floor - seconds = BigDecimal("60") * ( - absolute_decimal_minutes - absolute_decimal_minutes.floor - ) - - Hms.new(hours, minutes, seconds.to_f.floor(4)) - end - end -end diff --git a/lib/astronoby/angles/radian.rb b/lib/astronoby/angles/radian.rb deleted file mode 100644 index 97001e7..0000000 --- a/lib/astronoby/angles/radian.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module Astronoby - class Radian < Angle - def initialize(angle) - super(angle, unit: RADIANS) - end - - def to_degrees - self.class.as_degrees(@angle * BigDecimal("180") / PI) - end - - def to_hours - self.class.as_hours(@angle / (PI / BigDecimal("12"))) - end - - def to_radians - self - end - - def to_dms - to_degrees.to_dms - end - end -end diff --git a/lib/astronoby/bodies/sun.rb b/lib/astronoby/bodies/sun.rb index d95da99..2cf0eb8 100644 --- a/lib/astronoby/bodies/sun.rb +++ b/lib/astronoby/bodies/sun.rb @@ -16,7 +16,7 @@ def ecliptic_coordinates Coordinates::Ecliptic.new( latitude: Angle.as_degrees(0), longitude: Angle.as_degrees( - (true_anomaly.value + longitude_at_perigee.to_degrees.value) % 360 + (true_anomaly.degrees + longitude_at_perigee.degrees) % 360 ) ) end @@ -33,29 +33,24 @@ def horizontal_coordinates(latitude:, longitude:) def mean_anomaly Angle.as_degrees( - ( - longitude_at_base_epoch.to_degrees.value - - longitude_at_perigee.to_degrees.value - ) % 360 + (longitude_at_base_epoch.degrees - longitude_at_perigee.degrees) % 360 ) end def true_anomaly eccentric_anomaly = Astronoby::Util::Astrodynamics.eccentric_anomaly_newton_raphson( mean_anomaly, - orbital_eccentricity.to_degrees.value, + orbital_eccentricity.degrees, 2e-06, 10 ) tan = Math.sqrt( - (1 + orbital_eccentricity.to_degrees.value)./( - 1 - orbital_eccentricity.to_degrees.value - ) - ) * Math.tan(eccentric_anomaly.to_radians.value / 2) + (1 + orbital_eccentricity.degrees) / (1 - orbital_eccentricity.degrees) + ) * Math.tan(eccentric_anomaly.radians / 2) Astronoby::Angle.as_degrees( - (Astronoby::Angle.as_radians(Math.atan(tan)).to_degrees.value * 2) % 360 + (Astronoby::Angle.as_radians(Math.atan(tan)).degrees * 2) % 360 ) end diff --git a/lib/astronoby/body.rb b/lib/astronoby/body.rb index e88c8bd..612d4cd 100644 --- a/lib/astronoby/body.rb +++ b/lib/astronoby/body.rb @@ -16,8 +16,7 @@ def rising_time(latitude:, longitude:, date:) return nil if h2_component.nil? rising_lst = 24 + - @equatorial_coordinates.right_ascension.to_hours.value - - h2_component.to_degrees.value + @equatorial_coordinates.right_ascension.hours - h2_component.degrees rising_lst -= 24 if rising_lst > 24 Astronoby::Util::Time.lst_to_ut( @@ -48,8 +47,7 @@ def setting_time(latitude:, longitude:, date:) h2_component = h2(latitude: latitude) return nil if h2_component.nil? - setting_lst = @equatorial_coordinates.right_ascension.to_hours.value + - h2_component.to_degrees.value + setting_lst = @equatorial_coordinates.right_ascension.hours + h2_component.degrees setting_lst -= 24 if setting_lst > 24 Astronoby::Util::Time.lst_to_ut( @@ -68,14 +66,14 @@ def setting_azimuth(latitude:) rising_az = rising_azimuth(latitude: latitude) return nil if rising_az.nil? - Astronoby::Angle.as_degrees(360 - rising_az.to_degrees.value) + Astronoby::Angle.as_degrees(360 - rising_az.degrees) end private def azimuth_component(latitude:) - Math.sin(@equatorial_coordinates.declination.to_radians.value)./( - Math.cos(latitude.to_radians.value) + Math.sin(@equatorial_coordinates.declination.radians)./( + Math.cos(latitude.radians) ) end @@ -83,8 +81,8 @@ def h2(latitude:) ar = azimuth_component(latitude: latitude) return nil if ar >= 1 - h1 = Math.tan(latitude.to_radians.value) * - Math.tan(@equatorial_coordinates.declination.to_radians.value) + h1 = Math.tan(latitude.radians) * + Math.tan(@equatorial_coordinates.declination.radians) return nil if h1.abs > 1 Astronoby::Angle.as_radians(Math.acos(-h1) / 15.0) diff --git a/lib/astronoby/coordinates/ecliptic.rb b/lib/astronoby/coordinates/ecliptic.rb index 54a56e7..6e69973 100644 --- a/lib/astronoby/coordinates/ecliptic.rb +++ b/lib/astronoby/coordinates/ecliptic.rb @@ -17,9 +17,9 @@ def initialize(latitude:, longitude:) # Chapter: 4 - Orbits and Coordinate Systems def to_equatorial(epoch:) mean_obliquity = Astronoby::MeanObliquity.for_epoch(epoch) - obliquity_in_radians = mean_obliquity.value.to_radians.value - longitude_in_radians = @longitude.to_radians.value - latitude_in_radians = @latitude.to_radians.value + obliquity_in_radians = mean_obliquity.value.radians + longitude_in_radians = @longitude.radians + latitude_in_radians = @latitude.radians y = Astronoby::Angle.as_radians( Math.sin(longitude_in_radians) * Math.cos(obliquity_in_radians) - @@ -29,13 +29,13 @@ def to_equatorial(epoch:) # TODO: investigate, there should be a better and it might not ever # work all the time - right_ascension = if y.value.negative? && x.value.negative? - r = Astronoby::Angle.as_radians(Math.atan(y.value / x.value)) + right_ascension = if y.radians.negative? && x.radians.negative? + r = Astronoby::Angle.as_radians(Math.atan(y.radians / x.radians)) Astronoby::Angle.as_radians( - Astronoby::Util::Trigonometry.adjustement_for_arctangent(y, x, r).value + Astronoby::Util::Trigonometry.adjustement_for_arctangent(y, x, r).radians ) else - Astronoby::Angle.as_radians(Math.atan2(y.value, x.value)) + Astronoby::Angle.as_radians(Math.atan2(y.radians, x.radians)) end declination = Astronoby::Angle.as_radians( @@ -46,8 +46,8 @@ def to_equatorial(epoch:) ) Equatorial.new( - right_ascension: right_ascension.to_hours, - declination: declination.to_degrees, + right_ascension: right_ascension, + declination: declination, epoch: epoch ) end diff --git a/lib/astronoby/coordinates/equatorial.rb b/lib/astronoby/coordinates/equatorial.rb index 80883ab..d21574d 100644 --- a/lib/astronoby/coordinates/equatorial.rb +++ b/lib/astronoby/coordinates/equatorial.rb @@ -23,7 +23,7 @@ def compute_hour_angle(time:, longitude:) longitude: longitude ) - ha = (lst - @right_ascension.to_hours.value) + ha = (lst - @right_ascension.hours) ha += 24 if ha.negative? Astronoby::Angle.as_hours(ha) @@ -32,25 +32,25 @@ def compute_hour_angle(time:, longitude:) def to_horizontal(time:, latitude:, longitude:) ha = @hour_angle || compute_hour_angle(time: time, longitude: longitude) - latitude_radians = latitude.to_radians.value - declination_radians = @declination.to_radians.value + latitude_radians = latitude.radians + declination_radians = @declination.radians t0 = Math.sin(declination_radians) * Math.sin(latitude_radians) + - Math.cos(declination_radians) * Math.cos(latitude_radians) * Math.cos(ha.to_radians.value) + Math.cos(declination_radians) * Math.cos(latitude_radians) * Math.cos(ha.radians) altitude = Astronoby::Angle.as_radians(Math.asin(t0)) t1 = Math.sin(declination_radians) - - Math.sin(latitude_radians) * Math.sin(altitude.to_radians.value) - t2 = t1 / (Math.cos(latitude_radians) * Math.cos(altitude.to_radians.value)) - sin_hour_angle = Math.sin(ha.to_radians.value) + Math.sin(latitude_radians) * Math.sin(altitude.radians) + t2 = t1 / (Math.cos(latitude_radians) * Math.cos(altitude.radians)) + sin_hour_angle = Math.sin(ha.radians) azimuth = Astronoby::Angle.as_radians(Math.acos(t2)) if sin_hour_angle.positive? - azimuth = Astronoby::Angle.as_degrees(BigDecimal("360") - azimuth.to_degrees.value) + azimuth = Astronoby::Angle.as_degrees(BigDecimal("360") - azimuth.degrees) end Horizontal.new( - azimuth: azimuth.to_degrees, - altitude: altitude.to_degrees, + azimuth: azimuth, + altitude: altitude, latitude: latitude, longitude: longitude ) @@ -63,16 +63,16 @@ def to_horizontal(time:, latitude:, longitude:) # Chapter: 4 - Orbits and Coordinate Systems def to_ecliptic(epoch:) mean_obliquity = Astronoby::MeanObliquity.for_epoch(epoch) - obliquity_in_radians = mean_obliquity.value.to_radians.value - right_ascension_in_radians = @right_ascension.to_radians.value - declination_in_radians = @declination.to_radians.value + obliquity_in_radians = mean_obliquity.value.radians + right_ascension_in_radians = @right_ascension.radians + declination_in_radians = @declination.radians y = Astronoby::Angle.as_radians( Math.sin(right_ascension_in_radians) * Math.cos(obliquity_in_radians) + Math.tan(declination_in_radians) * Math.sin(obliquity_in_radians) ) x = Astronoby::Angle.as_radians(Math.cos(right_ascension_in_radians)) - r = Astronoby::Angle.as_radians(Math.atan(y.value / x.value)) + r = Astronoby::Angle.as_radians(Math.atan(y.radians / x.radians)) longitude = Astronoby::Util::Trigonometry.adjustement_for_arctangent(y, x, r) latitude = Astronoby::Angle.as_radians( @@ -83,8 +83,8 @@ def to_ecliptic(epoch:) ) Ecliptic.new( - latitude: latitude.to_degrees, - longitude: longitude.to_degrees + latitude: latitude, + longitude: longitude ) end diff --git a/lib/astronoby/coordinates/horizontal.rb b/lib/astronoby/coordinates/horizontal.rb index be02e11..cee8e93 100644 --- a/lib/astronoby/coordinates/horizontal.rb +++ b/lib/astronoby/coordinates/horizontal.rb @@ -18,39 +18,39 @@ def initialize( end def to_equatorial(time:) - latitude_radians = @latitude.to_radians.value + latitude_radians = @latitude.radians - t0 = Math.sin(@altitude.to_radians.value) * Math.sin(latitude_radians) + - Math.cos(@altitude.to_radians.value) * Math.cos(latitude_radians) * Math.cos(@azimuth.to_radians.value) + t0 = Math.sin(@altitude.radians) * Math.sin(latitude_radians) + + Math.cos(@altitude.radians) * Math.cos(latitude_radians) * Math.cos(@azimuth.radians) declination = Astronoby::Angle.as_radians(Math.asin(t0)) - t1 = Math.sin(@altitude.to_radians.value) - - Math.sin(latitude_radians) * Math.sin(declination.to_radians.value) + t1 = Math.sin(@altitude.radians) - + Math.sin(latitude_radians) * Math.sin(declination.radians) hour_angle_degrees = Astronoby::Angle.as_radians( Math.acos( - t1 / (Math.cos(latitude_radians) * Math.cos(declination.to_radians.value)) + t1 / (Math.cos(latitude_radians) * Math.cos(declination.radians)) ) - ).to_degrees + ).degrees - if Math.sin(@azimuth.to_radians.value).positive? + if Math.sin(@azimuth.radians).positive? hour_angle_degrees = Astronoby::Angle.as_degrees( - BigDecimal("360") - hour_angle_degrees.value - ) + BigDecimal("360") - hour_angle_degrees + ).degrees end - hour_angle_hours = hour_angle_degrees.to_hours + hour_angle_hours = Astronoby::Angle.as_degrees(hour_angle_degrees).hours right_ascension_decimal = Astronoby::Util::Time.local_sidereal_time( time: time, longitude: @longitude - ) - hour_angle_hours.value + ) - hour_angle_hours right_ascension_decimal += 24 if right_ascension_decimal.negative? right_ascension = Astronoby::Angle.as_hours(right_ascension_decimal) Equatorial.new( - right_ascension: right_ascension.to_hours, - declination: declination.to_degrees + right_ascension: right_ascension, + declination: declination ) end end diff --git a/lib/astronoby/errors.rb b/lib/astronoby/errors.rb index c4591e5..c0029ea 100644 --- a/lib/astronoby/errors.rb +++ b/lib/astronoby/errors.rb @@ -1,3 +1,5 @@ module Astronoby class IncompatibleArgumentsError < ArgumentError; end + + class UnsupportedFormatError < ArgumentError; end end diff --git a/lib/astronoby/mean_obliquity.rb b/lib/astronoby/mean_obliquity.rb index 85e2781..882a3d9 100644 --- a/lib/astronoby/mean_obliquity.rb +++ b/lib/astronoby/mean_obliquity.rb @@ -26,7 +26,7 @@ def self.for_epoch(epoch) new( Astronoby::Angle.as_degrees( - obliquity_of_reference.value - ( + obliquity_of_reference.degrees - ( 46.815 * t - 0.0006 * t * t + 0.00181 * t * t * t diff --git a/lib/astronoby/nutation.rb b/lib/astronoby/nutation.rb index 5d5e648..77e135f 100644 --- a/lib/astronoby/nutation.rb +++ b/lib/astronoby/nutation.rb @@ -25,8 +25,8 @@ def for_ecliptic_longitude 0, 0, ( - -17.2 * Math.sin(moon_ascending_node_longitude.to_radians.value) - - 1.3 * Math.sin(2 * sun_mean_longitude.to_radians.value) + -17.2 * Math.sin(moon_ascending_node_longitude.radians) - + 1.3 * Math.sin(2 * sun_mean_longitude.radians) ) ) end @@ -36,8 +36,8 @@ def for_obliquity_of_the_ecliptic 0, 0, ( - 9.2 * Math.cos(moon_ascending_node_longitude.to_radians.value) + - 0.5 * Math.cos(2 * sun_mean_longitude.to_radians.value) + 9.2 * Math.cos(moon_ascending_node_longitude.radians) + + 0.5 * Math.cos(2 * sun_mean_longitude.radians) ) ) end diff --git a/lib/astronoby/precession.rb b/lib/astronoby/precession.rb index 4c70966..8f441ec 100644 --- a/lib/astronoby/precession.rb +++ b/lib/astronoby/precession.rb @@ -19,8 +19,8 @@ def initialize(coordinates, epoch) # Edition: Cambridge University Press # Chapter: 34 - Precession def precess - right_ascension = @coordinates.right_ascension.to_radians.value - declination = @coordinates.declination.to_radians.value + right_ascension = @coordinates.right_ascension.radians + declination = @coordinates.declination.radians matrix_a = matrix_for_epoch(@coordinates.epoch) matrix_b = matrix_for_epoch(@epoch).transpose @@ -39,7 +39,7 @@ def precess Astronoby::Angle.as_radians(w[0]), Astronoby::Angle.as_radians(Math.atan(w[1] / w[0])) ), - declination: Astronoby::Angle.as_radians(Math.asin(w[2])).to_degrees, + declination: Astronoby::Angle.as_radians(Math.asin(w[2])), epoch: @epoch ) end @@ -53,13 +53,13 @@ def matrix_for_epoch(epoch) ζ = Astronoby::Angle.as_degrees( 0.6406161 * t + 0.0000839 * t * t + 0.000005 * t * t * t - ).to_radians.value + ).radians z = Astronoby::Angle.as_degrees( 0.6406161 * t + 0.0003041 * t * t + 0.0000051 * t * t * t - ).to_radians.value + ).radians θ = Astronoby::Angle.as_degrees( 0.5567530 * t - 0.0001185 * t * t - 0.0000116 * t * t * t - ).to_radians.value + ).radians cx = Math.cos(ζ) sx = Math.sin(ζ) diff --git a/lib/astronoby/refraction.rb b/lib/astronoby/refraction.rb index fd1458a..c9c3a0c 100644 --- a/lib/astronoby/refraction.rb +++ b/lib/astronoby/refraction.rb @@ -25,21 +25,21 @@ def initialize(coordinates, pressure, temperature) # Edition: Cambridge University Press # Chapter: 37 - Refraction def refract - altitude_in_degrees = @coordinates.altitude.to_degrees.value + altitude_in_degrees = @coordinates.altitude.degrees refraction_angle = Astronoby::Angle.as_degrees( if altitude_in_degrees > 15 zenith_angle = Astronoby::Angle.as_degrees( - 90 - @coordinates.altitude.value + 90 - @coordinates.altitude.degrees ) - 0.00452 * @pressure * Math.tan(zenith_angle.to_radians.value) / (273 + @temperature) + 0.00452 * @pressure * Math.tan(zenith_angle.radians) / (273 + @temperature) else ( @pressure * ( 0.1594 + 0.0196 * altitude_in_degrees + - 0.00002 * altitude_in_degrees * altitude_in_degree + 0.00002 * altitude_in_degrees * altitude_in_degrees ) )./( (273 + @temperature) * @@ -55,8 +55,7 @@ def refract Astronoby::Coordinates::Horizontal.new( azimuth: @coordinates.azimuth, altitude: Astronoby::Angle.as_degrees( - @coordinates.altitude.to_degrees.value + - refraction_angle.value + @coordinates.altitude.degrees + refraction_angle.degrees ), latitude: @coordinates.latitude, longitude: @coordinates.longitude diff --git a/lib/astronoby/true_obliquity.rb b/lib/astronoby/true_obliquity.rb index 750da00..6df6adf 100644 --- a/lib/astronoby/true_obliquity.rb +++ b/lib/astronoby/true_obliquity.rb @@ -12,7 +12,7 @@ def self.for_epoch(epoch) new( Astronoby::Angle.as_degrees( - mean_obliquity.value.to_degrees.value + nutation.to_degrees.value + mean_obliquity.value.degrees + nutation.degrees ) ) end diff --git a/lib/astronoby/util/astrodynamics.rb b/lib/astronoby/util/astrodynamics.rb index 2fcb233..29937d3 100644 --- a/lib/astronoby/util/astrodynamics.rb +++ b/lib/astronoby/util/astrodynamics.rb @@ -17,11 +17,11 @@ def eccentric_anomaly_newton_raphson( current_iteration = 0, solution_on_previous_interation = nil ) - previous_solution = solution_on_previous_interation&.to_radians&.value + previous_solution = solution_on_previous_interation&.radians solution = if current_iteration == 0 if orbital_eccentricity <= 0.75 - mean_anomaly.to_radians.value + mean_anomaly.radians else Math::PI end @@ -30,7 +30,7 @@ def eccentric_anomaly_newton_raphson( ( previous_solution - orbital_eccentricity * Math.sin(previous_solution) - - mean_anomaly.to_radians.value + mean_anomaly.radians ) / ( 1 - orbital_eccentricity * Math.cos(previous_solution) ) diff --git a/lib/astronoby/util/time.rb b/lib/astronoby/util/time.rb index f3389f0..686e880 100644 --- a/lib/astronoby/util/time.rb +++ b/lib/astronoby/util/time.rb @@ -79,11 +79,11 @@ def lst_to_ut(date:, longitude:, lst:) end def local_sidereal_time(time:, longitude:) - ut_to_gmst(time.utc) + longitude.to_hours.value + ut_to_gmst(time.utc) + longitude.hours end def lst_to_gmst(lst:, longitude:) - gmst = lst - longitude.to_hours.value + gmst = lst - longitude.hours # If gmst negative, add 24 hours to the date # If gmst is greater than 24, subtract 24 hours from the date diff --git a/lib/astronoby/util/trigonometry.rb b/lib/astronoby/util/trigonometry.rb index 490d9c7..652ed27 100644 --- a/lib/astronoby/util/trigonometry.rb +++ b/lib/astronoby/util/trigonometry.rb @@ -5,9 +5,6 @@ module Astronoby module Util module Trigonometry - PRECISION = 10 - PI = BigMath.PI(PRECISION) - class << self # Source: # Title: Celestial Calculations @@ -15,13 +12,13 @@ class << self # Edition: MIT Press # Chapter: 4 - Orbits and Coordinate Systems def adjustement_for_arctangent(y, x, angle) - return angle if y.value.positive? && x.value.positive? + return angle if y.radians.positive? && x.radians.positive? - if y.value.negative? && x.value.positive? - return Astronoby::Angle.as_degrees(angle.to_degrees.value + 360).to_radians + if y.radians.negative? && x.radians.positive? + return Astronoby::Angle.as_degrees(angle.degrees + 360) end - Astronoby::Angle.as_degrees(angle.to_degrees.value + 180).to_radians + Astronoby::Angle.as_degrees(angle.degrees + 180) end end end diff --git a/spec/astronoby/aberration_spec.rb b/spec/astronoby/aberration_spec.rb index 32a1c06..6a4895b 100644 --- a/spec/astronoby/aberration_spec.rb +++ b/spec/astronoby/aberration_spec.rb @@ -40,10 +40,10 @@ sun_longitude: sun_longitude ) - expect(apparent_coordinates.latitude.to_dms.format).to( + expect(apparent_coordinates.latitude.str(:dms)).to( eq("-1° 32′ 56.3319″") ) - expect(apparent_coordinates.longitude.to_dms.format).to( + expect(apparent_coordinates.longitude.str(:dms)).to( eq("+352° 37′ 30.4521″") ) end diff --git a/spec/astronoby/angle_spec.rb b/spec/astronoby/angle_spec.rb index a6abbed..b7b4b81 100644 --- a/spec/astronoby/angle_spec.rb +++ b/spec/astronoby/angle_spec.rb @@ -1,111 +1,137 @@ # frozen_string_literal: true RSpec.describe Astronoby::Angle do - describe "::as_degrees" do - subject { described_class.as_degrees(180) } - + describe "::as_radians" do it "returns an Angle object" do - expect(subject).to be_a(described_class) + expect(described_class.as_radians(described_class::PI)) + .to be_a(described_class) end + end - it "returns an Angle in Degree" do - expect(subject).to be_a(Astronoby::Degree) + describe "::as_degrees" do + it "returns an Angle object" do + expect(described_class.as_degrees(180)).to be_a(described_class) end end - describe "::as_dms" do + describe "::as_hours" do it "returns an Angle object" do - angle = described_class.as_dms(180, 15, 10) - expect(angle).to be_a(described_class) + expect(described_class.as_hours(23)).to be_a(described_class) end + end + + describe "#radians" do + it "returns the angle value in radian unit" do + radians = described_class.new(described_class::PI).radians - it "returns an Angle in Degree" do - angle = described_class.as_dms(180, 15, 10) - expect(angle).to be_a(Astronoby::Degree) + expect(radians).to eq described_class::PI end - it "converts HMS format into decimal format" do - angle = described_class.as_dms(180, 15, 45) - expect(angle.value).to eq(180.2625) + context "when the angle is initialized from degrees" do + it "returns the angle value in radian unit" do + radians = described_class.as_degrees(180).radians + + expect(radians).to eq described_class::PI + end end - it "converts HMS format into decimal format when negative" do - angle = described_class.as_dms(-180, 15, 45) - expect(angle.value).to eq(-180.2625) + context "when the angle is initialized from hours" do + it "returns the angle value in radian unit within fixed precision" do + radians = described_class.as_hours(12).radians + + expect(radians) + .to be_within(10**-described_class::PRECISION).of(described_class::PI) + end end end - describe "::as_hms" do - it "returns an Angle object" do - angle = described_class.as_hms(12, 15, 10) - expect(angle).to be_a(described_class) + describe "#degrees" do + it "returns the angle value in degree unit" do + degrees = described_class.as_degrees(180).degrees + + expect(degrees).to eq 180 end - it "returns an Angle in Hour" do - angle = described_class.as_hms(12, 15, 10) - expect(angle).to be_a(Astronoby::Hour) + context "when the angle is initialized from radians" do + it "returns the angle value in degree unit" do + degrees = described_class.as_radians(described_class::PI).degrees + + expect(degrees).to eq 180 + end end - it "converts HMS format into decimal format" do - angle = described_class.as_hms(12, 15, 45) - expect(angle.value).to eq(12.2625) + context "when the angle is initialized from hours" do + it "returns the angle value in degree unit within fixed precision" do + degrees = described_class.as_hours(12).degrees + + expect(degrees).to be_within(10**-described_class::PRECISION).of(180) + end end end - describe "::as_hours" do - subject { described_class.as_hours(180) } + describe "#hours" do + it "returns the angle value in degree unit within fixed precision" do + hours = described_class.as_hours(12).hours - it "returns an Angle object" do - expect(subject).to be_a(described_class) + expect(hours).to eq 12 end - it "returns an Angle in Hour" do - expect(subject).to be_a(Astronoby::Hour) + context "when the angle is initialized from radians" do + it "returns the angle value in degree unit within fixed precision" do + hours = described_class.as_radians(described_class::PI).hours + + expect(hours).to be_within(10**-described_class::PRECISION).of(12) + end end - end - describe "::as_radians" do - subject { described_class.as_radians(described_class::PI) } + context "when the angle is initialized from degrees" do + it "returns the angle value in degree unit within fixed precision" do + hours = described_class.as_degrees(180).hours - it "returns an Angle object" do - expect(subject).to be_a(described_class) + expect(hours).to be_within(10**-described_class::PRECISION).of(12) + end end + end + + describe "#str" do + it "returns a String" do + angle = described_class.as_degrees(180) - it "returns an Angle in Radian" do - expect(subject).to be_a(Astronoby::Radian) + expect(angle.str(:dms)).to be_a(String) end - end - describe "#==" do - context "when the two angles have the same value and same unit" do - it "returns true" do - expect(described_class.as_degrees(1)).to( - eq(described_class.as_degrees(1)) - ) + describe "to the DMS format" do + context "when the angle is positive" do + it "properly formats the angle" do + angle = described_class.as_degrees(10.2958) + + expect(angle.str(:dms)).to eq "+10° 17′ 44.88″" + end end - end - context "when the two angles have the same value and different units" do - it "returns false" do - expect(described_class.as_degrees(1)).not_to( - eq(described_class.as_radians(1)) - ) + context "when the angle is negative" do + it "properly formats the angle" do + angle = described_class.as_degrees(-10.2958) + + expect(angle.str(:dms)).to eq "-10° 17′ 44.88″" + end end end - context "when the two angles have different values and the same unit" do - it "returns false" do - expect(described_class.as_degrees(1)).not_to( - eq(described_class.as_degrees(2)) - ) + describe "to the HMS format" do + it "properly formats the angle" do + angle = described_class.as_hours(12.345678) + + expect(angle.str(:hms)).to eq "12h 20m 44.4408s" end end - context "when the two angles have different values and different units" do - it "returns false" do - expect(described_class.as_degrees(1)).not_to( - eq(described_class.as_radians(2)) - ) + describe "to an unsupported format" do + it "raises an UnsupportedFormatError exception" do + angle = described_class.as_degrees(180) + + expect { angle.str(:unknown) } + .to raise_error(Astronoby::UnsupportedFormatError, "Expected a format between dms, hms, got unknown") end end end diff --git a/spec/astronoby/angles/degree_spec.rb b/spec/astronoby/angles/degree_spec.rb deleted file mode 100644 index 5954b74..0000000 --- a/spec/astronoby/angles/degree_spec.rb +++ /dev/null @@ -1,68 +0,0 @@ -# frozen_string_literal: true - -require "bigdecimal/util" - -RSpec.describe Astronoby::Degree do - let(:instance) { described_class.new(value) } - let(:value) { 180 } - - describe "#value" do - subject { instance.value } - - it "returns the angle's numeric value in the current unit" do - expect(subject).to eq(180) - end - end - - describe "#to_degrees" do - subject { instance.to_degrees } - - it "returns itself" do - expect(subject).to eq(instance) - end - end - - describe "#to_hours" do - it "returns a new Hour instance" do - angle = described_class.new(1) - - expect(angle.to_hours).to be_a(Astronoby::Hour) - end - - it "converted the degrees value into hours" do - angle = described_class.new(330) - - expect(angle.to_hours.value).to eq(22) - end - end - - describe "#to_radians" do - subject { instance.to_radians } - - it "returns a new Radian instance" do - expect(subject).to be_a(Astronoby::Radian) - end - - it "converted the degrees value into radians" do - expect(subject.value).to eq(described_class::PI.ceil(described_class::PRECISION)) - end - end - - describe "#to_dms" do - subject { instance.to_dms } - - it "returns an new Dms instance" do - expect(subject).to be_a(Astronoby::Dms) - end - - context "when ange is positive" do - let(:value) { 10.2958 } - - it "converts properly" do - expect(subject.degrees).to eq(10) - expect(subject.minutes).to eq(17) - expect(subject.seconds).to eq(44.88) - end - end - end -end diff --git a/spec/astronoby/angles/hour_spec.rb b/spec/astronoby/angles/hour_spec.rb deleted file mode 100644 index be17c4c..0000000 --- a/spec/astronoby/angles/hour_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -require "bigdecimal/util" - -RSpec.describe Astronoby::Hour do - describe "#to_hours" do - it "returns itself" do - angle = described_class.new(1.234) - - expect(angle).to be(angle) - end - end - - describe "#to_degrees" do - it "returns a new Degree instance" do - angle = described_class.new(1) - - expect(angle.to_degrees).to be_a(Astronoby::Degree) - end - - it "converted the degrees value into radians" do - angle = described_class.new(23) - - expect(angle.to_degrees.value).to eq(345) - end - end - - describe "#to_radians" do - it "returns a new Radian instance" do - angle = described_class.new(1) - - expect(angle.to_radians).to be_a(Astronoby::Radian) - end - - it "converted the hours value into radians" do - angle = described_class.new(12) - - expect(angle.to_radians.value).to( - eq described_class::PI.ceil(described_class::PRECISION) - ) - end - end -end diff --git a/spec/astronoby/angles/radian_spec.rb b/spec/astronoby/angles/radian_spec.rb deleted file mode 100644 index dee775b..0000000 --- a/spec/astronoby/angles/radian_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Astronoby::Radian do - let(:instance) { described_class.new(value) } - let(:value) { described_class::PI } - - describe "#value" do - subject { instance.value } - - it "returns the angle's numeric value in the current unit" do - expect(instance.value).to eq(described_class::PI.ceil(described_class::PRECISION)) - end - end - - describe "#to_degrees" do - subject { instance.to_degrees } - - it "returns a new Degree instance" do - expect(subject).to be_a(Astronoby::Degree) - end - - it "converted the degrees value into degrees" do - expect(subject.value).to be_within(0.1).of(180) - end - end - - describe "#to_hours" do - it "returns a new Hour instance" do - angle = described_class.new(1) - - expect(angle.to_hours).to be_a(Astronoby::Hour) - end - - it "converted the degrees value into degrees" do - angle = described_class.new( - described_class::PI.ceil(described_class::PRECISION) - ) - - expect(angle.to_hours.value).to be_within(0.1).of(12) - end - end - - describe "#to_radians" do - subject { instance.to_radians } - - it "returns itself" do - expect(subject).to eq(instance) - end - end - - describe "#to_dms" do - subject { instance.to_dms } - - it "returns an new Dms instance" do - expect(subject).to be_a(Astronoby::Dms) - end - - context "when angle is positive" do - let(:value) { described_class::PI / 7r } - - it "converts properly" do - expect(subject.sign).to eq("+") - expect(subject.degrees).to eq(25) - expect(subject.minutes).to eq(42) - expect(subject.seconds).to eq(51.4285) - end - end - end -end diff --git a/spec/astronoby/bodies/sun_spec.rb b/spec/astronoby/bodies/sun_spec.rb index 3f939f9..97e3b00 100644 --- a/spec/astronoby/bodies/sun_spec.rb +++ b/spec/astronoby/bodies/sun_spec.rb @@ -21,8 +21,8 @@ ecliptic_coordinates = described_class.new(epoch: epoch).ecliptic_coordinates - expect(ecliptic_coordinates.longitude.to_degrees.value.to_f).to( - eq(316.5726713406947) + expect(ecliptic_coordinates.longitude.degrees.to_f).to( + eq(316.57267134069514) ) end @@ -37,8 +37,8 @@ ecliptic_coordinates = described_class.new(epoch: epoch).ecliptic_coordinates - expect(ecliptic_coordinates.longitude.to_degrees.value.to_f).to( - eq(137.36484079770828) + expect(ecliptic_coordinates.longitude.degrees.to_f).to( + eq(137.36484079771108) ) end @@ -53,8 +53,8 @@ ecliptic_coordinates = described_class.new(epoch: epoch).ecliptic_coordinates - expect(ecliptic_coordinates.longitude.to_degrees.value).to( - eq(45.92185191446102) + expect(ecliptic_coordinates.longitude.degrees.to_f).to( + eq(45.92185191445215) ) end @@ -70,10 +70,10 @@ ecliptic_coordinates = described_class.new(epoch: epoch).ecliptic_coordinates equatorial_coordinates = ecliptic_coordinates.to_equatorial(epoch: epoch) - expect(equatorial_coordinates.right_ascension.to_hours.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("8h 26m 3.6131s") ) - expect(equatorial_coordinates.declination.to_degrees.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("+19° 12′ 43.1836″") ) end @@ -90,10 +90,10 @@ ecliptic_coordinates = described_class.new(epoch: epoch).ecliptic_coordinates equatorial_coordinates = ecliptic_coordinates.to_equatorial(epoch: epoch) - expect(equatorial_coordinates.right_ascension.to_hours.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("13h 13m 31.4636s") ) - expect(equatorial_coordinates.declination.to_degrees.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("-7° 47′ 6.9653″") ) end @@ -128,10 +128,10 @@ longitude: Astronoby::Angle.as_degrees(-78) ) - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("+35° 47′ 12.6437″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+172° 17′ 2.7301″") ) end @@ -152,10 +152,10 @@ ) # TODO: very far from the expected value - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("+53° 44′ 26.5426″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+105° 8′ 12.9486″") ) end @@ -175,10 +175,10 @@ longitude: Astronoby::Angle.as_degrees(-30) ) - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("+13° 34′ 17.4237″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+293° 37′ 12.5231″") ) end diff --git a/spec/astronoby/body_spec.rb b/spec/astronoby/body_spec.rb index 731ea9b..dffbfdd 100644 --- a/spec/astronoby/body_spec.rb +++ b/spec/astronoby/body_spec.rb @@ -91,9 +91,7 @@ latitude: Astronoby::Angle.as_degrees(38) ) - expect(rising_azimuth.to_degrees.value).to( - be_within(10**-9).of(80.465577913) - ) + expect(rising_azimuth&.degrees).to be_within(10**-9).of(80.465577913) end end @@ -187,9 +185,7 @@ latitude: Astronoby::Angle.as_degrees(38) ) - expect(setting_azimuth.to_degrees.value).to( - be_within(10**-7).of(279.534422) - ) + expect(setting_azimuth&.degrees).to be_within(10**-7).of(279.534422) end end end diff --git a/spec/astronoby/coordinates/ecliptic_spec.rb b/spec/astronoby/coordinates/ecliptic_spec.rb index b7581e3..a2f6bfa 100644 --- a/spec/astronoby/coordinates/ecliptic_spec.rb +++ b/spec/astronoby/coordinates/ecliptic_spec.rb @@ -18,10 +18,10 @@ longitude: longitude ).to_equatorial(epoch: epoch) - expect(equatorial_coordinates.right_ascension.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("12h 18m 47.4954s") ) - expect(equatorial_coordinates.declination.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("-0° 43′ 35.5098″") ) end @@ -43,10 +43,10 @@ longitude: longitude ).to_equatorial(epoch: epoch) - expect(equatorial_coordinates.right_ascension.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("8h 10m 50.4188s") ) - expect(equatorial_coordinates.declination.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("+20° 2′ 30.795″") ) end @@ -68,10 +68,10 @@ longitude: longitude ).to_equatorial(epoch: epoch) - expect(equatorial_coordinates.right_ascension.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("9h 34m 53.3214s") ) - expect(equatorial_coordinates.declination.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("+19° 32′ 6.0105″") ) end diff --git a/spec/astronoby/coordinates/equatorial_spec.rb b/spec/astronoby/coordinates/equatorial_spec.rb index dd92eb8..e2bc060 100644 --- a/spec/astronoby/coordinates/equatorial_spec.rb +++ b/spec/astronoby/coordinates/equatorial_spec.rb @@ -23,9 +23,7 @@ longitude: longitude ) - expect(hour_angle.to_hours.to_hms.format).to( - eq("9h 52m 23.6554s") - ) + expect(hour_angle.str(:hms)).to eq "9h 52m 23.6554s" end end @@ -59,10 +57,10 @@ declination: Astronoby::Angle.as_dms(-22, 10, 0) ).to_horizontal(time: time, latitude: latitude, longitude: longitude) - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("-73° 27′ 19.1557″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+341° 33′ 21.587″") ) end @@ -84,10 +82,10 @@ declination: Astronoby::Angle.as_dms(7, 29, 54) ).to_horizontal(time: time, latitude: latitude, longitude: longitude) - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("+59° 13′ 0.3617″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+171° 5′ 0.4263″") ) end @@ -109,10 +107,10 @@ declination: Astronoby::Angle.as_degrees(-60) ).to_horizontal(time: time, latitude: latitude, longitude: longitude) - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("-59° 41′ 58.4833″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+224° 15′ 26.7345″") ) end @@ -134,10 +132,10 @@ hour_angle: Astronoby::Angle.as_hms(5, 51, 44) ).to_horizontal(time: time, latitude: latitude, longitude: longitude) - expect(horizontal_coordinates.altitude.to_dms.format).to( + expect(horizontal_coordinates.altitude.str(:dms)).to( eq("+19° 20′ 3.6428″") ) - expect(horizontal_coordinates.azimuth.to_dms.format).to( + expect(horizontal_coordinates.azimuth.str(:dms)).to( eq("+283° 16′ 15.6981″") ) end @@ -161,10 +159,10 @@ declination: declination ).to_ecliptic(epoch: epoch) - expect(ecliptic_coordinates.latitude.to_dms.format).to( + expect(ecliptic_coordinates.latitude.str(:dms)).to( eq("+22° 41′ 53.8752″") ) - expect(ecliptic_coordinates.longitude.to_dms.format).to( + expect(ecliptic_coordinates.longitude.str(:dms)).to( eq("+156° 19′ 8.9427″") ) end @@ -186,10 +184,10 @@ declination: declination ).to_ecliptic(epoch: epoch) - expect(ecliptic_coordinates.latitude.to_dms.format).to( + expect(ecliptic_coordinates.latitude.str(:dms)).to( eq("+6° 41′ 3.0103″") ) - expect(ecliptic_coordinates.longitude.to_dms.format).to( + expect(ecliptic_coordinates.longitude.str(:dms)).to( eq("+113° 12′ 56.2651″") ) end @@ -211,10 +209,10 @@ declination: declination ).to_ecliptic(epoch: epoch) - expect(ecliptic_coordinates.latitude.to_dms.format).to( + expect(ecliptic_coordinates.latitude.str(:dms)).to( eq("+4° 52′ 30.993″") ) - expect(ecliptic_coordinates.longitude.to_dms.format).to( + expect(ecliptic_coordinates.longitude.str(:dms)).to( eq("+139° 41′ 9.9812″") ) end diff --git a/spec/astronoby/coordinates/horizontal_spec.rb b/spec/astronoby/coordinates/horizontal_spec.rb index 93a2bdc..34ebd6b 100644 --- a/spec/astronoby/coordinates/horizontal_spec.rb +++ b/spec/astronoby/coordinates/horizontal_spec.rb @@ -27,10 +27,10 @@ longitude: Astronoby::Angle.as_degrees(-78) ).to_equatorial(time: Time.new(2016, 1, 21, 21, 45, 0, "-05:00")) - expect(equatorial_coordinates.right_ascension.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("5h 54m 58.0175s") ) - expect(equatorial_coordinates.declination.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("+7° 29′ 53.6679″") ) end @@ -50,10 +50,10 @@ longitude: Astronoby::Angle.as_degrees(-78) ).to_equatorial(time: Time.new(2016, 1, 21, 21, 30, 0, "-05:00")) - expect(equatorial_coordinates.right_ascension.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("17h 43m 54.0935s") ) - expect(equatorial_coordinates.declination.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("-22° 10′ 0.2016″") ) end @@ -73,10 +73,10 @@ longitude: Astronoby::Angle.as_degrees(-78.3) ).to_equatorial(time: Time.new(2015, 6, 6, 21, 0, 0, "-04:00")) - expect(equatorial_coordinates.right_ascension.to_hms.format).to( + expect(equatorial_coordinates.right_ascension.str(:hms)).to( eq("16h 14m 41.827s") ) - expect(equatorial_coordinates.declination.to_dms.format).to( + expect(equatorial_coordinates.declination.str(:dms)).to( eq("+25° 57′ 41.0393″") ) end diff --git a/spec/astronoby/mean_obliquity_spec.rb b/spec/astronoby/mean_obliquity_spec.rb index 2cf5bc3..1b8c8aa 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.to_degrees.value).to eq(23.439291666667) + expect(obliquity.degrees).to eq(23.43929166666666) end it "returns the obliquity angle for epoch 1950" do obliquity = described_class.for_epoch(Astronoby::Epoch::J1950).value - expect(obliquity.to_degrees.value).to eq(23.44579385451423) + expect(obliquity.degrees.to_f).to eq 23.445793854513884 end # Source: @@ -30,9 +30,7 @@ epoch = Astronoby::Epoch.from_time(Time.utc(2009, 7, 6, 0, 0, 0)) obliquity = described_class.for_epoch(epoch).value - expect(obliquity.to_degrees.to_dms.format).to( - eq("+23° 26′ 16.9979″") - ) + expect(obliquity.str(:dms)).to eq "+23° 26′ 16.9979″" end end @@ -46,9 +44,7 @@ epoch = Astronoby::Epoch.from_time(Time.utc(1987, 4, 10, 0, 0, 0)) obliquity = described_class.for_epoch(epoch).value - expect(obliquity.to_degrees.to_dms.format).to( - eq("+23° 26′ 27.4093″") - ) + expect(obliquity.str(:dms)).to eq "+23° 26′ 27.4093″" end end end diff --git a/spec/astronoby/nutation_spec.rb b/spec/astronoby/nutation_spec.rb index 574bdb3..ab0e8d1 100644 --- a/spec/astronoby/nutation_spec.rb +++ b/spec/astronoby/nutation_spec.rb @@ -2,12 +2,12 @@ RSpec.describe Astronoby::Nutation do describe "::for_ecliptic_longitude" do - it "returns an angle in degrees" do + it "returns an Angle object" do nutation = described_class.for_ecliptic_longitude( epoch: Astronoby::Epoch::J2000 ) - expect(nutation).to be_a(Astronoby::Degree) + expect(nutation).to be_a(Astronoby::Angle) end # Source: @@ -20,19 +20,17 @@ epoch: Astronoby::Epoch.from_time(Time.utc(1988, 9, 1, 0, 0, 0)) ) - expect(nutation.to_degrees.to_dms.format).to( - eq("+0° 0′ 5.4929″") - ) + expect(nutation.str(:dms)).to eq "+0° 0′ 5.4929″" end end describe "::for_obliquity_of_the_ecliptic" do - it "returns an angle in degrees" do + it "returns an Angle object" do nutation = described_class.for_obliquity_of_the_ecliptic( epoch: Astronoby::Epoch::J2000 ) - expect(nutation).to be_a(Astronoby::Degree) + expect(nutation).to be_a(Astronoby::Angle) end # Source: @@ -45,9 +43,7 @@ epoch: Astronoby::Epoch.from_time(Time.utc(1988, 9, 1, 0, 0, 0)) ) - expect(nutation.to_degrees.to_dms.format).to( - eq("+0° 0′ 9.2415″") - ) + expect(nutation.str(:dms)).to eq "+0° 0′ 9.2415″" end end end diff --git a/spec/astronoby/precession_spec.rb b/spec/astronoby/precession_spec.rb index 13cb99b..29b0ab2 100644 --- a/spec/astronoby/precession_spec.rb +++ b/spec/astronoby/precession_spec.rb @@ -45,10 +45,10 @@ epoch: Astronoby::Epoch.from_time(Time.utc(1979, 6, 1, 0, 0, 0)) ) - expect(precessed_coordinates.right_ascension.to_hours.to_hms.format).to( + expect(precessed_coordinates.right_ascension.str(:hms)).to( eq("9h 12m 20.1577s") ) - expect(precessed_coordinates.declination.to_dms.format).to( + expect(precessed_coordinates.declination.str(:dms)).to( eq("+14° 16′ 7.6506″") ) end @@ -72,10 +72,10 @@ epoch: Astronoby::Epoch::J2000 ) - expect(precessed_coordinates.right_ascension.to_hours.to_hms.format).to( + expect(precessed_coordinates.right_ascension.str(:hms)).to( eq("12h 34m 34.1434s") ) - expect(precessed_coordinates.declination.to_dms.format).to( + expect(precessed_coordinates.declination.str(:dms)).to( eq("+29° 49′ 8.3259″") ) end @@ -99,10 +99,10 @@ epoch: Astronoby::Epoch.from_time(Time.utc(2015, 1, 1, 0, 0, 0)) ) - expect(precessed_coordinates.right_ascension.to_hours.to_hms.format).to( + expect(precessed_coordinates.right_ascension.str(:hms)).to( eq("12h 35m 18.383s") ) - expect(precessed_coordinates.declination.to_dms.format).to( + expect(precessed_coordinates.declination.str(:dms)).to( eq("+29° 44′ 10.8629″") ) end diff --git a/spec/astronoby/refraction_spec.rb b/spec/astronoby/refraction_spec.rb index 145cb47..bcf1836 100644 --- a/spec/astronoby/refraction_spec.rb +++ b/spec/astronoby/refraction_spec.rb @@ -37,7 +37,7 @@ ) expect(apparent_coordinates.azimuth).to eq(true_coordinates.azimuth) - expect(apparent_coordinates.altitude.to_dms.format).to( + expect(apparent_coordinates.altitude.str(:dms)).to( eq("+19° 22′ 47.0924″") ) expect(apparent_coordinates.latitude).to eq(true_coordinates.latitude) diff --git a/spec/astronoby/true_obliquity_spec.rb b/spec/astronoby/true_obliquity_spec.rb index 62eed6c..aa217b8 100644 --- a/spec/astronoby/true_obliquity_spec.rb +++ b/spec/astronoby/true_obliquity_spec.rb @@ -18,9 +18,7 @@ epoch = Astronoby::Epoch.from_time(Time.utc(1987, 4, 10, 0, 0, 0)) obliquity = described_class.for_epoch(epoch).value - expect(obliquity.to_degrees.to_dms.format).to( - eq("+23° 26′ 36.8401″") - ) + expect(obliquity.str(:dms)).to eq "+23° 26′ 36.8401″" end end end diff --git a/spec/astronoby/util/astrodynamics_spec.rb b/spec/astronoby/util/astrodynamics_spec.rb index af77659..0add706 100644 --- a/spec/astronoby/util/astrodynamics_spec.rb +++ b/spec/astronoby/util/astrodynamics_spec.rb @@ -36,7 +36,7 @@ iterations ) - expect(solution.value).to eq(0.78539851485077) + expect(solution.radians).to be_within(precision).of(0.78539851485077) end # Source: @@ -57,7 +57,7 @@ iterations ) - expect(solution.to_degrees.value).to be_within(precision).of(5.5) + expect(solution.degrees).to be_within(precision).of(5.5) end # Source: @@ -78,7 +78,7 @@ iterations ) - expect(solution.to_degrees.value).to be_within(precision).of(16.744355) + expect(solution.degrees).to be_within(precision).of(16.744355) end # Source: @@ -99,7 +99,7 @@ iterations ) - expect(solution.to_degrees.value).to be_within(precision).of(29.422286) + expect(solution.degrees).to be_within(precision).of(29.422286) end end end diff --git a/spec/astronoby/util/time_spec.rb b/spec/astronoby/util/time_spec.rb index 7b0dce9..066d528 100644 --- a/spec/astronoby/util/time_spec.rb +++ b/spec/astronoby/util/time_spec.rb @@ -210,7 +210,7 @@ expect(gmst.to_i).to eq(4) expect(minute.to_i).to eq(40) - expect(second.ceil(4)).to eq(5.2308) + expect(second.ceil(4)).to eq(5.2309) end end end diff --git a/spec/astronoby/util/trigonometry_spec.rb b/spec/astronoby/util/trigonometry_spec.rb index e28127e..7ea7ffd 100644 --- a/spec/astronoby/util/trigonometry_spec.rb +++ b/spec/astronoby/util/trigonometry_spec.rb @@ -38,7 +38,8 @@ Astronoby::Angle.as_degrees(0) ) - expect(adjustement).to eq(Astronoby::Angle.as_radians(Math::PI)) + expect(adjustement.radians) + .to be_within(Astronoby::Angle::PRECISION).of(Math::PI) end end @@ -50,9 +51,8 @@ Astronoby::Angle.as_degrees(0) ) - expect(adjustement.value).to( - be_within(10**-14).of(Astronoby::Angle.as_radians(Math::PI * 2).value) - ) + expect(adjustement.radians) + .to be_within(Astronoby::Angle::PRECISION).of(Math::PI * 2) end end @@ -64,7 +64,8 @@ Astronoby::Angle.as_degrees(0) ) - expect(adjustement).to eq(Astronoby::Angle.as_radians(Math::PI)) + expect(adjustement.radians) + .to be_within(Astronoby::Angle::PRECISION).of(Math::PI) end end end