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

The Archive Base Latest Questions

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

long-time listener, first-time caller. I am relatively new to programming and was looking back

  • 0

long-time listener, first-time caller. I am relatively new to programming and was looking back at some of the code I wrote for an old lab. Is there an easier way to tell if a double is evenly divisible by an integer?

double num (//whatever);
int divisor (//an integer);
bool bananas;

   if(floor(num)!= num || static_cast<int>(num)%divisor != 0) {
     bananas=false;
   }   
   if(bananas==true)
         //do stuff;
}
  • 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-21T01:59:28+00:00Added an answer on May 21, 2026 at 1:59 am

    The question is strange, and the checks are as well. The problem is that it makes little sense to speak about divisibility of a floating point number because floating point number are represented imprecisely in binary, and divisibility is about exactitude.

    I encourage you to read this article, by David Goldberg: What Every Computer Scientist Should Know About Floating Point Arithmetic. It is a bit long-winded, so you may appreciate this website, instead: The Floating-Point Guide.

    The truth is that floor(num) == num is a strange piece of code.

    • num is a double
    • floor(num) returns an double, close to an int

    The trouble is that this does not check what you really wanted. For example, suppose (for the sake of example) that 5 cannot be represented exactly as a double, therefore, instead of storing 5, the computer will store 4.999999999999.

    double num = 5; // 4.999999999999999
    double floored = floor(num); // 4.0
    
    assert(num != floored);
    

    In general exact comparisons are meaningless for floating point numbers, because of rounding errors.

    If you insist on using floor, I suggest to use floor(num + 0.5) which is better, though slightly biased. A better rounding method is the Banker’s rounding because it is unbiased, and the article references others if you wish. Note that the Banker’s rounding is the baked in in round…


    As for your question, first you need a double aware modulo: fmod, then you need to remember the avoid exact comparisons bit.

    A first (naive) attempt:

    // divisor is deemed non-zero
    // epsilon is a constant
    
    double mod = fmod(num, divisor); // divisor will be converted to a double
    
    if (mod <= epsilon) { }
    

    Unfortunately it fails one important test: the magnitude of mod depends on the magnitude of divisor, thus if divisor is smaller than epsilon to begin with, it will always be true.

    A second attempt:

    // divisor is deemed non-zero
    double const epsilon = divisor / 1000.0;
    
    double mod = fmod(num, divisor);
    
    if (mod <= epsilon) { }
    

    Better, but not quite there: mod and epsilon are signed! Yes, it’s a bizarre modulo, th sign of mod is the sign of num

    A third attempt:

    // divisor is deemed non-zero
    double const eps = fabs(divisor / 1000.0);
    
    double mod = fabs(fmod(num, divisor));
    
    if (mod <= eps) { }
    

    Much better.

    Should work fairly well too if divisor comes from an integer, as there won’t be precision issues… or at least not too much.

    EDIT: fourth attempt, by @ybungalobill

    The previous attempt does not deal well with situations where num/divisor errors on the wrong side. Like 1.999/1.000 –> 0.999, it’s nearly divisor so we should indicate equality, yet it failed.

    // divisor is deemed non-zero
    mod = fabs(fmod(num/divisor, 1));
    
    if (mod <= 0.001 || fabs(1 - mod) <= 0.001) { }
    

    Looks like a never ending task eh ?


    There is still cause for troubles though.

    double has a limited precision, that is a limited number of digits that is representable (16 I think ?). This precision might be insufficient to represent an integer:

    Integer n = 12345678901234567890;
    double d = n; // 1.234567890123457 * 10^20
    

    This truncation means it is impossible to map it back to its original value. This should not cause any issue with double and int, for example on my platform double is 8 bytes and int is 4 bytes, so it would work, but changing double to float or int to long could violate this assumption, oh hell!

    Are you sure you really need floating point, by the way ?

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

Sidebar

Related Questions

Long time listener - first time caller. I am hoping to get some advice.
Long time listener, first time caller. I'm a full time SE during the day
Hey Overflow. Long time listener, first time caller. So here's what I've got: I'm
Hey, long time listener first time caller, and I'm asking a question related to
long time listener. First time caller... Not strictly a PHP question as it involves
Long time listener, first time caller. Does anyone know of a good interop library
As they say on the radio - long time listener first time caller.... Here's
long-time listener, first-time caller! I’m trying to connect to MySQL using the ADO.NET Entity
Long time listener, first time caller… I've got two questions in a form, the
Long time lurker, first time poster. I'm making a ServerConnection module to make it

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.