mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* lib/rational.rb (floor, ceil, truncate, round): do not use
definitions of Numeric.
	* lib/rational.rb (to_i): should returns truncated self.
	* lib/complex.rb (numerator): requires
	  Integer#{numerator,denominator}.
	* lib/complex.rb (quo): do not use definition of Numeric.
	* lib/complex.rb (>, >=, <, <=, between?, div, divmod, modulo,
	  floor, ceil, truncate, round): undef'ed.
	* lib/mathn.rb (Rational#inspect): removed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
			
			
This commit is contained in:
		
							parent
							
								
									7d4defbe1b
								
							
						
					
					
						commit
						2b72892ac0
					
				
					 4 changed files with 123 additions and 45 deletions
				
			
		
							
								
								
									
										17
									
								
								ChangeLog
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								ChangeLog
									
										
									
									
									
								
							|  | @ -1,3 +1,20 @@ | |||
| Tue Feb 12 20:32:50 2008  Tadayoshi Funaba  <tadf@dotrb.org> | ||||
| 
 | ||||
| 	* lib/rational.rb (floor, ceil, truncate, round): do not use | ||||
| 	  definitions of Numeric. | ||||
| 
 | ||||
| 	* lib/rational.rb (to_i): should returns truncated self. | ||||
| 
 | ||||
| 	* lib/complex.rb (numerator): requires | ||||
| 	  Integer#{numerator,denominator}. | ||||
| 
 | ||||
| 	* lib/complex.rb (quo): do not use definition of Numeric. | ||||
| 
 | ||||
| 	* lib/complex.rb (>, >=, <, <=, between?, div, divmod, modulo, | ||||
| 	  floor, ceil, truncate, round): undef'ed. | ||||
| 
 | ||||
| 	* lib/mathn.rb (Rational#inspect): removed. | ||||
| 
 | ||||
| Tue Feb 12 16:48:10 2008  Nobuyoshi Nakada  <nobu@ruby-lang.org> | ||||
| 
 | ||||
| 	* parse.y (args, mrhs): flattens literal array splats. | ||||
|  |  | |||
|  | @ -104,6 +104,10 @@ class Complex < Numeric | |||
|   @RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-' | ||||
| 
 | ||||
|   undef step | ||||
|   undef <, <=, <=>, >, >= | ||||
|   undef between? | ||||
|   undef div, divmod, modulo | ||||
|   undef floor, truncate, ceil, round | ||||
| 
 | ||||
|   def scalar? | ||||
|     false | ||||
|  | @ -199,6 +203,10 @@ class Complex < Numeric | |||
|       x/y | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def quo(other) | ||||
|     Complex(@real.quo(1), @image.quo(1)) / other | ||||
|   end | ||||
|    | ||||
|   # | ||||
|   # Raise this complex number to the given (real or complex) power. | ||||
|  | @ -248,6 +256,8 @@ class Complex < Numeric | |||
|   # | ||||
|   # Remainder after division by a real or complex number. | ||||
|   # | ||||
| 
 | ||||
| =begin | ||||
|   def % (other) | ||||
|     if other.kind_of?(Complex) | ||||
|       Complex(@real % other.real, @image % other.image) | ||||
|  | @ -258,7 +268,8 @@ class Complex < Numeric | |||
|       x % y | ||||
|     end | ||||
|   end | ||||
|    | ||||
| =end | ||||
| 
 | ||||
| #-- | ||||
| #    def divmod(other) | ||||
| #      if other.kind_of?(Complex) | ||||
|  | @ -312,8 +323,6 @@ class Complex < Numeric | |||
|   end | ||||
|   alias conj conjugate | ||||
| 
 | ||||
|   undef <=> | ||||
|    | ||||
|   # | ||||
|   # Test for numerical equality (<tt>a == a + 0<i>i</i></tt>). | ||||
|   # | ||||
|  | @ -410,8 +419,34 @@ class Complex < Numeric | |||
|    | ||||
| end | ||||
| 
 | ||||
| class Integer | ||||
| 
 | ||||
|   unless defined?(1.numerator) | ||||
|     def numerator() self end | ||||
|     def denominator() 1 end | ||||
| 
 | ||||
|     def gcd(other) | ||||
|       min = self.abs | ||||
|       max = other.abs | ||||
|       while min > 0 | ||||
|         tmp = min | ||||
|         min = max % min | ||||
|         max = tmp | ||||
|       end | ||||
|       max | ||||
|     end | ||||
| 
 | ||||
|     def lcm(other) | ||||
|       if self.zero? or other.zero? | ||||
|         0 | ||||
|       else | ||||
|         (self.div(self.gcd(other)) * other).abs | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|   end | ||||
| 
 | ||||
| end | ||||
| 
 | ||||
| module Math | ||||
|   alias sqrt! sqrt | ||||
|  |  | |||
|  | @ -121,11 +121,6 @@ end | |||
| class Rational | ||||
|   Unify = true | ||||
| 
 | ||||
|   remove_method :inspect | ||||
|   def inspect | ||||
|     format "%s/%s", numerator.inspect, denominator.inspect | ||||
|   end | ||||
| 
 | ||||
|   alias power! ** | ||||
| 
 | ||||
|   def ** (other) | ||||
|  |  | |||
							
								
								
									
										105
									
								
								lib/rational.rb
									
										
									
									
									
								
							
							
						
						
									
										105
									
								
								lib/rational.rb
									
										
									
									
									
								
							|  | @ -238,6 +238,10 @@ class Rational < Numeric | |||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def div(other) | ||||
|     (self / other).floor | ||||
|   end | ||||
| 
 | ||||
|   # | ||||
|   # Returns the remainder when this value is divided by +other+. | ||||
|   # | ||||
|  | @ -249,7 +253,7 @@ class Rational < Numeric | |||
|   #   r % 0.26             # -> 0.19 | ||||
|   # | ||||
|   def % (other) | ||||
|     value = (self / other).to_i | ||||
|     value = (self / other).floor | ||||
|     return self - other * value | ||||
|   end | ||||
| 
 | ||||
|  | @ -261,7 +265,7 @@ class Rational < Numeric | |||
|   #   r.divmod Rational(1,2)   # -> [3, Rational(1,4)] | ||||
|   # | ||||
|   def divmod(other) | ||||
|     value = (self / other).to_i | ||||
|     value = (self / other).floor | ||||
|     return value, self - other * value | ||||
|   end | ||||
| 
 | ||||
|  | @ -270,7 +274,7 @@ class Rational < Numeric | |||
|   # | ||||
|   def abs | ||||
|     if @numerator > 0 | ||||
|       Rational.new!(@numerator, @denominator) | ||||
|       self | ||||
|     else | ||||
|       Rational.new!(-@numerator, @denominator) | ||||
|     end | ||||
|  | @ -345,8 +349,35 @@ class Rational < Numeric | |||
|   #   Rational(-7,4) == -1.75                 # -> true | ||||
|   #   Rational(-7,4).to_i == (-1.75).to_i     # false | ||||
|   # | ||||
|   def to_i | ||||
|     Integer(@numerator.div(@denominator)) | ||||
| 
 | ||||
|   def floor() | ||||
|     @numerator.div(@denominator) | ||||
|   end | ||||
| 
 | ||||
|   def ceil() | ||||
|     -((-@numerator).div(@denominator)) | ||||
|   end | ||||
| 
 | ||||
|   def truncate() | ||||
|     if @numerator < 0 | ||||
|       return -((-@numerator).div(@denominator)) | ||||
|     end | ||||
|     @numerator.div(@denominator) | ||||
|   end | ||||
| 
 | ||||
|   alias_method :to_i, :truncate | ||||
| 
 | ||||
|   def round() | ||||
|     if @numerator < 0 | ||||
|       num = -@numerator | ||||
|       num = num * 2 + @denominator | ||||
|       den = @denominator * 2 | ||||
|       -(num.div(den)) | ||||
|     else | ||||
|       num = @numerator * 2 + @denominator | ||||
|       den = @denominator * 2 | ||||
|       num.div(den) | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   # | ||||
|  | @ -476,38 +507,11 @@ end | |||
| 
 | ||||
| class Fixnum | ||||
|   alias quof quo | ||||
|   undef quo | ||||
|   # If Rational is defined, returns a Rational number instead of a Fixnum. | ||||
|   remove_method :quo | ||||
| 
 | ||||
|   # If Rational is defined, returns a Rational number instead of a Float. | ||||
|   def quo(other) | ||||
|     Rational.new!(self,1) / other | ||||
|   end | ||||
|   alias rdiv quo | ||||
| 
 | ||||
|   # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0). | ||||
|   def rpower (other) | ||||
|     if other >= 0 | ||||
|       self.power!(other) | ||||
|     else | ||||
|       Rational.new!(self,1)**other | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   unless defined? 1.power! | ||||
|     alias power! ** | ||||
|     alias ** rpower | ||||
|   end | ||||
| end | ||||
| 
 | ||||
| class Bignum | ||||
|   unless defined? Complex | ||||
|     alias power! ** | ||||
|   end | ||||
| 
 | ||||
|   alias quof quo | ||||
|   undef quo | ||||
|   # If Rational is defined, returns a Rational number instead of a Bignum. | ||||
|   def quo(other) | ||||
|     Rational.new!(self,1) / other | ||||
|     Rational.new!(self, 1) / other | ||||
|   end | ||||
|   alias rdiv quo | ||||
| 
 | ||||
|  | @ -519,8 +523,35 @@ class Bignum | |||
|       Rational.new!(self, 1)**other | ||||
|     end | ||||
|   end | ||||
| end | ||||
| 
 | ||||
|   unless defined? Complex | ||||
| class Bignum | ||||
|   alias quof quo | ||||
|   remove_method :quo | ||||
| 
 | ||||
|   # If Rational is defined, returns a Rational number instead of a Float. | ||||
|   def quo(other) | ||||
|     Rational.new!(self, 1) / other | ||||
|   end | ||||
|   alias rdiv quo | ||||
| 
 | ||||
|   # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0). | ||||
|   def rpower (other) | ||||
|     if other >= 0 | ||||
|       self.power!(other) | ||||
|     else | ||||
|       Rational.new!(self, 1)**other | ||||
|     end | ||||
|   end | ||||
| end | ||||
| 
 | ||||
| unless defined? 1.power! | ||||
|   class Fixnum | ||||
|     alias power! ** | ||||
|     alias ** rpower | ||||
|   end | ||||
|   class Bignum | ||||
|     alias power! ** | ||||
|     alias ** rpower | ||||
|   end | ||||
| end | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 tadf
						tadf