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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:44:56+00:00 2026-06-13T18:44:56+00:00

Possible Duplicate: How can I turn a floating point number into the closest fraction

  • 0

Possible Duplicate:
How can I turn a floating point number into the closest fraction represented by a byte numerator and denominator?

I would like to take an arbitrary float or double in Java and convert it to a rational number – ie. a number of the form a/b where a and b are long integers. How can I do this in a reasonably efficient way?

(BTW – I already have code for simplifying the fractions, so it doesn’t matter whether a/b are in their simplest form).

  • 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-13T18:44:57+00:00Added an answer on June 13, 2026 at 6:44 pm

    First see how double (or float but I refer only to double below) is constructed by IEEE-754 rules:

    Then convert double to bits with Double.doubleToLongBits.
    Count the fraction using this method 1 + bit_0 * 2^(-1) + bit_1 * 2^(-2) ....
    Multiply the result with exponent (2^(exponent) to be precise) and with a sign.

    Here is the code:

    double number =  -0.15625;
    // Code below doesn't work for 0 and NaN - just check before
    
    long bits = Double.doubleToLongBits(number);
    
    long sign = bits >>> 63;
    long exponent = ((bits >>> 52) ^ (sign << 11)) - 1023;
    long fraction = bits << 12; // bits are "reversed" but that's not a problem
    
    long a = 1L;
    long b = 1L;
    
    for (int i = 63; i >= 12; i--) {
        a = a * 2 + ((fraction >>> i) & 1);
        b *= 2;
    }
    
    if (exponent > 0)
        a *= 1 << exponent;
    else
        b *= 1 << -exponent;
    
    if (sign == 1)
        a *= -1;
    
    // Here you have to simplify the fraction
    
    System.out.println(a + "/" + b);
    

    But be careful – with big exponents you may run into numbers that won’t fit into your variables. In fact you may consider storing exponent along the fraction and only multiply it if exponent is small enough. If it’s not and you have to display the fraction to the user you may use the scientific notation (that requires solving equation 2^n = x * 10^m where m is your decimal exponent and x is a number you have to multiply the fraction with. But that’s a matter for another question…).

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

Sidebar

Related Questions

Possible Duplicate: How can I turn a list into an array in python? How
Possible Duplicate: Can I turn off impersonation just in a couple instances I've created
Possible Duplicate: how to create an animated gif in .net How would I turn
Possible Duplicate: How to turn a string into a method call? Currently I have
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Can I run from command line program created by Eclipse? I am
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: Can we use any other TAG inside <ul> along with <li>? Or
Possible Duplicate: Can I comment a JSON file? We are using a .json file
Possible Duplicate: Can not connect to Oracle via VBA - Driver's SQLSetConnectAttr Failed I

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.