Changeset 15476


Ignore:
Timestamp:
27 Jun 2019, 06:07:52 (5 years ago)
Author:
Henrik Bettermann
Message:

Use decimal.Decimal to counteract Python floating point limitation.

Location:
main/waeup.kofa/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/CHANGES.txt

    r15446 r15476  
    441.6.1.dev0 (unreleased)
    55=======================
     6
     7* Use decimal.Decimal to counteract Python floating point limitation.
    68
    79* Add level 0 (None) to course levels.
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_utils.py

    r12110 r15476  
    123123        utils.SYSTEM_MAX_LOAD['virt-mem'] = -(sys.maxint)  # negative int
    124124        assert utils.expensive_actions_allowed() == False
     125
     126    def test_format_float(self):
     127        format_float= KofaUtils().format_float
     128        # This is the floating point problem ...
     129        self.assertEqual(4.6*100, 459.99999999999994)
     130        # ... which is solved in format_float
     131        self.assertEqual(format_float(4.6 ,1), '4.6')
     132        self.assertEqual(format_float(4.6 ,2), '4.60')
     133        self.assertEqual(format_float(4.6 ,3), '4.600')
     134        self.assertEqual(format_float(4.6 ,4), '4.6000')
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/utils.py

    r15432 r15476  
    2222import string
    2323import pytz
     24import decimal
    2425from copy import deepcopy
    2526from random import SystemRandom as r
     
    417418
    418419    def format_float(self, value, prec):
     420        # >>> 4.6 * 100
     421        # 459.99999999999994
     422        value = decimal.Decimal(str(value))
    419423        # cut floating point value
    420424        value = int(pow(10, prec)*value) / (1.0*pow(10, prec))
Note: See TracChangeset for help on using the changeset viewer.