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.

Bug: floats are arbitrarily rounded (no filters used)

See original GitHub issue

Test case is just rendering a float:

$ python -c 'from jinja2 import Template; print Template("{{num}}").render(num=1441712583.216615)'
1441712583.22

4 last digits are not present and the last digits indicates rounding.

If I delete 3 last integer digits I get 5 rounded fraction digits (instead of full 6):

python -c 'from jinja2 import Template; print Template("{{num}}").render(num=1441712.216615)' 
1441712.21662

Unless I’m overlooking something stupid, it’s a critical bug. I tested it on Jinja2 2.7.3 and 2.8.0 and CPython 2.7.6 and PyPy 2.6.1.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aarturcommented, Dec 20, 2015

Jinja isn’t only used to directly produce “customer facing representations”. Even when it’s used to produce HTML, there are major usages (data-* attributes, elements, Javascript fragments) for which you want a higher precision for floats. it’s also advertised as working good not only for HTML, and indeed it’s widely used for things like source code or configs (Salt and Ansible).

For me the behavior of str(float_val) (using up to exactly 17 digits of representation) is weird and I don’t see reasons, other than being easy to implement in CPython, that it was chosen.

0reactions
mitsuhikocommented, Jan 7, 2017

This won’t change because it would require special casing floats. I’m happy to accept filters to improve float rendering in general for specific cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numeric precision - What causes floating point rounding errors?
Any form of representation will have some rounding error for some number. Try to express 1/3 in IEEE floating point, or in decimal....
Read more >
Is floating point math broken? - Stack Overflow
Using the truncation, round-up, and round down alone may result in an error that is greater than one half of one unit in...
Read more >
That's Not Normal–the Performance of Odd Floats
Positive and negative infinity round out the number line and are used to represent overflow and divide-by-zero. There are two of them. NaNs....
Read more >
[postgres] type float4 not correctly parsed for large numbers ...
Hasura does not correctly retrieve value for Postgres float4 large numbers (ex: ... The value is rounded, instead of returning the real value....
Read more >
Parameter Filters — GeoWebCache 1.12.1 User Manual
Parameter filters provide templates for extracting arbitrary parameters from requests. This allows using GeoWebCache in scenarios such as time series data, ...
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