Python Forum
Python random formatting issues
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python random formatting issues
#11
This is a limitation of binary floating point arithmetics. The python number 0.0952 is not exactly equal to the real number 952/10000 because 0.0952 is stored in base 2 and only a finite number of bits are kept. In other words it cannot be represented exactly in memory at the machine precision. You can see the error with the fractions module
>>> from fractions import Fraction
>>> Fraction(0.0952) - Fraction(952, 10000)
Fraction(77, 11258999068426240000)
you see that there is an error. This is not a Python specific error: you would get the same behavior in all languages or systems that use 64 bits floating point numbers.

The consequence for you is that B*100 is not exactly equal to 9.52
>>> x = 0.0952
>>> x * 100
9.520000000000001
By printing with the format() method, you don't see this error because the format method performs a rounding when transforming the number to a string
>>> '{:.2f}'.format(x * 100)
'9.52'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String formatting (strptime) issues Henrio 2 811 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  python-docx: preserve formatting when printing lines Tmagpy 4 2,009 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
Shocked Issues Installing Pyenv/Python 3.9.1 Brandon_Contactum 1 2,462 Feb-22-2022, 06:32 PM
Last Post: snippsat
  OpenPyXl formatting issues kpayney1 0 1,597 Nov-26-2021, 01:56 AM
Last Post: kpayney1
  Formatting issues? Mark17 1 1,654 Dec-30-2020, 04:17 PM
Last Post: Mark17
  Changing to new Python formatting example leodavinci1990 3 1,991 Sep-22-2020, 07:36 PM
Last Post: yaythomas
  TurtleWorld on MAC in Python 3.6 issues benniehanas 3 3,679 Jul-09-2018, 08:00 PM
Last Post: ichabod801
  Issues with Python script as service satishgunjal 8 7,367 Jun-21-2018, 11:40 AM
Last Post: satishgunjal
  python variable issues - using spyder and opencv Afrodizzyjack 5 5,713 Jun-19-2018, 09:46 AM
Last Post: gontajones
  issues while using Psexec from python code dev_devil_1983 0 3,503 May-28-2018, 07:55 AM
Last Post: dev_devil_1983

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020