## Bug #10086 ยป Remove_Rational#power_from_mathn.patch

View differences:

lib/mathn.rb
77 77
`end`
78 78

79 79
`##`
80
`# When mathn is required Rational is changed to simplify the use of Rational`
81
`# operations.`
82
`#`
83
`# Normal behaviour:`
84
`#`
85
`#   Rational.new!(1,3) ** 2 # => Rational(1, 9)`
86
`#   (1 / 3) ** 2            # => 0`
87
`#`
88
`# require 'mathn' behaviour:`
89
`#`
90
`#   (1 / 3) ** 2            # => 1/9`
91

92
`class Rational`
93
`  remove_method :**`
94

95
`  ##`
96
`  # Exponentiate by +other+`
97
`  #`
98
`  #   (1/3) ** 2 # => 1/9`
99

100
`  def ** (other)`
101
`    if other.kind_of?(Rational)`
102
`      other2 = other`
103
`      if self < 0`
104
`        return Complex(self, 0.0) ** other`
105
`      elsif other == 0`
106
`        return Rational(1,1)`
107
`      elsif self == 0`
108
`        return Rational(0,1)`
109
`      elsif self == 1`
110
`        return Rational(1,1)`
111
`      end`
112

113
`      npd = numerator.prime_division`
114
`      dpd = denominator.prime_division`
115
`      if other < 0`
116
`        other = -other`
117
`        npd, dpd = dpd, npd`
118
`      end`
119

120
`      for elm in npd`
121
`        elm[1] = elm[1] * other`
122
`        if !elm[1].kind_of?(Integer) and elm[1].denominator != 1`
123
`          return Float(self) ** other2`
124
`        end`
125
`        elm[1] = elm[1].to_i`
126
`      end`
127

128
`      for elm in dpd`
129
`        elm[1] = elm[1] * other`
130
`        if !elm[1].kind_of?(Integer) and elm[1].denominator != 1`
131
`          return Float(self) ** other2`
132
`        end`
133
`        elm[1] = elm[1].to_i`
134
`      end`
135

136
`      num = Integer.from_prime_division(npd)`
137
`      den = Integer.from_prime_division(dpd)`
138

139
`      Rational(num,den)`
140

141
`    elsif other.kind_of?(Integer)`
142
`      if other > 0`
143
`        num = numerator ** other`
144
`        den = denominator ** other`
145
`      elsif other < 0`
146
`        num = denominator ** -other`
147
`        den = numerator ** -other`
148
`      elsif other == 0`
149
`        num = 1`
150
`        den = 1`
151
`      end`
152
`      Rational(num, den)`
153
`    elsif other.kind_of?(Float)`
154
`      Float(self) ** other`
155
`    else`
156
`      x , y = other.coerce(self)`
157
`      x ** y`
158
`    end`
159
`  end`
160
`end`
161

162
`##`
163 80
`# When mathn is required, the Math module changes as follows:`
164 81
`#`
165 82
`# Standard Math module behaviour:`