Help Me!!!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 1
Reputation: piods is an unknown quantity at this point 
Solved Threads: 0
piods piods is offline Offline
Newbie Poster

Help Me!!!

 
0
  #1
Jul 31st, 2008
how to cast a floating point to display an fraction form?? is there a way??
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: Help Me!!!

 
0
  #2
Jul 31st, 2008
How do you convert a decimal to fraction on paper?
Last edited by Colin Mac; Jul 31st, 2008 at 9:32 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Help Me!!!

 
0
  #3
Aug 1st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help Me!!!

 
0
  #4
Aug 1st, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help Me!!!

 
0
  #5
Aug 1st, 2008
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)...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Help Me!!!

 
0
  #6
Aug 1st, 2008
>>But are you ready to get a fraction looks like
No, I will limit it to 5 decimal places
and the other ways.... Please
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help Me!!!

 
0
  #7
Aug 1st, 2008
Some improvisation:
  1. /* It turned up up just at the right moment:
  2. http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/gcd.html
  3.  
  4. PROGRAM GreatestCommonDivisor
  5.   IMPLICIT NONE
  6.  
  7.   INTEGER :: a, b, c
  8.  
  9.   WRITE(*,*) 'Two positive integers please --> '
  10.   READ(*,*) a, b
  11.   IF (a < b) THEN ! since a >= b must be true, they
  12.   c = a ! are swapped if a < b
  13.   a = b
  14.   b = c
  15.   END IF
  16.  
  17.   DO ! now we have a <= b
  18.   c = MOD(a, b) ! compute c, the reminder
  19.   IF (c == 0) EXIT ! if c is zero, we are done. GCD = b
  20.   a = b ! otherwise, b becomes a
  21.   b = c ! and c becomes b
  22.   END DO ! go back
  23.  
  24.   WRITE(*,*) 'The GCD is ', b
  25.  
  26. END PROGRAM GreatestCommonDivisor
  27.  */
  28. int GreatestCommonDivisor(int a, int b)
  29. {
  30. int c;
  31.  
  32. if (a < 0) a = -a;
  33. if (b < 0) b = -b;
  34.  
  35. if (a < b)
  36. {
  37. c = a; a = b; b = c;
  38. }
  39. if (b == 0)
  40. return a;
  41. for (;;)
  42. {
  43. c = a % b;
  44. if (c == 0)
  45. break;
  46. a = b;
  47. b = c;
  48. }
  49. return b;
  50. }
  51.  
  52. char *double2fraction(char* buf, int bufsize, double x, int maxdigits)
  53. {
  54. double intpart;
  55. double f;
  56. int negative = 0;
  57. int i, gcd, numerator, divisor;
  58. char temp[64];
  59. int templen;
  60.  
  61. if (!buf || bufsize < 2)
  62. return 0;
  63. else if (x == 0.0)
  64. {
  65. buf[0] = '0'; buf[1] = '\0';
  66. }
  67.  
  68. if (x < 0.0)
  69. {
  70. x = -x; negative = 1;
  71. }
  72.  
  73. if (maxdigits <= 0)
  74. maxdigits = 5;
  75. else if (maxdigits > 9)
  76. maxdigits = 9;
  77.  
  78. divisor = 1;
  79. for (i = 0; i < maxdigits; ++i)
  80. divisor *= 10;
  81. f = modf(x,&intpart);
  82.  
  83. numerator = (int)(f*divisor+0.5);
  84.  
  85. if (numerator != 0)
  86. {
  87. while ((gcd = GreatestCommonDivisor(divisor,numerator)) > 1)
  88. {
  89. divisor /= gcd;
  90. numerator /= gcd;
  91. }
  92. sprintf(temp,"%s%.0f/%d",(negative?"-":""),intpart*divisor+numerator,divisor);
  93. }
  94. else
  95. sprintf(temp,"%.0f",intpart);
  96. templen = strlen(temp);
  97. if (templen+1 < bufsize)
  98. strcpy(buf,temp);
  99. else
  100. {
  101. buf[0] = '*'; buf[1] = '\0';
  102. }
  103. return buf;
  104. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help Me!!!

 
0
  #8
Aug 1st, 2008
A little thorough version:
  1. int greatestCommonDivisor(int a, int b)
  2. {
  3. int t;
  4.  
  5. if (a < 0) a = -a;
  6. if (b < 0) b = -b;
  7. if (a < b)
  8. {
  9. t = a; a = b; b = t;
  10. }
  11. while (b)
  12. {
  13. t = b;
  14. b = a % b;
  15. a = t;
  16. }
  17. return a;
  18. }
  19.  
  20. char *double2fraction(char* buf, int bufsize, double x, int maxdigits)
  21. {
  22. double intpart;
  23. double f;
  24. int negative = 0;
  25. int i, gcd, numerator, divisor;
  26. char temp[64];
  27. int templen;
  28.  
  29. if (!buf || bufsize < 2)
  30. return 0;
  31.  
  32. if (x == 0.0)
  33. {
  34. buf[0] = '0'; buf[1] = '\0';
  35. return buf;
  36. }
  37.  
  38. if (x < 0.0)
  39. {
  40. x = -x; negative = 1;
  41. }
  42.  
  43. if (maxdigits <= 0)
  44. maxdigits = 5;
  45. else if (maxdigits > 9)
  46. maxdigits = 9;
  47.  
  48. divisor = 1;
  49. for (i = 0; i < maxdigits; ++i)
  50. divisor *= 10;
  51.  
  52. f = modf(x,&intpart);
  53. numerator = (int)(f*divisor+0.5);
  54.  
  55. if (numerator != 0)
  56. {
  57. while ((gcd = greatestCommonDivisor(divisor,numerator)) > 1)
  58. {
  59. divisor /= gcd;
  60. numerator /= gcd;
  61. }
  62. sprintf(temp,"%s%.0f/%d",(negative?"-":""),
  63. intpart*divisor+numerator,divisor);
  64. }
  65. else
  66. sprintf(temp,"%.0f",intpart);
  67.  
  68. templen = strlen(temp);
  69. if (templen+1 < bufsize)
  70. strcpy(buf,temp);
  71. else
  72. {
  73. buf[0] = '*'; buf[1] = '\0';
  74. }
  75. return buf;
  76. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Help Me!!!

 
0
  #9
Aug 1st, 2008
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
Last edited by ssharish2005; Aug 1st, 2008 at 6:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help Me!!!

 
0
  #10
Aug 1st, 2008
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)...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC