how to cast a floating point to display an fraction form?? is there a way??

Recommended Answers

All 9 Replies

How do you convert a decimal to fraction on paper?

Is there a way - sure.

You have to code it.

5.xxxx will become 5 (xxxx/10000) then just find the gcd of xxxx & 10000. Divide either with it & you have what you want. 3 ints representing the float.

There could be other ways.

Yes, of course.
But are you ready to get a fraction looks like

1/(2*1021) - denominator is an integer number with ~308 digits

?
See <float.h> header, DBL_MIN_EXP and DBL_MIN definitions...

Sorry for misprinting in my prev post: use power (not multiplication) op sign in

1/(2**1021) - denominator is an integer number with ~308 digits

(thank you, Fortran)...

>>But are you ready to get a fraction looks like
No, I will limit it to 5 decimal places;)
and the other ways.... Please

Some improvisation:

/* It turned up up just at the right moment: 
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/gcd.html 

PROGRAM  GreatestCommonDivisor
   IMPLICIT  NONE

   INTEGER   :: a, b, c

   WRITE(*,*) 'Two positive integers please --> '
   READ(*,*)  a, b
   IF (a < b) THEN       ! since a >= b must be true, they
      c = a              ! are swapped if a < b
      a = b
      b = c
   END IF

   DO                    ! now we have a <= b
      c = MOD(a, b)      !    compute c, the reminder
      IF (c == 0) EXIT   !    if c is zero, we are done.  GCD = b
      a = b              !    otherwise, b becomes a
      b = c              !    and c becomes b
   END DO                !    go back

   WRITE(*,*) 'The GCD is ', b

END PROGRAM  GreatestCommonDivisor
 */
int GreatestCommonDivisor(int a, int b)
{
    int c;

    if (a < 0) a = -a;
    if (b < 0) b = -b;
	
    if (a < b)
    {
        c = a; a = b; b = c;
    }
    if (b == 0)
       return a;
    for (;;)
   {
        c = a % b;
        if (c == 0)
            break;
        a = b;
        b = c;
    }
    return b;
}

char *double2fraction(char* buf, int bufsize, double x, int maxdigits)
{
    double intpart;
    double f;
    int negative = 0;
    int i, gcd, numerator, divisor;
    char temp[64];
    int templen;

    if (!buf || bufsize < 2)
        return 0;
    else if (x == 0.0)
    {
        buf[0] = '0'; buf[1] = '\0';
    }

    if (x < 0.0)
    {
        x = -x; negative = 1;
    }

    if (maxdigits <= 0)
        maxdigits = 5;
    else if (maxdigits > 9)
        maxdigits = 9;

    divisor = 1;
    for (i = 0; i < maxdigits; ++i)
        divisor *= 10;
    f = modf(x,&intpart);

    numerator = (int)(f*divisor+0.5);
	
    if (numerator != 0)
    {
        while ((gcd = GreatestCommonDivisor(divisor,numerator)) > 1)
        {
            divisor   /= gcd;
            numerator /= gcd;
        }
        sprintf(temp,"%s%.0f/%d",(negative?"-":""),intpart*divisor+numerator,divisor);
    }
    else
        sprintf(temp,"%.0f",intpart);
    templen = strlen(temp);
    if (templen+1 < bufsize)
        strcpy(buf,temp);
    else
    {
        buf[0] = '*'; buf[1] = '\0';
    }
    return buf;
}

A little thorough version:

int greatestCommonDivisor(int a, int b)
{
    int t;

    if (a < 0) a = -a;
    if (b < 0) b = -b;
    if (a < b)
    {
        t = a; a = b; b = t;
    }
   while (b)
   {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

char *double2fraction(char* buf, int bufsize, double x, int maxdigits)
{
    double intpart;
    double f;
    int negative = 0;
    int i, gcd, numerator, divisor;
    char temp[64];
    int templen;

    if (!buf || bufsize < 2)
        return 0;

    if (x == 0.0)
   {
        buf[0] = '0'; buf[1] = '\0';
        return buf;
    }

    if (x < 0.0)
    {
        x = -x; negative = 1;
    }

    if (maxdigits <= 0)
        maxdigits = 5;
    else if (maxdigits > 9)
        maxdigits = 9;

    divisor = 1;
    for (i = 0; i < maxdigits; ++i)
        divisor *= 10;

    f = modf(x,&intpart);
    numerator = (int)(f*divisor+0.5);
	
    if (numerator != 0)
    {
        while ((gcd = greatestCommonDivisor(divisor,numerator)) > 1)
        {
            divisor   /= gcd;
            numerator /= gcd;
        }
        sprintf(temp,"%s%.0f/%d",(negative?"-":""),
        intpart*divisor+numerator,divisor);
    }
    else
        sprintf(temp,"%.0f",intpart);

    templen = strlen(temp);
    if (templen+1 < bufsize)
        strcpy(buf,temp);
    else
    {
        buf[0] = '*'; buf[1] = '\0';
    }
    return buf;
}

The only thing which you need to work around is to find the no of digit after the decimal point. If you can get the no of digits you could pow function to get the denominator and the numerator would be just the original number with no decimal point in it.

ssharish

What is it: "the no of digit after the decimal point"? For double value of 1/3, for example?

That's a problem (strictly speaking, we always have ~16 digits after decimal point for IEEE floats;))...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.