某些 BigDecimal 无法使用 ** 运算符计算
Some BigDecimal fail to calculate with ** operator
当我使用带负数 BigDecimal
和小数 BigDecimal
的 **
运算符时,失败并出现 Zero or negative argument
错误。
我检查了一些类似的值,如下所示:
-2 ** '0.3'.to_d
# => -1.23114441
2 ** '0.3'.to_d
# => 1.23114441
2 ** '0.3'.to_d
# => 1.23114441
-2 ** '0.3'.to_d
# => -1.23114441
2.to_d ** '0.3'.to_d
# => 1.23114441
-2.to_d ** '0.3'.to_d
# => Math::DomainError: Zero or negative argument for log
from (pry):111:in `**'
2.to_d ** 3.to_d
# => 8.0
-2.to_d ** 3.to_d
# => -8.0
为什么会出现此错误,我该如何解决?
我现在明白我的误解了。
我以为-2.0 ** 0.3
和(-2.0) ** 0.3
一样,结果和-(2.0 ** 0.3)
一样。
我不知道 **
比 -
强。
当我使用带负数 BigDecimal
和小数 BigDecimal
的 **
运算符时,失败并出现 Zero or negative argument
错误。
我检查了一些类似的值,如下所示:
-2 ** '0.3'.to_d
# => -1.23114441
2 ** '0.3'.to_d
# => 1.23114441
2 ** '0.3'.to_d
# => 1.23114441
-2 ** '0.3'.to_d
# => -1.23114441
2.to_d ** '0.3'.to_d
# => 1.23114441
-2.to_d ** '0.3'.to_d
# => Math::DomainError: Zero or negative argument for log
from (pry):111:in `**'
2.to_d ** 3.to_d
# => 8.0
-2.to_d ** 3.to_d
# => -8.0
为什么会出现此错误,我该如何解决?
我现在明白我的误解了。
我以为-2.0 ** 0.3
和(-2.0) ** 0.3
一样,结果和-(2.0 ** 0.3)
一样。
我不知道 **
比 -
强。