2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2021-01-06 00:41:13 -05:00
|
|
|
require_relative "helper"
|
2011-07-26 11:40:27 -04:00
|
|
|
require 'bigdecimal/util'
|
|
|
|
|
|
|
|
class TestBigDecimalUtil < Test::Unit::TestCase
|
2021-01-06 00:41:13 -05:00
|
|
|
include TestBigDecimalBase
|
|
|
|
|
2011-07-26 11:40:27 -04:00
|
|
|
def test_BigDecimal_to_d
|
|
|
|
x = BigDecimal(1)
|
|
|
|
assert_same(x, x.to_d)
|
|
|
|
end
|
2011-07-26 11:46:42 -04:00
|
|
|
|
|
|
|
def test_Integer_to_d
|
|
|
|
assert_equal(BigDecimal(1), 1.to_d)
|
|
|
|
assert_equal(BigDecimal(2<<100), (2<<100).to_d)
|
2018-12-05 06:30:24 -05:00
|
|
|
|
|
|
|
assert(1.to_d.frozen?)
|
2011-07-26 11:46:42 -04:00
|
|
|
end
|
2011-07-26 11:48:25 -04:00
|
|
|
|
|
|
|
def test_Float_to_d_without_precision
|
2020-12-18 09:46:26 -05:00
|
|
|
delta = 1.0/10**(Float::DIG+1)
|
2021-01-12 20:28:23 -05:00
|
|
|
assert_in_delta(BigDecimal(0.5, 0), 0.5.to_d, delta)
|
|
|
|
assert_in_delta(BigDecimal(355.0/113.0, 0), (355.0/113.0).to_d, delta)
|
2020-12-18 09:46:26 -05:00
|
|
|
|
|
|
|
assert_equal(9.05, 9.05.to_d.to_f)
|
2021-01-12 20:28:23 -05:00
|
|
|
assert_equal("9.05", 9.05.to_d.to_s('F'))
|
2013-12-05 11:34:59 -05:00
|
|
|
|
2021-10-27 04:31:45 -04:00
|
|
|
assert_equal("65.6", 65.6.to_d.to_s("F"))
|
|
|
|
|
2021-01-08 07:00:45 -05:00
|
|
|
assert_equal(Math::PI, Math::PI.to_d.to_f)
|
|
|
|
|
2013-12-05 11:34:59 -05:00
|
|
|
bug9214 = '[ruby-core:58858]'
|
|
|
|
assert_equal((-0.0).to_d.sign, -1, bug9214)
|
2018-12-05 06:30:24 -05:00
|
|
|
|
|
|
|
assert_raise(TypeError) { 0.3.to_d(nil) }
|
|
|
|
assert_raise(TypeError) { 0.3.to_d(false) }
|
|
|
|
|
|
|
|
assert(1.1.to_d.frozen?)
|
2021-01-12 20:28:23 -05:00
|
|
|
|
|
|
|
assert_equal(BigDecimal("999_999.9999"), 999_999.9999.to_d)
|
2011-07-26 11:48:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_Float_to_d_with_precision
|
|
|
|
digits = 5
|
|
|
|
delta = 1.0/10**(digits)
|
|
|
|
assert_in_delta(BigDecimal(0.5, 5), 0.5.to_d(digits), delta)
|
|
|
|
assert_in_delta(BigDecimal(355.0/113.0, 5), (355.0/113.0).to_d(digits), delta)
|
2013-12-05 11:34:59 -05:00
|
|
|
|
|
|
|
bug9214 = '[ruby-core:58858]'
|
|
|
|
assert_equal((-0.0).to_d(digits).sign, -1, bug9214)
|
2018-12-05 06:30:24 -05:00
|
|
|
|
|
|
|
assert(1.1.to_d(digits).frozen?)
|
2011-07-26 11:48:25 -04:00
|
|
|
end
|
2011-07-26 11:49:47 -04:00
|
|
|
|
2020-12-18 09:46:26 -05:00
|
|
|
def test_Float_to_d_bug13331
|
|
|
|
assert_equal(64.4.to_d,
|
|
|
|
1.to_d * 64.4,
|
|
|
|
"[ruby-core:80234] [Bug #13331]")
|
2021-01-08 07:00:45 -05:00
|
|
|
|
|
|
|
assert_equal((2*Math::PI).to_d,
|
|
|
|
2.to_d * Math::PI,
|
|
|
|
"[ruby-core:80234] [Bug #13331]")
|
2020-12-18 09:46:26 -05:00
|
|
|
end
|
|
|
|
|
2021-10-27 04:31:45 -04:00
|
|
|
def test_Float_to_d_issue_192
|
|
|
|
# https://github.com/ruby/bigdecimal/issues/192
|
|
|
|
# https://github.com/rails/rails/pull/42125
|
|
|
|
if BASE_FIG == 9
|
|
|
|
flo = 1_000_000_000.12345
|
|
|
|
big = BigDecimal("0.100000000012345e10")
|
|
|
|
else # BASE_FIG == 4
|
|
|
|
flo = 1_0000.12
|
|
|
|
big = BigDecimal("0.1000012e5")
|
|
|
|
end
|
|
|
|
assert_equal(flo.to_d, big, "[ruby/bigdecimal#192]")
|
|
|
|
end
|
|
|
|
|
2011-07-26 11:49:47 -04:00
|
|
|
def test_Rational_to_d
|
|
|
|
digits = 100
|
|
|
|
delta = 1.0/10**(digits)
|
|
|
|
assert_in_delta(BigDecimal(1.quo(2), digits), 1.quo(2).to_d(digits), delta)
|
|
|
|
assert_in_delta(BigDecimal(355.quo(113), digits), 355.quo(113).to_d(digits), delta)
|
2018-12-05 06:30:24 -05:00
|
|
|
|
|
|
|
assert(355.quo(113).to_d(digits).frozen?)
|
2011-07-26 11:49:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_Rational_to_d_with_zero_precision
|
2017-03-13 02:03:18 -04:00
|
|
|
assert_equal(BigDecimal(355.quo(113), 0), 355.quo(113).to_d(0))
|
2011-07-26 11:49:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_Rational_to_d_with_negative_precision
|
|
|
|
assert_raise(ArgumentError) { 355.quo(113).to_d(-42) }
|
|
|
|
end
|
2017-02-10 08:23:58 -05:00
|
|
|
|
2019-10-08 21:39:39 -04:00
|
|
|
def test_Complex_to_d
|
2020-07-12 04:30:29 -04:00
|
|
|
BigDecimal.save_rounding_mode do
|
|
|
|
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_EVEN)
|
2019-10-08 21:39:39 -04:00
|
|
|
|
2020-07-12 04:30:29 -04:00
|
|
|
assert_equal(BigDecimal("1"), Complex(1, 0).to_d)
|
|
|
|
assert_equal(BigDecimal("0.333333333333333333333"),
|
|
|
|
Complex(1.quo(3), 0).to_d(21))
|
|
|
|
assert_equal(BigDecimal("0.1234567"), Complex(0.1234567, 0).to_d)
|
|
|
|
assert_equal(BigDecimal("0.1235"), Complex(0.1234567, 0).to_d(4))
|
2019-10-08 21:39:39 -04:00
|
|
|
|
2020-07-12 04:30:29 -04:00
|
|
|
assert_raise_with_message(ArgumentError, "can't omit precision for a Rational.") { Complex(1.quo(3), 0).to_d }
|
|
|
|
|
|
|
|
assert_raise_with_message(ArgumentError, "Unable to make a BigDecimal from non-zero imaginary number") { Complex(1, 1).to_d }
|
|
|
|
end
|
2019-10-08 21:39:39 -04:00
|
|
|
end
|
|
|
|
|
2017-02-10 08:23:58 -05:00
|
|
|
def test_String_to_d
|
2018-12-05 06:30:24 -05:00
|
|
|
assert_equal(BigDecimal('1'), "1__1_1".to_d)
|
|
|
|
assert_equal(BigDecimal('2.5'), "2.5".to_d)
|
|
|
|
assert_equal(BigDecimal('2.5'), "2.5 degrees".to_d)
|
|
|
|
assert_equal(BigDecimal('2.5e1'), "2.5e1 degrees".to_d)
|
|
|
|
assert_equal(BigDecimal('0'), "degrees 100.0".to_d)
|
|
|
|
assert_equal(BigDecimal('0.125'), "0.1_2_5".to_d)
|
|
|
|
assert_equal(BigDecimal('0.125'), "0.1_2_5__".to_d)
|
|
|
|
assert_equal(BigDecimal('1'), "1_.125".to_d)
|
|
|
|
assert_equal(BigDecimal('1'), "1._125".to_d)
|
|
|
|
assert_equal(BigDecimal('0.1'), "0.1__2_5".to_d)
|
|
|
|
assert_equal(BigDecimal('0.1'), "0.1_e10".to_d)
|
|
|
|
assert_equal(BigDecimal('0.1'), "0.1e_10".to_d)
|
|
|
|
assert_equal(BigDecimal('1'), "0.1e1__0".to_d)
|
2018-12-20 18:29:49 -05:00
|
|
|
assert_equal(BigDecimal('1.2'), "1.2.3".to_d)
|
2019-10-07 20:06:28 -04:00
|
|
|
assert_equal(BigDecimal('1'), "1.".to_d)
|
|
|
|
assert_equal(BigDecimal('1'), "1e".to_d)
|
2018-12-05 06:30:24 -05:00
|
|
|
|
|
|
|
assert("2.5".to_d.frozen?)
|
2017-02-10 08:23:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_String_to_d
|
2017-12-13 10:28:30 -05:00
|
|
|
assert_equal("invalid".to_d, BigDecimal('0.0'))
|
2017-02-10 08:23:58 -05:00
|
|
|
end
|
|
|
|
|
2018-12-05 06:30:24 -05:00
|
|
|
def test_Nil_to_d
|
|
|
|
assert_equal(nil.to_d, BigDecimal('0.0'))
|
|
|
|
|
|
|
|
assert(nil.to_d)
|
|
|
|
end
|
2011-07-26 11:40:27 -04:00
|
|
|
end
|