我创建了log_slow
别名,但它无效:
require 'bigdecimal'
require 'bigdecimal/math'
require 'bigdecimal/util'
module BigMath
alias :log :log_slow
def log(x, prec)
if x <= 0 || prec <= 0
raise ArgumentError, "Zero or negative argument for log"
end
return x if x.infinite? || x.nan?
sign, fraction, power, exponent = x.split
fraction = BigDecimal(".#{fraction}")
power = power.to_s.to_d
log_slow(fraction, prec) + (log_slow(power, prec) * exponent)
end
end
include BigMath
number = BigDecimal("1234.5678")
Math.log(number)
prec = 50
puts BigMath.log_slow(number, prec).round(prec).to_s("F")
puts BigMath.log(number, prec).round(prec).to_s("F")
puts BigMath.log(number ** 1000, prec).round(prec).to_s("F")
我收到错误:
in `<module:BigMath>': undefined method `log_slow' for module `BigMath' (NameError)
答案 0 :(得分:1)
您的别名应该是这样的,以逃避错误
module BigMath
def log(x, prec)
#your code
end
alias log_slow log
end
格式:
alias new_name old_name