Modify

Ticket #49 (closed enhancement: fixed)

Opened 7 years ago

Last modified 7 years ago

yaml confused by python float repr()

Reported by: cems a.t. lanl.gov Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:
Cc:

Description (last modified by xi) (diff)

platform: intel macintosh, python2.5 , latest yaml.

Description: depending on a floats value dump() will output a different representation format. Sometimes it uses a tag format and others a standard float notation depending on the value.

example:

In [160]:dat ="""
float1: 1.0E-8
float2: 1.0E-9
"""

In [165]:t = yaml.load(dat)

In [166]:t
Out[166]:{'float1': 1e-08, 'float2': 1.0000000000000001e-09}

In [167]:yaml.dump(t)
Out[167]:"{float1: !!float '1e-08', float2: 1.0000000000000001e-09}\n"

notice the !!float tag followed by a string.

regression: this appears to be an attempt to compensate for fact that python's string representation of some float values having different formats: that is in python2.5 the repr() of 1.0e-8 is actually 1e-08 (it supresses the decimal point). Since YAML does not recognize floats without the decimal point the dump command adds the float tag. However while the repr() of 1.0E-9 is 1.0000000000000001e-09. which has the decimal point making it legal YAML and it dump avoids the float tag

It seems to me that a more consistent and desirable result would be obtained if dump were to output 1e-8 as 1.0e-8 rather than using the float if one is parsing a yaml file in another language the tagged format of the float can't be read easily. (e.g. consider reading this in fortran or perl without using a yaml lib to read the file.)

Attachments

Change History

comment:1 Changed 7 years ago by xi

  • Status changed from new to closed
  • Resolution set to fixed
  • Description modified (diff)

Thanks for the suggestion. Fixed in [248].

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.