The fact that the numbers are stored in binary is significant:
In [16]: nums = [eval('1.%d5' % n) for n in range(10)]
In [17]: nums
Out[17]: [1.05, 1.15, 1.25, 1.35, 1.45, 1.55, 1.65, 1.75, 1.85, 1.95]
In [18]: [round(n, 1) for n in nums]
Out[18]: [1.1, 1.1, 1.2, 1.4, 1.4, 1.6, 1.6, 1.8, 1.9, 1.9]
Proper decimal rounding might look like:
In [20]: from decimal import Decimal
In [21]: nums = [Decimal('1.%d5' % n) for n in range(10)]
In [22]: nums
Out[22]:
[Decimal('1.05'),
Decimal('1.15'),
Decimal('1.25'),
Decimal('1.35'),
Decimal('1.45'),
Decimal('1.55'),
Decimal('1.65'),
Decimal('1.75'),
Decimal('1.85'),
Decimal('1.95')
In [23]: [round(n, 1) for n in nums]
Out[23]:
[Decimal('1.1'),
Decimal('1.2'),
Decimal('1.3'),
Decimal('1.4'),
Decimal('1.5'),
Decimal('1.6'),
Decimal('1.7'),
Decimal('1.8'),
Decimal('1.9'),
Decimal('2.0')]
Or with half-even rounding:
In [24]: from decimal import getcontext
In [25]: getcontext().rounding = 'ROUND_HALF_EVEN'
In [26]: [round(n, 1) for n in nums]
Out[26]:
[Decimal('1.0'),
Decimal('1.2'),
Decimal('1.2'),
Decimal('1.4'),
Decimal('1.4'),
Decimal('1.6'),
Decimal('1.6'),
Decimal('1.8'),
Decimal('1.8'),
Decimal('2.0')]
The binary floats don't work out correct because some are above and
some are below the number suggested by the original float literal.
> To unsubscribe from this group and stop receiving emails from it, send an email to
sympy+un...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/7873bfe6-6f91-4d39-af6f-9f4039714fa8%40googlegroups.com.