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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:29:56+00:00 2026-06-17T09:29:56+00:00

I just wrote the following C++ code in order to convert a fractional number

  • 0

I just wrote the following C++ code in order to convert a fractional number into its corresponding binary format.

double get_decimal_part(double num) {
   long x = static_cast<long>(num);
   return (num - static_cast<double>(x));
}

long get_real_part(double num) {
   return static_cast<long>(num);
}

string fraction_to_binary(double num) {
   string decimal_binary = "";
   double decimal_part = get_decimal_part(num);
   while ( decimal_part > 0 ) {
      double temp = decimal_part * 2;
      if ( get_real_part(temp) == 0 ) decimal_binary += "0";
      else                            decimal_binary += "1";
      decimal_part = get_decimal_part(temp); 
   }
   return decimal_binary;
}

int main() {
   cout << "3.50 - " << fraction_to_binary(3.50) << endl;
   cout << "3.14 - " << fraction_to_binary(3.14) << endl;
}

The output would be :-

3.50 - 1
3.14 - 001000111101011100001010001111010111000010100011111

I’d have the following questions regarding the same :-

  1. In the case if “3.50”, my implementation would give “1” as the output — how can I go about modifying my implementation in order to account for the trailing “0” in 3.50?
  2. If there were any library functions that could help me get the precision of a floating point number? I’m guessing I could use that information to modify my implementation.

[EDIT]
I also tried using the following to convert a float to a string but it wouldnt help either.

   stringstream ss;
   ss << my_float;
   cout << string(ss.str()) << endl;
  • 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-17T09:29:57+00:00Added an answer on June 17, 2026 at 9:29 am

    Before answering your specific questions, what’s wrong with
    modf for this?

    With regards to your specific questions:

    1. What trailing "0"? You’re talking about a text
      representation here. Inside the machine, "3.5" and "3.50"
      correspond to the same number, and have the same representation.

    2. There is a library function which returns the precision of a
      floating point number: std::numeric_limits<double>::digits
      (except that it isn’t a function, but a constant). But if you
      want to break a number down into its integral and whole number
      parts, modf fits the bill exactly. And unlike your code, will
      actually work, for all values of double.

    EDIT:

    Looking closer at the larger picture of what you are trying to
    do: my approach would be to use frexp to extract the base 2
    exponent, then ldexp to scale the number into the range
    [0.5...1) Then loop std::numeric_limits<double>::digits
    times, each time multiplying by 2, and checking that: if the
    results of the multiplication are less than 1, then insert a 0
    digit; otherwise, insert a 1 digit and subtract 1. (Note
    that all of the above actions will be exact if the machine
    floating point is base 2, or a power of 2.)

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

Sidebar

Related Questions

I just wrote the following line of code: if (++(data_ptr->count) > threshold) { /*...*/
I wrote the following code in order to find the newest files added in
I wrote the following: <html> <head> </head> <body> <input type=date /> </body> </html> Just
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to
I just wrote some code to test the behavior of std::equal, and came away
I wrote the following code to pop a property from an object as if
I wrote the below code in order to check for three files and whichever
I just wrote some C code: #include <stdlib.h> #include <time.h> #include <string.h> typedef struct
Just wrote a web service and try to return columns of a sharepoint list
I just wrote a very simple perl tk script to read a huge text

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.