pry(main)> time = Time.now
=> 2012-01-20 00:10:44 +0000
pry(main)> (time + 4.days).to_f
=> 1327363844.9709609
pry(main)> time.to_f
=> 1327018244.970961
Why does adding a day to a time change the fractional part of seconds?
306 Views Asked by Peder At
2
There are 2 best solutions below
0
MRocklin
On
This is a floating point rounding issue. Your number is stored as a double precision floating point number which has a precision of 53 bits. 2^53 is roughly 9*10^15 giving you between 15 and 16 decimal digits, depending on the exact number to be represented.
You may notice that these two numbers have 16 and 15 decimal digits respectively. You are off only in the last place. In truth the exact stored value is neither of these two decimal numbers but rather something that is only exactly represented in fractional binary.
Related Questions in RUBY
- Best way to make an HABTM association via console
- undefined method `namespace' for main:Object (NoMethodError) - active record / rakefile
- Ruby destroy is not working? Or objects still present?
- Trying to set the value of an input with mechanize
- How to split the logic in a ruby game
- How can I monitor an endpoint's status with Ruby?
- Why can a private class method be explicitly invoked in Ruby?
- Rails - Ajax do not work properly on production server
- syntax error, unexpected kEND
- Carrierwave file upload with different file types
- b.javascript_dialog().exists? is not working for me in WATIR 4.0.2
- Combine two arrays of hashes
- Building a simple calculator form in Rails 4
- How do I update create route from rails 3 to 4
- Comparison of Fixnum with nil failed - palindrome program Ruby
Related Questions in DATETIME
- Python datetime.now() with timezone
- Create a list of sequential monthly dates in PHP given initial date and quantity
- DataTable RowFilter on Time component of DateTime column
- Joda DateTime Json date format issue
- how to create folder and file with datetime in wpf application
- String concatenation with padded integers
- What is the correct DateTime format in WebSQL to perform date comparison queries?
- how to get the time interval of a date and a datetime displayed as day,hour, min,seconds in sql
- DateTime.Now.ToString("HH:MM") allways gives 20:06
- How can I get the ctime and/or mtime of a file in Python including timezone?
- add time (char(8)) to date column
- MyBatis cannot work with joda DateTime?
- PHP Fatal error: Call to a member function format() on boolean
- Use DateTime format in a class but restrict time tokens
- Modify records to account for correct priority and order between two systems
Related Questions in DATE
- PLSQL Need REFCURSOR DATE + TIME
- Pandas date ranges and averaging the counts
- javascript date validation to check whether it exceeds exactly next year's date
- How to INSERT date in PHP & Mysqli
- Create a list of sequential monthly dates in PHP given initial date and quantity
- Date parse with Timezone - Android
- RAILS: date_select validation
- Having trouble writing a calendar, I couldn't find a detailed guide answering my questions anywhere
- Julian Date Conversion SQL
- SQL find out how many days into year date is
- Algorithm to find the Gregorian date of the Chinese New Year of a certain Gregorian year
- Angularjs, date input : enter a date manually with a specific format
- How to convert a Microsoft SQL date to a vba string?
- Why does new java.text.SimpleDateFormat("EEEE").format(new java.util.Date(2015, 6, 9)) return the wrong day of the week?
- SQL Column Numeric to DATE
Related Questions in DATETIME-FORMAT
- What is the correct DateTime format in WebSQL to perform date comparison queries?
- MYSQL comparing time and day of week simultaneously
- How to remove mili seconds from '0' date formatting
- php date->foramt() return object instead of string
- Select from Oracle table, date time value?
- Matlab : datestr predefined date formats ISO8601
- Merging two columns into a single column and formatting the content to form an accurate date-time format in Hive?
- How to change DateTime model field representation in serializer?
- How to format the given time string and convert to date/time object
- Plot two files in gnuplot with different timefmt
- SimpleDateFormat.parse(String) gives null value android
- Conversion of Python DateTime string into integer milliseconds
- Convert string to MM/yyyy in c# to sort
- Retrieve Date information from table with Timestamp and convert into 24hr format string
- Formatting date using moment.js using locale rather than explicit format
Related Questions in SECONDS
- How do I check if a time is between two times which are stored as Strings in Ruby on Rails?
- C/C++ time_t in microseconds
- UTC seconds and time format expression convertion in Python
- Convert double to struct tm
- Convert varchar to datetime and add seconds
- pause countdown javascript jquery
- In Ada, how do I get the current time in seconds as a value I can print out and perform operations with?
- How to round off timestamp in milliseconds to nearest seconds?
- php - How To Convert 1 day To Seconds
- Get number of seconds since
- Convert minutes into seconds in php
- Converting Seconds to Time Format
- Want to put some seconds delay in calling a function
- Get date-time of directory and compare
- Unity3d- How can I check if my character is in air for 2 seconds?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
It didn't for me when I did:
I believe this is just a small round issue common with floats and you found a small precision error.
That is much less than a second, i.e. .0000001 of a day. Given there are only 86,400 seconds in a day this is frequently not a issue, although a good reason to store dates as dates and do Ruby date arithmetic on them.