Modify

Ticket #30 (closed defect: fixed)

Opened 8 years ago

Last modified 8 years ago

Timestamp support has floating-point roundoff

Reported by: edemaine@… Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:
Cc:

Description

Example:

>>> import yaml, datetime
>>> yaml.dump(datetime.datetime(2005, 7, 8, 17, 35, 4, 517600))
'2005-07-08 17:35:04.517600\n'
>>> yaml.load(_)
datetime.datetime(2005, 7, 8, 17, 35, 4, 517599)

This breaks the desired rule that yaml.load(yaml.dump(x)) == x in a case where there should be no roundoff. (datetime.datetime uses integers everywhere to avoid any error.)

The offending code seems to be line 321 in yaml/constructor.py:

fraction = int(float(values['fraction'])*1000000)

This seems to be an "easy" way to convert the trailing '.517600' into an integer, but it can go beyond floating-point precision. Wouldn't the following work?

fraction = int(values['fraction'][:6].ljust(6, '0'))

Attachments

Change History

comment:1 Changed 8 years ago by xi

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in [234].

Thanks for the patch, it works perfectly.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.