From 81887f1c74a942e83c7a1d841c78c46f233a963a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Tue, 12 Jul 2022 13:05:30 +0200 Subject: [PATCH 1/2] Add overloads for math operation with rhs being BigDecimal --- stdlib/bigdecimal/0/big_decimal.rbs | 49 ++++++++++++++++++ test/stdlib/BigDecimal_test.rb | 80 +++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/stdlib/bigdecimal/0/big_decimal.rbs b/stdlib/bigdecimal/0/big_decimal.rbs index 7b79a8816..d985ab30f 100644 --- a/stdlib/bigdecimal/0/big_decimal.rbs +++ b/stdlib/bigdecimal/0/big_decimal.rbs @@ -1408,6 +1408,18 @@ class Integer # See also BigDecimal::new. # def to_d: () -> BigDecimal + + def /: (BigDecimal) -> BigDecimal + | ... + + def *: (BigDecimal) -> BigDecimal + | ... + + def +: (BigDecimal) -> BigDecimal + | ... + + def -: (BigDecimal) -> BigDecimal + | ... end %a{annotate:rdoc:skip} @@ -1430,6 +1442,18 @@ class Float # See also BigDecimal::new. # def to_d: (?Integer precision) -> BigDecimal + + def /: (BigDecimal) -> BigDecimal + | ... + + def *: (BigDecimal) -> BigDecimal + | ... + + def +: (BigDecimal) -> BigDecimal + | ... + + def -: (BigDecimal) -> BigDecimal + | ... end %a{annotate:rdoc:skip} @@ -1472,6 +1496,18 @@ class Rational # See also BigDecimal::new. # def to_d: (Integer precision) -> BigDecimal + + def /: (BigDecimal) -> BigDecimal + | ... + + def *: (BigDecimal) -> BigDecimal + | ... + + def +: (BigDecimal) -> BigDecimal + | ... + + def -: (BigDecimal) -> BigDecimal + | ... end %a{annotate:rdoc:skip} @@ -1496,6 +1532,19 @@ class Complex # See also BigDecimal::new. # def to_d: (*untyped args) -> BigDecimal + + + def /: (BigDecimal) -> Complex + | ... + + def *: (BigDecimal) -> Complex + | ... + + def +: (BigDecimal) -> Complex + | ... + + def -: (BigDecimal) -> Complex + | ... end %a{annotate:rdoc:skip} diff --git a/test/stdlib/BigDecimal_test.rb b/test/stdlib/BigDecimal_test.rb index 3ed177bb0..60c9d62c5 100644 --- a/test/stdlib/BigDecimal_test.rb +++ b/test/stdlib/BigDecimal_test.rb @@ -373,6 +373,26 @@ class IntegerToBigDecimalTest < Test::Unit::TestCase def test_to_d_with_integer assert_send_type "() -> ::BigDecimal", 123, :to_d end + + def test_plus_with_integer + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123, :+, BigDecimal("1.23") + end + + def test_minus_with_integer + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123, :-, BigDecimal("1.23") + end + + def test_divide_with_integer + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123, :/, BigDecimal("1.23") + end + + def test_multiply_with_integer + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123, :*, BigDecimal("1.23") + end end class FloatToBigDecimalTest < Test::Unit::TestCase @@ -384,6 +404,26 @@ class FloatToBigDecimalTest < Test::Unit::TestCase def test_to_d_with_float assert_send_type "() -> ::BigDecimal", 12.3, :to_d end + + def test_plus_with_float + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 1.23, :+, BigDecimal("1.23") + end + + def test_minus_with_float + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 1.23, :-, BigDecimal("1.23") + end + + def test_divide_with_float + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 1.23, :/, BigDecimal("1.23") + end + + def test_multiply_with_float + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 1.23, :*, BigDecimal("1.23") + end end class StringToBigDecimalTest < Test::Unit::TestCase @@ -406,6 +446,26 @@ class RationalToBigDecimalTest < Test::Unit::TestCase def test_to_d_with_rational assert_send_type "(Integer) -> ::BigDecimal", Rational(22, 7), :to_d, 3 end + + def test_plus_with_rational + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123r, :+, BigDecimal("1.23") + end + + def test_minus_with_rational + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123r, :-, BigDecimal("1.23") + end + + def test_divide_with_rational + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123r, :/, BigDecimal("1.23") + end + + def test_multiply_with_rational + assert_send_type "(::BigDecimal) -> ::BigDecimal", + 123r, :*, BigDecimal("1.23") + end end class ComplexToBigDecimalTest < Test::Unit::TestCase @@ -417,6 +477,26 @@ class ComplexToBigDecimalTest < Test::Unit::TestCase def test_to_d_with_complex assert_send_type "() -> ::BigDecimal", Complex(0.1234567, 0), :to_d end + + def test_plus_with_complex + assert_send_type "(::BigDecimal) -> ::Complex", + Complex(0.1234567, 0), :+, BigDecimal("1.23") + end + + def test_minus_with_complex + assert_send_type "(::BigDecimal) -> ::Complex", + Complex(0.1234567, 0), :-, BigDecimal("1.23") + end + + def test_divide_with_complex + assert_send_type "(::BigDecimal) -> ::Complex", + Complex(0.1234567, 0), :/, BigDecimal("1.23") + end + + def test_multiply_with_complex + assert_send_type "(::BigDecimal) -> ::Complex", + Complex(0.1234567, 0), :*, BigDecimal("1.23") + end end class NilToBigDecimalTest < Test::Unit::TestCase From 51da34f811fcac7392c9b6a1c607a9f353203ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Tue, 12 Jul 2022 13:08:21 +0200 Subject: [PATCH 2/2] rake annotate --- stdlib/bigdecimal/0/big_decimal.rbs | 203 +++++++++++++++++++++++++++- 1 file changed, 202 insertions(+), 1 deletion(-) diff --git a/stdlib/bigdecimal/0/big_decimal.rbs b/stdlib/bigdecimal/0/big_decimal.rbs index d985ab30f..5de9c4c21 100644 --- a/stdlib/bigdecimal/0/big_decimal.rbs +++ b/stdlib/bigdecimal/0/big_decimal.rbs @@ -1409,15 +1409,71 @@ class Integer # def to_d: () -> BigDecimal + # + # Performs division; for integer `numeric`, truncates the result to an integer: + # + # 4 / 3 # => 1 + # 4 / -3 # => -2 + # -4 / 3 # => -2 + # -4 / -3 # => 1 + # + # For other +numeric+, returns non-integer result: + # + # 4 / 3.0 # => 1.3333333333333333 + # 4 / Rational(3, 1) # => (4/3) + # 4 / Complex(3, 0) # => ((4/3)+0i) + # def /: (BigDecimal) -> BigDecimal | ... + # + # Performs multiplication: + # + # 4 * 2 # => 8 + # 4 * -2 # => -8 + # -4 * 2 # => -8 + # 4 * 2.0 # => 8.0 + # 4 * Rational(1, 3) # => (4/3) + # 4 * Complex(2, 0) # => (8+0i) + # def *: (BigDecimal) -> BigDecimal | ... + # + # Performs addition: + # + # 2 + 2 # => 4 + # -2 + 2 # => 0 + # -2 + -2 # => -4 + # 2 + 2.0 # => 4.0 + # 2 + Rational(2, 1) # => (4/1) + # 2 + Complex(2, 0) # => (4+0i) + # def +: (BigDecimal) -> BigDecimal | ... + # + # Performs subtraction: + # + # 4 - 2 # => 2 + # -4 - 2 # => -6 + # -4 - -2 # => -2 + # 4 - 2.0 # => 2.0 + # 4 - Rational(2, 1) # => (2/1) + # 4 - Complex(2, 0) # => (2+0i) + # def -: (BigDecimal) -> BigDecimal | ... end @@ -1443,15 +1499,63 @@ class Float # def to_d: (?Integer precision) -> BigDecimal + # + # Returns a new Float which is the result of dividing `self` by `other`: + # + # f = 3.14 + # f / 2 # => 1.57 + # f / 2.0 # => 1.57 + # f / Rational(2, 1) # => 1.57 + # f / Complex(2, 0) # => (1.57+0.0i) + # def /: (BigDecimal) -> BigDecimal | ... + # + # Returns a new Float which is the product of `self` and `other`: + # + # f = 3.14 + # f * 2 # => 6.28 + # f * 2.0 # => 6.28 + # f * Rational(1, 2) # => 1.57 + # f * Complex(2, 0) # => (6.28+0.0i) + # def *: (BigDecimal) -> BigDecimal | ... + # + # Returns a new Float which is the sum of `self` and `other`: + # + # f = 3.14 + # f + 1 # => 4.140000000000001 + # f + 1.0 # => 4.140000000000001 + # f + Rational(1, 1) # => 4.140000000000001 + # f + Complex(1, 0) # => (4.140000000000001+0i) + # def +: (BigDecimal) -> BigDecimal | ... + # + # Returns a new Float which is the difference of `self` and `other`: + # + # f = 3.14 + # f - 1 # => 2.14 + # f - 1.0 # => 2.14 + # f - Rational(1, 1) # => 2.14 + # f - Complex(1, 0) # => (2.14+0i) + # def -: (BigDecimal) -> BigDecimal | ... end @@ -1497,15 +1601,64 @@ class Rational # def to_d: (Integer precision) -> BigDecimal + # + # Performs division. + # + # Rational(2, 3) / Rational(2, 3) #=> (1/1) + # Rational(900) / Rational(1) #=> (900/1) + # Rational(-2, 9) / Rational(-9, 2) #=> (4/81) + # Rational(9, 8) / 4 #=> (9/32) + # Rational(20, 9) / 9.8 #=> 0.22675736961451246 + # def /: (BigDecimal) -> BigDecimal | ... + # + # Performs multiplication. + # + # Rational(2, 3) * Rational(2, 3) #=> (4/9) + # Rational(900) * Rational(1) #=> (900/1) + # Rational(-2, 9) * Rational(-9, 2) #=> (1/1) + # Rational(9, 8) * 4 #=> (9/2) + # Rational(20, 9) * 9.8 #=> 21.77777777777778 + # def *: (BigDecimal) -> BigDecimal | ... + # + # Performs addition. + # + # Rational(2, 3) + Rational(2, 3) #=> (4/3) + # Rational(900) + Rational(1) #=> (901/1) + # Rational(-2, 9) + Rational(-9, 2) #=> (-85/18) + # Rational(9, 8) + 4 #=> (41/8) + # Rational(20, 9) + 9.8 #=> 12.022222222222222 + # def +: (BigDecimal) -> BigDecimal | ... + # + # Performs subtraction. + # + # Rational(2, 3) - Rational(2, 3) #=> (0/1) + # Rational(900) - Rational(1) #=> (899/1) + # Rational(-2, 9) - Rational(-9, 2) #=> (77/18) + # Rational(9, 8) - 4 #=> (-23/8) + # Rational(20, 9) - 9.8 #=> -7.577777777777778 + # def -: (BigDecimal) -> BigDecimal | ... end @@ -1533,16 +1686,64 @@ class Complex # def to_d: (*untyped args) -> BigDecimal - + # + # Performs division. + # + # Complex(2, 3) / Complex(2, 3) #=> ((1/1)+(0/1)*i) + # Complex(900) / Complex(1) #=> ((900/1)+(0/1)*i) + # Complex(-2, 9) / Complex(-9, 2) #=> ((36/85)-(77/85)*i) + # Complex(9, 8) / 4 #=> ((9/4)+(2/1)*i) + # Complex(20, 9) / 9.8 #=> (2.0408163265306123+0.9183673469387754i) + # def /: (BigDecimal) -> Complex | ... + # + # Performs multiplication. + # + # Complex(2, 3) * Complex(2, 3) #=> (-5+12i) + # Complex(900) * Complex(1) #=> (900+0i) + # Complex(-2, 9) * Complex(-9, 2) #=> (0-85i) + # Complex(9, 8) * 4 #=> (36+32i) + # Complex(20, 9) * 9.8 #=> (196.0+88.2i) + # def *: (BigDecimal) -> Complex | ... + # + # Performs addition. + # + # Complex(2, 3) + Complex(2, 3) #=> (4+6i) + # Complex(900) + Complex(1) #=> (901+0i) + # Complex(-2, 9) + Complex(-9, 2) #=> (-11+11i) + # Complex(9, 8) + 4 #=> (13+8i) + # Complex(20, 9) + 9.8 #=> (29.8+9i) + # def +: (BigDecimal) -> Complex | ... + # + # Performs subtraction. + # + # Complex(2, 3) - Complex(2, 3) #=> (0+0i) + # Complex(900) - Complex(1) #=> (899+0i) + # Complex(-2, 9) - Complex(-9, 2) #=> (7+7i) + # Complex(9, 8) - 4 #=> (5+8i) + # Complex(20, 9) - 9.8 #=> (10.2+9i) + # def -: (BigDecimal) -> Complex | ... end