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

  • SEARCH
  • Home
  • 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 5960491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:50:01+00:00 2026-05-22T18:50:01+00:00

In a for-loop, I’m integrating with respect to time with constant, fractional time step,

  • 0

In a for-loop, I’m integrating with respect to time with constant, fractional time step, dt. I only want to save the simulation results for integral (finite ordinal) time points. My solution is as follows,

dt = 0.1
steps = 100

for step in range(steps):
    if (step*dt) % 1 == 0.0:
        print step

I’ve never really trusted modular arithmetic on floats. Is there a better way to check if a float is integral or am I simply being paranoid?

  • 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-22T18:50:02+00:00Added an answer on May 22, 2026 at 6:50 pm

    This is dangerous, in any programming language. In your example, 0.1 cannot be represented exactly by in floating-point, so that test will never pass (well I suppose it may do after 2^24 iterations or so). In many cases, the step size may not have an exact representation in floating-point, so that the accumulated rounding error causes the test to erroneously trigger/not trigger. In other cases, as the accumulated value gets larger, eventually it will begin to lose precision due to the increasing exponent (in your example, assuming Python uses single-precision by default, you’ll get an erroneous trigger after 20971529 iterations).

    Try to find a way to avoid performing equality tests on floating-point values (checking for integral values is one such test). So in your case, just test on step % 10.

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

Sidebar

Related Questions

I want to loop a array of chars in a WHILE loop (with only
I have a for loop. Inside that loop I want to fill up an
Is it possible to loop vertically ? I want to display in JSP by
I have a for loop that writes the results of a query into a
I have a loop in my KornShell (ksh) script that I want to execute
I have while loop and in that there's forum posts.I want to show number
This loop doesn't seem to be finding the lines I want it to (it's
Infinite loop on bash script and I want to run forever but (I guess)
I want to loop over the contents of a text file and do a
I want to loop over the elements of an HTML form, and store the

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.