Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/numeric.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class Numeric
# The direction you can round
type round_mode = :up | :down | :even | 'up' | 'down' | 'even' | string | nil

# An interface that indicates a type can be coerced.
interface _Coerce[O, Other, Self]
def coerce: (O other) -> [Other, Self]
end

# <!--
# rdoc-file=numeric.c
# - self % other -> real_numeric
Expand Down
71 changes: 40 additions & 31 deletions core/rational.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
# Rational(900) * Rational(1) #=> (900/1)
# Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
#
def *: (Integer) -> Rational
| (Rational) -> Rational
| [T < Numeric](T) -> T
def *: (Integer | Rational other) -> instance

Check failure on line 67 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 67 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| (Float other) -> Float
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Times[S, R], S] other) -> R

# <!--
# rdoc-file=rational.c
Expand All @@ -81,9 +81,14 @@
# Rational(1, 2) ** 0 #=> (1/1)
# Rational(1, 2) ** 0.0 #=> 1.0
#
def **: (Complex) -> Complex
| (Numeric) -> Numeric
def **: (Integer exponent) -> instance

Check failure on line 84 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 84 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| (Rational exponent) -> (instance | Float | Complex) # `instance` only for special-cases like `**0r`

Check failure on line 85 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 85 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| (Float exponent) -> (Float | Complex)
| (Complex exponent) -> (instance | Complex) # instance only for special-cases like `**0i`

Check failure on line 87 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 87 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| [S, R] ((Numeric & Numeric::_Coerce[self, RBS::Ops::_Power[S, R], S]) other) -> (R | Rational)
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Power[S, R], S] other) -> R


Check failure on line 91 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Layout/EmptyLines: Extra blank line detected.

Check failure on line 91 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Layout/EmptyLines: Extra blank line detected.
# <!--
# rdoc-file=rational.c
# - self + other -> numeric
Expand All @@ -106,9 +111,9 @@
# Rational(2, 3) + 1.0 # => 1.6666666666666665
# Rational(2, 3) + Complex(1.0, 0.0) # => (1.6666666666666665+0.0i)
#
def +: (Float) -> Float
| (Complex) -> Complex
| (Numeric) -> Rational
def +: (Integer | Rational other) -> instance

Check failure on line 114 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 114 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| (Float other) -> Float
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Add[S, R], S] other) -> R

# <!--
# rdoc-file=rational.c
Expand All @@ -122,9 +127,9 @@
# Rational(9, 8) - 4 #=> (-23/8)
# Rational(20, 9) - 9.8 #=> -7.577777777777778
#
def -: (Float) -> Float
| (Complex) -> Complex
| (Numeric) -> Rational
def -: (Integer | Rational other) -> instance

Check failure on line 130 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 130 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| (Float other) -> Float
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Subtract[S, R], S] other) -> R

# <!--
# rdoc-file=rational.c
Expand All @@ -135,7 +140,7 @@
# -(1/3r) # => (-1/3)
# -(-1/3r) # => (1/3)
#
def -@: () -> Rational
def -@: () -> instance

Check failure on line 143 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 143 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

# <!--
# rdoc-file=rational.c
Expand All @@ -149,9 +154,9 @@
# Rational(9, 8) / 4 #=> (9/32)
# Rational(20, 9) / 9.8 #=> 0.22675736961451246
#
def /: (Float) -> Float
| (Complex) -> Complex
| (Numeric) -> Rational
def /: (Integer | Rational other) -> instance

Check failure on line 157 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 157 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.
| (Float other) -> Float
| [S, R] (Numeric::_Coerce[self, RBS::Ops::_Divide[S, R], S] other) -> R

# <!--
# rdoc-file=rational.c
Expand Down Expand Up @@ -179,8 +184,8 @@
# Class Rational includes module Comparable, each of whose methods uses
# Rational#<=> for comparison.
#
def <=>: (Integer | Rational) -> Integer
| (untyped) -> Integer?
def <=>: (Integer | Rational other) -> (-1 | 0 | 1)
| (untyped other) -> Integer?

