mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	
							parent
							
								
									6607212224
								
							
						
					
					
						commit
						03a33603c6
					
				
				
				Notes:
				
					git
				
				2020-07-06 08:56:28 +09:00 
				
			
			
			
		
		
					 2 changed files with 45 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -131,6 +131,39 @@ class Rational < Numeric
 | 
			
		|||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Complex < Numeric
 | 
			
		||||
  # call-seq:
 | 
			
		||||
  #     cmp.to_d             -> bigdecimal
 | 
			
		||||
  #     cmp.to_d(precision)  -> bigdecimal
 | 
			
		||||
  #
 | 
			
		||||
  # Returns the value as a BigDecimal.
 | 
			
		||||
  #
 | 
			
		||||
  # The +precision+ parameter is required for a rational complex number.
 | 
			
		||||
  # This parameter is used to determine the number of significant digits
 | 
			
		||||
  # for the result.
 | 
			
		||||
  #
 | 
			
		||||
  #     require 'bigdecimal'
 | 
			
		||||
  #     require 'bigdecimal/util'
 | 
			
		||||
  #
 | 
			
		||||
  #     Complex(0.1234567, 0).to_d(4)   # => 0.1235e0
 | 
			
		||||
  #     Complex(Rational(22, 7), 0).to_d(3)   # => 0.314e1
 | 
			
		||||
  #
 | 
			
		||||
  # See also BigDecimal::new.
 | 
			
		||||
  #
 | 
			
		||||
  def to_d(*args)
 | 
			
		||||
    BigDecimal(self) unless self.imag.zero? # to raise eerror
 | 
			
		||||
 | 
			
		||||
    if args.length == 0
 | 
			
		||||
      case self.real
 | 
			
		||||
      when Rational
 | 
			
		||||
        BigDecimal(self.real) # to raise error
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
    self.real.to_d(*args)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class NilClass
 | 
			
		||||
  # call-seq:
 | 
			
		||||
  #     nil.to_d -> bigdecimal
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -60,6 +60,18 @@ class TestBigDecimalUtil < Test::Unit::TestCase
 | 
			
		|||
    assert_raise(ArgumentError) { 355.quo(113).to_d(-42) }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_Complex_to_d
 | 
			
		||||
    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))
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
  def test_String_to_d
 | 
			
		||||
    assert_equal(BigDecimal('1'), "1__1_1".to_d)
 | 
			
		||||
    assert_equal(BigDecimal('2.5'), "2.5".to_d)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue