Python Forum
Simple Code - Strange Results?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Code - Strange Results?
#1
Here's a simple code I ran for practice as part of my class. Below are the results. Can anyone tell me why the increment when to the ?trillionth+? decimal place and not the thousandth that I specified?

import os
import sys
import re

votes=3
while True:
    votes=votes + .001
    print(votes)
    if votes>=5:
        break
Result(portion of...)
4.9870000000002195
4.98800000000022
4.98900000000022
4.9900000000002205
4.991000000000221
4.992000000000221
4.9930000000002215
4.994000000000222
4.995000000000222
4.9960000000002225
4.997000000000223
4.998000000000223
4.9990000000002235
5.000000000000224
Reply
#2
As soon as you add a float to an int, the int becomes a float.
Floats are made up of two values, the exponent and mantissa, so you can not expect that
floating point will be exact, but very close.
Reply
#3
Understood. Thank you very much
Reply
#4
Floating point arithmetic is notoriously imprecise because of how computer hardware handles floating point numbers. The trillionths arise from that imprecision.
Reply
#5
Some point on round() and force decimal places {:.2f} --> 2 decimal places.
votes = 3
while True:
    votes = votes + .001
    print(round(votes, 3))
    #print('{:.2f}'.format(votes))
    if votes >= 5:
        break
To overcome it decimal,can be important in eg finance calculation.
from decimal import Decimal

votes = Decimal('3')
while True:
    votes = votes + Decimal('.001')
    print(votes)
    if votes >= 5:
        break
Output:
.... 4.990 4.991 4.992 4.993 4.994 4.995 4.996 4.997 4.998 4.999 5.000
Reply
#6
i ran your code in python 2.7 it looped all the way to 5.0, each result either less or equal to number of decimal spaces you specified
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 313 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 476 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Code works but doesn't give the right results colin_dent 2 710 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  help me simple code result min and max number abrahimusmaximus 2 902 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,438 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,477 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,788 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,299 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,601 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,819 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea

Forum Jump:

User Panel Messages

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