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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:57:20+00:00 2026-05-24T23:57:20+00:00

I would like some advise on how to optimise the following while loop: double

  • 0

I would like some advise on how to optimise the following while loop:

double minor_interval   = 0.1;
double major_interval   = 1.0;

double start            = 0.0;
double finish           = 10.0;

printf("Start\r\n");

while (start < finish)
{
    printf("Minor interval: %.20f\r\n", start);

    double m = fmod(start, major_interval);
    printf("m: %.20f\r\n", m);

    if (m == 0)
        printf("At major interval: %.20f\r\n", start);

    start += minor_interval;
}

printf("Finished\r\n");

Essentially, I am incrementing a counter in the loop by the minor interval and would like to know every time around the loop if I am at a major interval. Picture this as drawing a ruler with millimeter intervals, and every time I get to a major interval, I want to draw a centimetre. Given inaccuracies in floating point arithmetic, how can I modify the above loop to implement the functionality I require? I have tried different methods of comparing the result of the modulus using a tolerance with no luck. Note that the minor and major intervals can be any values i.e. minor = 0.4 and major = 1.6 (to draw quarter mile increments).

Thanks in advance.

  • 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-24T23:57:20+00:00Added an answer on May 24, 2026 at 11:57 pm

    I would implement this with a for loop over an int variable i. Using integers avoids rounding problems due to the limitations of floating point arithmetic. [See What Every Computer Scientist Should Know About Floating-Point Arithmetic.]

    Let i run from 0 to iFinish-1 and set time equal to start + i*minor_interval. The value of iFinish can be found by rounding (finish-start)/minor_interval to the nearest int.

    Major axis updates can be handled in a similar way. Round major_interval/minor_interval to the nearest int, say k. Then update major axis marks every k-th iteration.

    In terms of code it looks like this:

    double round(double r) {
        return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
    }
    ...
    int iFinish = round((finish-start)/minor_interval);
    int k = round(major_interval/minor_interval);
    for (int i=0; i<iFinish; i++)
    {
        double time = start + i*minor_interval;
        bool isMajor = (i%k == 0);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like some advice on the best approach to use in the following
Hope you can advise I would like to add some simple fade in out
I would like some advice about the following Makefile. It works fine, but it
I would like some advise on threading as I'm new to it. I've read
I'm in the process of creating a website and would like some advise on
I would like to get some advise. I have an application which consist of
Newbie with databases, I would like some advise please.. I have agencies who can
So, who could advise some good Apache Camel routes visual designer? Would like to
I am designing RESTful Api's and would like some advice on designing an API
I am considering starting an OSS project and would like some advice. I would

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.