Bug: floats are arbitrarily rounded (no filters used)
See original GitHub issueTest 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:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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.