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 8441935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:42:22+00:00 2026-06-10T08:42:22+00:00

Never seen anything like this. Simple while loop: t_end = 100.0 t_step= 0.1 time

  • 0

Never seen anything like this. Simple while loop:

t_end = 100.0
t_step= 0.1
time = 0

while time<=t_end:
    time+=t_step
    print time

Last 3 printed values:

...
99.9
100.0
100.1

Looks right to me.

Now, I change t_step to 0.01:

t_end = 100.0
t_step= 0.01
time = 0

while time<=t_end:
    time+=t_step
    print time

Last 3 printed values:

...
99.98
99.99
100.0

Question: Why it doesn’t go for the final loop when time = t_end =100.0 ?

What is the alternative solution?

  • 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-06-10T08:42:24+00:00Added an answer on June 10, 2026 at 8:42 am

    Because this 100.0 (result of a sum) can be bigger than 100.0 you write by hand. You should not compare float numbers for equality…

    You should read this:

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Possible solution:

    >>> t_end = 100.0
    >>> t_step = 0.01
    >>> total = int(t_end/t_step)
    >>> for x in itertools.accumulate((t_step for i in range(total + 1))):
        print(x)
    

    So the last element will be:
    100.01000000001426

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

Sidebar

Related Questions

I've never seen something like this before. So, it's confusing me for a while.
I've never seen anything like this. File structure: somefolder/base.php includes/stuff.php includes/constants.php base.php: <?php require(../includes/stuff.php);
This is really weird in my perspective. I've never seen anything like it. I
I've never seen anything like this before. Tried everything I could, searched the web
I've never seen code like this: public static function getInstance() { if ( !
I've never seen something like this before EntityA @Id long id @Column(name =field1) String
I've seen XML before, but I've never seen anything like EDI . How do
I've read XML or CSV before, but I've never seen anything like EDI. How
I have never seen this before, the rows will be sequential but I have
I've never seen this operator yet.

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.