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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:15:43+00:00 2026-06-14T07:15:43+00:00

Say if I have an NSNumber, which is something between 0 and 1, and

  • 0

Say if I have an NSNumber, which is something between 0 and 1, and it can be represented using X/Y, how do I calculate the X and Y in this case? I don’t want to compare:

if (number.doubleValue == 0.125)
{
    X = 1;
    Y = 8;
}

so I get 1/8 for 0.125

  • 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-14T07:15:44+00:00Added an answer on June 14, 2026 at 7:15 am

    That’s relatively straightforward. For example, 0.375 is equivalent to 0.375/1.

    First step is to multiply numerator and denominator until the numerator is an integral value (a), giving you 375/1000.

    Then find the greatest common divisor and divide both numerator and denominator by that.

    A (recursive) function for GCD is:

    int gcd (int a, int b) {
        return (b == 0) ? a : gcd (b, a%b);
    }
    

    If you call that with 375 and 1000, it will spit out 125 so that, when you divide the numerator and denominator by that, you get 3/8.


    (a) As pointed out in the comments, there may be problems with numbers that have more precision bits than your integer types (such as IEEE754 doubles with 32-bit integers). You can solve this by choosing integers with a larger range (longs, or a bignum library like MPIR) or choosing a “close-enough” strategy (consider it an integer when the fractional part is relatively insignificant compared to the integral part).

    Another issue is the fact that some numbers don’t even exist in IEEE754, such as the infamous 0.1 and 0.3.

    Unless a number can be represented as the sum of 2-n values where n is limited by the available precision (such as 0.375 being 1/4 + 1/8), the best you can hope for is an approximation.

    Example, consider the single-precision (you’ll see why below, I’m too lazy to do the whole 64 bits) 1/3. As a single precision value, this is stored as:

    s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
    0 01111101 01010101010101010101010
    

    In this example, the sign is 0 hence it’s a positive number.

    The exponent bits give 125 which, when you subtract the 127 bias, gives you -2. Hence the multiplier will be 2-2, or 0.25.

    The mantissa bits are a little trickier. They form the sum of an explicit 1 along with all the 2-n values for the 1 bits, where n is 1 through 23 (left to right. So the mantissa is calculated thus:

    s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
    0 01111101 01010101010101010101010
                | | | | | | | | | | |
                | | | | | | | | | | +-- 0.0000002384185791015625
                | | | | | | | | | +---- 0.00000095367431640625
                | | | | | | | | +------ 0.000003814697265625
                | | | | | | | +-------- 0.0000152587890625
                | | | | | | +---------- 0.00006103515625
                | | | | | +------------ 0.000244140625
                | | | | +-------------- 0.0009765625
                | | | +---------------- 0.00390625
                | | +------------------ 0.015625
                | +-------------------- 0.0625
                +---------------------- 0.25
                               Implicit 1
                                        ========================
                                        1.3333332538604736328125
    

    When you multiply that by 0.25 (see exponent earlier), you get:

    0.333333313465118408203125
    

    Now that’s why they say you only get about 7 decimal digits of precision (15 for IEEE754 double precision).

    Were you to pass that actual number through my algorithm above, you would not get 1/3, you would instead get:

     5,592,405
    ---------- (or 0.333333313465118408203125)
    16,777,216
    

    But that’s not a problem with the algorithm per se, more a limitation of the numbers you can represent.

    Thaks to Wolfram Alpha for helping out with the calculations. If you ever need to do any math that stresses out your calculator, that’s one of the best tools for the job.


    As an aside, you’ll no doubt notice the mantissa bits follow a certain pattern: 0101010101.... This is because 1/3 is an infinitely recurring binary value as well as an infinitely recurring decimal one. You would need and infinite number of 01 bits at the end to exactly represent 1/3 exactly.

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

Sidebar

Related Questions

Okay say I have a function like below: -(NSNumber *)calculate{ NSNumber *myNum = [[[NSNumber
Say I have a number, double, or NSNumber, 3.333333 I want that to turn
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have a Telerik MVC Grid, AJAX bound and I want to put
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Problem: Lets say I have a equation: y = x^2 Using core-plot I doing
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to
i have an enumeration say gender, now i want to associate it to string
Say have a linear model LM that I want a qq plot of the
let say have element like this <div class=watch-me style=display: none;>Watch Me Please</div> as we

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.