0

I am having an issue with adding floating point numbers to time in ruby. In the example below, how is t2 not equal to 2013-02-15T01:17:17.996000000?

irb(main):094:0> t1.strftime("%Y-%m-%dT%H:%M:%S.%N")
=> "2013-02-15T01:16:47.785000000"
irb(main):095:0> t2 = t1 + 30.211
=> 2013-02-15 01:17:17 -0700
irb(main):096:0> t2.strftime("%Y-%m-%dT%H:%M:%S.%N")
=> "2013-02-15T01:17:17.995999999"
dukebd711
  • 15
  • 2

2 Answers2

2

This is mainly about floating-point arithmetic.

Try 1.4 - 1.3 in the ruby shell.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

1

This has been discussed before. You should check the following thread: ruby floating point errors

Community
  • 1
  • 1
lsu_guy
  • 1,525
  • 15
  • 12