question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numbers in scientific notation without dot are parsed as string

See original GitHub issue

Hi,

it appears that PyYAML requires a dot in the mantissa as well as a sign in the exponent in order to parse numbers in scientific notation as numbers and otherwise parses them as string:

>>> import yaml
>>> yaml.safe_load('1e+10')
'1e+10'
>>> yaml.safe_load('1.e+10')
10000000000.0
>>> yaml.safe_load('1.0e10')
'1.0e10'

According to YAML 1.2 spec the dot is optional as in JSON:

Either 0, .inf, -.inf, .nan, or scientific notation matching the regular expression -? [1-9] ( \. [0-9]* [1-9] )? ( e [-+] [1-9] [0-9]* )?.

Furthermore, JSON makes even the sign in the exponent optional: https://www.json.org/number.gif

Since YAML 1.2 aspires to be a superset of JSON, I believe it should make the sign optional as well.

Others than ran into similar problems:

https://stackoverflow.com/questions/30458977/yaml-loads-5e-6-as-string-and-not-a-number

Best, Thomas

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:29
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
AlexanderNaehringcommented, Nov 7, 2019

Issue is still present, I use the fix found on Stackoverflow, manually adding the new implicit resolver.

5reactions
ischoeglcommented, Apr 10, 2019

For anyone running into similar issues, the derived package ruamel.yaml resolves issues like the one stated above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

YAML loads 5e-6 as string and not a number - Stack Overflow
The problem lies in the fact that the YAML Resolver is set up to match floats as follows: Resolver.add_implicit_resolver( u'tag:yaml.org,2002:float', ...
Read more >
Parsing from scientific notation - MSDN - Microsoft
Im trying to read in values from a file containing numbers that are a mix of "normal" doubles (i.e. 3.23124) and numbers written...
Read more >
SQL Format Models - Snowflake Documentation
Parsing strings containing numbers is affected by both the FX and FM modifiers: In lax mode: Digit group separators are optional (i.e. numbers...
Read more >
Floating point numbers - Manual - PHP
Converting to float ¶. From strings ¶. If the string is numeric or leading numeric then it will resolve to the corresponding float...
Read more >
DecimalFormat (Java Platform SE 7 ) - Oracle Help Center
DecimalFormat can be instructed to format and parse scientific notation only ... Formats a number and appends the resulting text to the given...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found