Project

General

Profile

Feature #10974 » deprecate_CMath_methods.patch

gogotanaka (Kazuki Tanaka), 06/11/2015 06:31 AM

View differences:

lib/cmath.rb
23 23

  
24 24
  include Math
25 25

  
26
  alias exp! exp
27
  alias log! log
28
  alias log2! log2
29
  alias log10! log10
30
  alias sqrt! sqrt
31
  alias cbrt! cbrt
32

  
33
  alias sin! sin
34
  alias cos! cos
35
  alias tan! tan
36

  
37
  alias sinh! sinh
38
  alias cosh! cosh
39
  alias tanh! tanh
40

  
41
  alias asin! asin
42
  alias acos! acos
43
  alias atan! atan
44
  alias atan2! atan2
45

  
46
  alias asinh! asinh
47
  alias acosh! acosh
48
  alias atanh! atanh
26
  %w[
27
    exp
28
    log
29
    log2
30
    log10
31
    sqrt
32
    cbrt
33
    sin
34
    cos
35
    tan
36
    sinh
37
    cosh
38
    tanh
39
    asin
40
    acos
41
    atan
42
    atan2
43
    asinh
44
    acosh
45
    atanh
46
  ].each do |meth|
47
    define_method(meth + '!') do |*args, &block|
48
      warn("CMath##{meth}! is deprecated; use CMath##{meth} or Math##{meth}") if $VERBOSE
49
      Math.send(meth, *args, &block)
50
    end
51
  end
49 52

  
50 53
  ##
51 54
  # Math::E raised to the +z+ power
......
54 57
  def exp(z)
55 58
    begin
56 59
      if z.real?
57
        exp!(z)
60
        Math.exp(z)
58 61
      else
59
        ere = exp!(z.real)
60
        Complex(ere * cos!(z.imag),
61
                ere * sin!(z.imag))
62
        ere = Math.exp(z.real)
63
        Complex(ere * Math.cos(z.imag),
64
                ere * Math.sin(z.imag))
62 65
      end
63 66
    rescue NoMethodError
64 67
      handle_no_method_error
......
74 77
  def log(z, b=::Math::E)
75 78
    begin
76 79
      if z.real? && z >= 0 && b >= 0
77
        log!(z, b)
80
        Math.log(z, b)
78 81
      else
79
        Complex(log!(z.abs), z.arg) / log(b)
82
        Complex(Math.log(z.abs), z.arg) / log(b)
80 83
      end
81 84
    rescue NoMethodError
82 85
      handle_no_method_error
......
90 93
  def log2(z)
91 94
    begin
92 95
      if z.real? and z >= 0
93
        log2!(z)
96
        Math.log2(z)
94 97
      else
95
        log(z) / log!(2)
98
        log(z) / Math.log(2)
96 99
      end
97 100
    rescue NoMethodError
98 101
      handle_no_method_error
......
106 109
  def log10(z)
107 110
    begin
108 111
      if z.real? and z >= 0
109
        log10!(z)
112
        Math.log10(z)
110 113
      else
111
        log(z) / log!(10)
114
        log(z) / Math.log(10)
112 115
      end
113 116
    rescue NoMethodError
114 117
      handle_no_method_error
......
123 126
    begin
124 127
      if z.real?
125 128
        if z < 0
126
          Complex(0, sqrt!(-z))
129
          Complex(0, Math.sqrt(-z))
127 130
        else
128
          sqrt!(z)
131
          Math.sqrt(z)
129 132
        end
130 133
      else
131 134
        if z.imag < 0 ||
......
134 137
        else
135 138
          r = z.abs
136 139
          x = z.real
137
          Complex(sqrt!((r + x) / 2.0), sqrt!((r - x) / 2.0))
140
          Complex(Math.sqrt((r + x) / 2.0), Math.sqrt((r - x) / 2.0))
138 141
        end
139 142
      end
140 143
    rescue NoMethodError
......
157 160
  def sin(z)
158 161
    begin
159 162
      if z.real?
160
        sin!(z)
163
        Math.sin(z)
161 164
      else
