From e7efeb459289262a2864548029905b489326f246 Mon Sep 17 00:00:00 2001 From: SamW Date: Thu, 8 Feb 2024 16:09:12 -0800 Subject: [PATCH] Update Rational --- core/numeric.rbs | 65 +++++++ core/rational.rbs | 238 +++++++++++++---------- test/stdlib/Rational_test.rb | 354 ++++++++++++++++++++++------------- test/stdlib/test_helper.rb | 16 ++ 4 files changed, 442 insertions(+), 231 deletions(-) diff --git a/core/numeric.rbs b/core/numeric.rbs index 613144948..a40f4d7ab 100644 --- a/core/numeric.rbs +++ b/core/numeric.rbs @@ -160,6 +160,71 @@ # * #step: Invokes the given block with the sequence of specified numbers. # class Numeric + interface _Coerce[Other, EquivOther, EquivSelf] + def coerce: (Other rhs) -> [EquivOther, EquivSelf] + end + + interface _Add[Other, Return] + def +: (Other other) -> Return + end + + interface _Subtract[Other, Return] + def -: (Other other) -> Return + end + + interface _Multiply[Other, Return] + def *: (Other other) -> Return + end + + interface _Divide[Other, Return] + def /: (Other other) -> Return + end + + interface _Modulo[Other, Return] + def %: (Other other) -> Return + end + + interface _Power[Other, Return] + def **: (Other other) -> Return + end + + interface _Divmod[Other, Whole, Part] + def divmod: (Other other) -> [Whole, Part] + end + + interface _Compare[Other] + def <=>: (Other other) -> (-1 | 0 | 1) + | (untyped other) -> (-1 | 0 | 1)? + end + + interface _GreaterThan[Other, Return] + def >: (Other other) -> Return + end + + interface _GreaterOrEqualTo[Other, Return] + def >=: (Other other) -> Return + end + + interface _LessThan[Other, Return] + def <: (Other other) -> Return + end + + interface _LessOrEqualTo[Other, Return] + def <=: (Other other) -> Return + end + + type round_direction = :up | :down | :even | 'up' | 'down' | 'even' | string | nil + + # The interface for the sole optional argument to subclasses of `Numeric`'s `rationalize` function. + interface _Eps + # Get the absolute value of this variable. + # + # Technically, the return value needs to be usable within `#-` and `#+`, but + # it's a bit specific; if you have a usecase for this please create a PR + def abs: () -> Numeric + end + + public include Comparable public diff --git a/core/rational.rbs b/core/rational.rbs index 1652dec67..28704bde4 100644 --- a/core/rational.rbs +++ b/core/rational.rbs @@ -48,13 +48,6 @@ # #=> (1.0000000000000002+1.7320508075688772i) # class Rational < Numeric - public - - def %: (Integer) -> Rational - | (Float) -> Float - | (Rational) -> Rational - | (Numeric) -> Numeric - # # Negates `rat`. # - def -@: () -> Rational + def -@: () -> instance # => Obtainable via `-Class.instance_metod(:new).bind_call(Class.new(Rational))` # # Returns the absolute value of `rat`. # @@ -328,9 +295,6 @@ class Rational < Numeric # alias magnitude abs - def modulo: (Float) -> Float - | (Numeric) -> Rational - #