Project

General

Profile

Actions

Bug #15023

closed

Time precision (in microsecond) inaccurate after performing strftime

Added by calvinchso (Calvin So) over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
[ruby-core:88632]

Description

Please see the example below. I think we should expect the result to be "1486525793.995". Right?

OS version: MacOS High Sierra 10.13.6

Ruby version: 2.5.1

2.5.1 :005 > Time.at(1486525793.995).strftime("%s.%6N")
 => "1486525793.994999"

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Rejected

IEEE 754 floating point numbers are inexact, and cannot represent that value exactly.

printf("%.20f\n", 1486525793.995) #=> 1486525793.99499988555908203125

You can use the second argument to Time.at if you have accurate fraction seconds.

puts Time.at(1486525793, 995_000).strftime("%s.%6N") #=> 1486525793.995000

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Time.at supports Rational numbers too.

puts Time.at(1486525793.995r).strftime("%s.%6N") #=> 1486525793.995000
Actions

Also available in: Atom PDF

Like0
Like0Like0