162
        Complex(sin!(z.real) * cosh!(z.imag),
163
                cos!(z.real) * sinh!(z.imag))
165
        Complex(Math.sin(z.real) * Math.cosh(z.imag),
166
                Math.cos(z.real) * Math.sinh(z.imag))
164 167
      end
165 168
    rescue NoMethodError
166 169
      handle_no_method_error
......
174 177
  def cos(z)
175 178
    begin
176 179
      if z.real?
177
        cos!(z)
180
        Math.cos(z)
178 181
      else
179
        Complex(cos!(z.real) * cosh!(z.imag),
180
                -sin!(z.real) * sinh!(z.imag))
182
        Complex(Math.cos(z.real) * Math.cosh(z.imag),
183
                -Math.sin(z.real) * Math.sinh(z.imag))
181 184
      end
182 185
    rescue NoMethodError
183 186
      handle_no_method_error
......
191 194
  def tan(z)
192 195
    begin
193 196
      if z.real?
194
        tan!(z)
197
        Math.tan(z)
195 198
      else
196 199
        sin(z) / cos(z)
197 200
      end
......
207 210
  def sinh(z)
208 211
    begin
209 212
      if z.real?
210
        sinh!(z)
213
        Math.sinh(z)
211 214
      else
212
        Complex(sinh!(z.real) * cos!(z.imag),
213
                cosh!(z.real) * sin!(z.imag))
215
        Complex(Math.sinh(z.real) * Math.cos(z.imag),
216
                Math.cosh(z.real) * Math.sin(z.imag))
214 217
      end
215 218
    rescue NoMethodError
216 219
      handle_no_method_error
......
224 227
  def cosh(z)
225 228
    begin
226 229
      if z.real?
227
        cosh!(z)
230
        Math.cosh(z)
228 231
      else
229
        Complex(cosh!(z.real) * cos!(z.imag),
230
                sinh!(z.real) * sin!(z.imag))
232
        Complex(Math.cosh(z.real) * Math.cos(z.imag),
233
                Math.sinh(z.real) * Math.sin(z.imag))
231 234
      end
232 235
    rescue NoMethodError
233 236
      handle_no_method_error
......
241 244
  def tanh(z)
242 245
    begin
243 246
      if z.real?
244
        tanh!(z)
247
        Math.tanh(z)
245 248
      else
246 249
        sinh(z) / cosh(z)
247 250
      end
......
257 260
  def asin(z)
258 261
    begin
259 262
      if z.real? and z >= -1 and z <= 1
260
        asin!(z)
263
        Math.asin(z)
261 264
      else
262 265
        (-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
263 266
      end
......
273 276
  def acos(z)
274 277
    begin
275 278
      if z.real? and z >= -1 and z <= 1
276
        acos!(z)
279
        Math.acos(z)
277 280
      else
278 281
        (-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
279 282
      end
......
289 292
  def atan(z)
290 293
    begin
291 294
      if z.real?
292
        atan!(z)
295
        Math.atan(z)
293 296
      else
294 297
        1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
295 298
      end
......
306 309
  def atan2(y,x)
307 310
    begin
308 311
      if y.real? and x.real?
309
        atan2!(y,x)
312
        Math.atan2(y,x)
310 313
      else
311 314
        (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
312 315
      end
......
322 325
  def asinh(z)
323 326
    begin
324 327
      if z.real?
325
        asinh!(z)
328
        Math.asinh(z)
326 329
      else
327 330
        log(z + sqrt(1.0 + z * z))
328 331
      end
......
338 341
  def acosh(z)
339 342
    begin
340 343
      if z.real? and z >= 1
341
        acosh!(z)
344
        Math.acosh(z)
342 345
      else
343 346
        log(z + sqrt(z * z - 1.0))
344 347
      end
......
354 357
  def atanh(z)
355 358
    begin
356 359
      if z.real? and z >= -1 and z <= 1
357
        atanh!(z)
360
        Math.atanh(z)
358 361
      else
359 362
        log((1.0 + z) / (1.0 - z)) / 2.0
360 363
      end
361
-