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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:03:51+00:00 2026-05-11T02:03:51+00:00

As part of a numerical library test I need to choose base 10 decimal

  • 0

As part of a numerical library test I need to choose base 10 decimal numbers that can be represented exactly in base 2. How do you detect in C++ if a base 10 decimal number can be represented exactly in base 2?

My first guess is as follows:

bool canBeRepresentedInBase2(const double &pNumberInBase10) {     //check if a number in base 10 can be represented exactly in base 2     //reference: http://en.wikipedia.org/wiki/Binary_numeral_system     bool funcResult = false;      int nbOfDoublings = 16*3;     double doubledNumber = pNumberInBase10;     for (int i = 0; i < nbOfDoublings ; i++)     {         doubledNumber = 2*doubledNumber;         double intPart;         double fracPart = modf(doubledNumber/2, &intPart);         if (fracPart == 0) //number can be represented exactly in base 2         {             funcResult = true;             break;         }     }     return funcResult; } 

I tested this function with the following values: -1.0/4.0, 0.0, 0.1, 0.2, 0.205, 1.0/3.0, 7.0/8.0, 1.0, 256.0/255.0, 1.02, 99.005. It returns true for -1.0/4.0, 0.0, 7.0/8.0, 1.0, 99.005 which is correct.

Any better ideas?

  • 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. 2026-05-11T02:03:52+00:00Added an answer on May 11, 2026 at 2:03 am

    I think what you are looking for is a number which has a fractional portion which is the sum of a sequence of negative powers of 2 (aka: 1 over a power of 2). I believe this should always be able to be represented exactly in IEEE floats/doubles.

    For example:

    0.375 = (1/4 + 1/8) which should have an exact representation.

    If you want to generate these. You could try do something like this:

    #include <iostream> #include <cstdlib>  int main() {     srand(time(0));     double value = 0.0;     for(int i = 1; i < 256; i *= 2) {         // doesn't matter, some random probability of including this         // fraction in our sequence..         if((rand() % 3) == 0) {             value += (1.0 / static_cast<double>(i));                 }     }     std::cout << value << std::endl; } 

    EDIT: I believe your function has a broken interface. It would be better if you had this:

    bool canBeRepresentedExactly(int numerator, int denominator); 

    because not all fractions have exact representations, but the moment you shove it into a double, you’ve chosen a representation in binary… defeating the purpose of the test.

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

Sidebar

Related Questions

I need to remove leading zeroes from the numerical part of an expression (using
Part of the GUI I'm building using tkinter has a pop-up window that says
Part of my web application is a background script that polls from a beanstalkd
Part of my app caches web pages for offline viewing. To do that, I
Part of my table design is to include a IsDeleted BIT column that is
Part of a complex query that our app is running contains the lines: ...(inner
As part of a program I'm writing, I need to solve a cubic equation
As part of my project I need to solve a quartic polynomial in a
The XML Schema Part 2 specifies that an instance of a datatype that is
I'm writing a program for a numerical simulation in C. Part of the simulation

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.