Cal*_*lin 6 ruby ruby-on-rails activesupport
更新到 ActiveSupport 6 时会发生这种情况
start_time = DateTime.now.beginning_of_day
start_time + BigDecimal(2).hours #=> Wed, 11 Sep 2019 01:59:59 +0000
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这与 Time 配合得很好
start_time = Time.now.beginning_of_day
start_time + BigDecimal(2).hours #=> 2019-09-11 02:00:00 +0000
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么?
最终,它归结为 ActiveSupport 内部执行的某些数学运算中的浮点错误。
请注意,使用 Rational 而不是 BigDecimal 有效:
DateTime.now.beginning_of_day + Rational(2, 1).hours
# => Mon, 02 Dec 2019 02:00:00 -0800
Time.now.beginning_of_day + Rational(2, 1).hours
# => 2019-12-02 02:00:00 -0800
Run Code Online (Sandbox Code Playgroud)
这是来自 Time/DateTime/ActiveSupport 的相关代码:
class DateTime
def since(seconds)
self + Rational(seconds, 86400)
end
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
else
plus_without_duration(other)
end
end
end
class Time
def since(seconds)
self + seconds
rescue
to_datetime.since(seconds)
end
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
else
plus_without_duration(other)
end
end
def advance(options)
unless options[:weeks].nil?
options[:weeks], partial_weeks = options[:weeks].divmod(1)
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
end
unless options[:days].nil?
options[:days], partial_days = options[:days].divmod(1)
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
end
d = to_date.gregorian.advance(options)
time_advanced_by_date = change(year: d.year, month: d.month, day: d.day)
seconds_to_advance = \
options.fetch(:seconds, 0) +
options.fetch(:minutes, 0) * 60 +
options.fetch(:hours, 0) * 3600
if seconds_to_advance.zero?
time_advanced_by_date
else
time_advanced_by_date.since(seconds_to_advance)
end
end
end
class ActiveSupport::Duration
def since(time = ::Time.current)
sum(1, time)
end
def sum(sign, time = ::Time.current)
parts.inject(time) do |t, (type, number)|
if t.acts_like?(:time) || t.acts_like?(:date)
if type == :seconds
t.since(sign * number)
elsif type == :minutes
t.since(sign * number * 60)
elsif type == :hours
t.since(sign * number * 3600)
else
t.advance(type => sign * number)
end
else
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
你的情况发生了什么就t.since(sign * number * 3600)行了,number是BigDecimal(2),而 DateTime.since 确实如此Rational(seconds, 86400)。所以使用 DateTime 时的整个表达式是Rational(1 * BigDecimal(2) * 3600, 86400).
由于 BigDecimal 用作 Rational 的参数,因此结果根本不合理:
Rational(1 * BigDecimal(2) * 3600, 86400)
# => 0.83333333333333333e-1 # Since there's no obvious way to coerce a BigDecimal into a Rational, this returns a BigDecimal
Rational(1 * 2 * 3600, 86400)
# => (1/12) # A rational, as expected
Run Code Online (Sandbox Code Playgroud)
该值使其回到 Time#advance。下面是它的计算结果:
options[:days], partial_days = options[:days].divmod(1)
# => [0.0, 0.83333333333333333e-1] # 0 days, 2 hours
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
# => 0.1999999999999999992e1 # juuuust under 2 hours.
Run Code Online (Sandbox Code Playgroud)
最后,0.199999999999999992e1 * 3600 = 7199.9999999999999712,当它最终转换回时间/日期时间时,它会被破坏。
Time 不会发生这种情况,因为 Time 不需要将持续时间的值传递给 Rational。
我不认为这应该被视为错误,因为如果您传递的是 BigDecimal,那么您应该期望代码如何处理您的数据:作为一个带有小数部分的数字,而不是一个比率。也就是说,当您使用 BigDecimals 时,您会遇到浮点错误。
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |