Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:38:59+00:00 2026-05-28T03:38:59+00:00

The following python code calculates the number of iterations to do stuff based on

  • 0

The following python code calculates the number of iterations to do stuff based on some variables.

  # a - b - c is always a multiple of d.
  i = (a - b - c) / d
  while i:
    # do stuff
    i -= 1

The variables will all be of the same type, that is only ints or floats or whatever. My concern is whether it will work correctly if the values are floats. I know enough to always consider the pitfalls of relying on exact float values. But I can’t tell if the above is dangerous or not. I can use i = int(round((a - b - c) / d)), but I am curious as to understand floats better.

It all comes down to the following: a - b - c is an exact multiple of d. So I am relying on (a-b-c)/d to become a value i that I can subtract 1 from and get the expected number of iterations in the while loop, with the implied assumption that i == 0 becomes true. That is, can calculated multiples like this be decremented by 1 to reach exactly 0?

I would like to not only know if it is unsafe, but more importantly, what do I need to understand about floating point to resolve a question like this? If someone knows decisively whether this is safe or not, would it be possible to explain how so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:39:00+00:00Added an answer on May 28, 2026 at 3:39 am

    You can use the decimal module to get an idea of what “hides” between a floating point number such as 0.3:

    >>> from decimal import Decimal
    >>> Decimal(0.3)
    Decimal('0.299999999999999988897769753748434595763683319091796875')
    

    Note that Python 2.7 changed how floating point numbers are written (how repr(f) works) so that it now shows the shortest string that will give the same floating point number if you do float(s). This means that repr(0.3) == '0.3' in Python 2.7, but repr(0.3) == '0.29999999999999999' in earlier versions. I’m mentioning this since it can confuse things further when you really want to see what’s behind the numbers.

    Using the decimal module, we can see the error in a computation with floats:

    >>> (Decimal(2.0) - Decimal(1.1)) / Decimal(0.3) - Decimal(3) 
    Decimal('-1.85037170771E-16')
    

    Here we might expect (2.0 - 1.1) / 0.3 == 3.0, but there is a small non-zero difference. However, if you do the computation with normal floating point numbers, then you do get zero:

    >>> (2 - 1.1) / 0.3 - 3
    0.0
    >>> bool((2 - 1.1) / 0.3 - 3)
    False
    

    The result is rounded somewhere along the way since 1.85e-16 is non-zero:

    >>> bool(-1.85037170771E-16)
    True
    

    I’m unsure exactly where this rounding takes place.

    As for the loop termination in general, then there’s one clue I can offer: for floats less than 253, IEEE 754 can represent all integers:

    >>> 2.0**53    
    9007199254740992.0
    >>> 2.0**53 + 1
    9007199254740992.0
    >>> 2.0**53 + 2
    9007199254740994.0
    

    The space between representable numbers is 2 from 253 to 254, as shown above. But if your i is an integer less than 253, then i - 1 will also be a representable integer and you will eventually hit 0.0, which is considered false in Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Python code will result in n (14) being printed, as the for
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
When running the following python code: >>> f = open(rmyfile.txt, a+) >>> f.seek(-1,2) >>>
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
in the following python code: narg=len(sys.argv) print @length arg= , narg if narg ==
Recently I've spotted a very disturbing issue. I've got the following python code: for
I want to execute python code from C# with following code. static void Main(string[]
I was looking at the following code in python: for ob in [ob for
The following code fails to run on Python 2.5.4: from matplotlib import pylab as
What is the Python equivalent of the following code in Ruby? def loop cont=nil

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.