# <!--
# rdoc-file=rational.c
Expand All @@ -194,7 +199,7 @@
# Rational('1/3') == 0.33 #=> false
# Rational('1/2') == '1/2' #=> false
#
def ==: (untyped) -> bool
def ==: (untyped object) -> bool

# <!--
# rdoc-file=rational.c
Expand All @@ -206,7 +211,7 @@
# (1/2r).abs #=> (1/2)
# (-1/2r).abs #=> (1/2)
#
def abs: () -> Rational
def abs: () -> instance

Check failure on line 214 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

Check failure on line 214 in core/rational.rbs

View workflow job for this annotation

GitHub Actions / test (4.0, rubocop validate test_doc build test_generate_stdlib raap)

RBS/Style/InstanceWithInstance: Use `self` instead of `instance`.

# <!--
# rdoc-file=rational.c
Expand All @@ -232,9 +237,11 @@
# Rational('-123.456').ceil(-1) #=> -120
#
def ceil: () -> Integer
| (Integer digits) -> (Integer | Rational)
| (Integer ndigits) -> (Integer | instance)

def coerce: (Numeric) -> [ Numeric, Numeric ]
def coerce: (Integer | Rational other) -> [ instance, self ]
| (Float other) -> [ Float, Float ]
| (Complex other) -> ([ Complex, Complex ] | [ instance, self ])

# <!--
# rdoc-file=rational.c
Expand Down Expand Up @@ -262,7 +269,8 @@
# Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333
# Rational(2).fdiv(3) #=> 0.6666666666666666
#
def fdiv: (Numeric) -> Float
def fdiv: (Integer | Rational | Float | Complex other) -> Float
| [S] ((Numeric::_Coerce[self, RBS::Ops::_Times[S, _ToF], S] & RBS::Ops::_Equal[0]) other) -> Float

# <!--
# rdoc-file=rational.c
Expand All @@ -288,7 +296,7 @@
# Rational('-123.456').floor(-1) #=> -130
#
def floor: () -> Integer
| (Integer digits) -> (Integer | Rational)
| (Integer ndigits) -> (Integer | instance)

# <!--
# rdoc-file=rational.c
Expand Down Expand Up @@ -360,9 +368,7 @@
# Rational(9, 8) / 4 #=> (9/32)
# Rational(20, 9) / 9.8 #=> 0.22675736961451246
#
def quo: (Float) -> Float
| (Complex) -> Complex
| (Numeric) -> Rational
alias quo /

# <!--
# rdoc-file=rational.c
Expand All @@ -377,7 +383,8 @@
# r.rationalize(Rational('0.01')) #=> (3/10)
# r.rationalize(Rational('0.1')) #=> (1/3)
#
def rationalize: (?Numeric eps) -> Rational
def rationalize: () -> self
| (Numeric eps) -> instance # technically has a more specific one, but it yields a segfault.

def rect: () -> [ Rational, Numeric ]

Expand Down Expand Up @@ -419,8 +426,8 @@
# Rational(-25, 100).round(1, half: :down) #=> (-1/5)
# Rational(-25, 100).round(1, half: :even) #=> (-1/5)
#
def round: (?half: :up | :down | :even) -> Integer
| (Integer digits, ?half: :up | :down | :even) -> (Integer | Rational)
def round: (?half: Numeric::round_mode) -> Integer
| (Integer digits, ?half: Numeric::round_mode) -> (Integer | Rational)

# <!--
# rdoc-file=rational.c
Expand Down Expand Up @@ -460,7 +467,7 @@
# Rational(2).to_r #=> (2/1)
# Rational(-8, 6).to_r #=> (-4/3)
#
def to_r: () -> Rational
def to_r: () -> self

# <!--
# rdoc-file=rational.c
Expand Down Expand Up @@ -498,5 +505,7 @@
# Rational('-123.456').truncate(-1) #=> -120
#
def truncate: () -> Integer
| (Integer ndigits) -> (Integer | Rational)
| (Integer ndigits) -> (Integer | instance)

private def marshal_dump: () -> [ Integer, Integer ]
end
Loading
Loading