Author Topic: Convert Decimal to Fraction  (Read 12375 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Convert Decimal to Fraction
« Reply #15 on: April 24, 2019, 11:42:50 am »
Funny, never thought of myself as a string variable. I'm more of a numeric type, although I do get parsed off at times.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #16 on: April 24, 2019, 04:14:59 pm »
That's wonderful. Basically what I wrote in CLISP many years ago.
Well done.
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #17 on: April 24, 2019, 09:10:04 pm »
R notation changed back to the redundant " repeat " form. This makes it easier to convert the R notation decimal number back to the fraction form if the repeat section is not too long (= over 14 digits).

The trick to converting a repeating decimal number to fraction is to align a second repeating section over the first by multiplying by some power of 10, p,  = length of repeating section. This way when you subtract  x * 10 ^ p - x = a definite finite decimal, d st.

x * (10 ^ p - 1) = d
then x = d / (10 ^ p - 1) a fraction!

QB64 can only process p up to around 14 digits in the repeating section of the decimal.

Code: QB64: [Select]
  1. _TITLE "n slash d to R notation and back again" '2019-04-24
  2. ' from:"Decimal Expansion of Division without Dividing  by bplus 2017-12-03"
  3. ' dove tailing Adrians and my recent dividing programs
  4.  
  5. ' 2019-04-24
  6. ' I want to isolate the repeated section immediately not write the fraction out and then repeat the repeated section
  7. ' not 1/6 = .16R6 >>> but .1R6
  8.  
  9. 'hmm... look like have to cycle through twice to get the length of the repeat section
  10. ' but before returning backup to start of repeat section insert the R where it starts the first time
  11. ' and end it where " repeat " starts.
  12.  
  13. '2019-04-24
  14. ' just for kicks, convert the R notation back to fraction if possible.
  15. ' Had to revert back to the redundant " repeat " form of R notation.
  16.  
  17. DEFLNG A-Z
  18.     PRINT: PRINT "Enter 2 integers < 3200, numerator / denominator, 0's quit, don't forget / "
  19.     INPUT nd$
  20.     slash = INSTR(nd$, "/")
  21.     IF slash THEN
  22.         dvsr = VAL(MID$(nd$, slash + 1))
  23.         IF dvsr = 0 THEN PRINT "Divisor is 0, bye.": END
  24.         numerator = VAL(MID$(nd$, 1, slash - 1))
  25.         IF numerator = 0 THEN PRINT "Numerator is 0, bye.": END
  26.     ELSE
  27.         PRINT "No slash found, bye.": END
  28.     END IF
  29.     CLS
  30.     d$ = divide$(numerator, dvsr)
  31.     PRINT numerator; " / "; dvsr; " = "; d$
  32.  
  33.     'and now arttemp"t to convert back to fraction
  34.     PRINT: PRINT "Check if can convert back to fraction:"
  35.     result$ = convertRnotation2Fraction$(d$)
  36.     IF result$ <> "" THEN PRINT result$
  37.  
  38. FUNCTION convertRnotation2Fraction$ (rNotedDecimal$)
  39.     'check if R in the decimal
  40.     dotPos = INSTR(rNotedDecimal$, ".")
  41.     IF dotPos = 0 THEN convertRnotation2Fraction$ = rNotedDecimal$: EXIT FUNCTION
  42.     RPos = INSTR(rNotedDecimal$, " repeat ")
  43.     IF RPos = 0 THEN convertRnotation2Fraction$ = convert2Fraction$(rNotedDecimal$): EXIT FUNCTION
  44.  
  45.     'still here? we have an R and a decimal
  46.     whole$ = MID$(rNotedDecimal$, 1, dotPos - 1)
  47.     IF VAL(whole$) = 0 THEN whole$ = ""
  48.  
  49.     p = LEN(rNotedDecimal$) - RPos - LEN(" repeat ") + 1
  50.     PRINT "Debug: repeat length ="; p
  51.     IF p > 15 THEN
  52.         PRINT "The length of the repeat section of: "
  53.         PRINT rNotedDecimal$
  54.         PRINT " is too long to convert back to fraction."
  55.         EXIT FUNCTION
  56.     END IF
  57.  
  58.     dec$ = MID$(rNotedDecimal$, dotPos)
  59.     PRINT "Debug: converting dec$ "; dec$
  60.  
  61.     'remove " repeat "
  62.     RPos = INSTR(dec$, " repeat ")
  63.     dec1$ = MID$(dec$, 1, RPos - 1) + MID$(dec$, RPos + LEN(" repeat "))
  64.     dec2$ = MID$(dec$, 1, RPos - 1)
  65.     PRINT "Debug: dec1$ (double repeat), dec2$ (single repeat) = "; dec1$; ", "; dec2$
  66.  
  67.     'mult by 10^p to get the 2nd repeat part in dec1$ aligned to 1st repeat part  in dec2$
  68.     vd1## = VAL(dec1$) * 10 ^ p
  69.     vd2## = VAL(dec2$)
  70.     n## = vd1## - vd2## 'subtract dec2$ from dec1$
  71.  
  72.     adj&& = 1 'convert to whole numbers
  73.     WHILE n## <> INT(n##)
  74.         adj&& = adj&& * 10
  75.         n## = n## * 10
  76.     WEND
  77.  
  78.     'reevaluate to avoid rounding errors from crazy floating point math
  79.     n1&& = vd1## * adj&& - vd2## * adj&&
  80.     PRINT "Debug values: vd1, vd2, adj&&, n1&& (difference * adj&& for whole number):"
  81.     PRINT vd1##; ", "; vd2##; ", "; adj&&; ", "; n1&&
  82.  
  83.     d&& = (10 ^ p - 1) * adj&&
  84.     PRINT "Debug: Giant numerator, denominator "; n1&&; ", "; d&& 'giant numbers
  85.  
  86.     'reduce giant numbers by Gretaest Common Divisor between them
  87.     g&& = gcd&&(n1&&, d&&): sn&& = n1&& / g&&: sd&& = d&& / g&&
  88.  
  89.     convertRnotation2Fraction$ = whole$ + " " + _TRIM$(STR$(sn&&)) + "/" + _TRIM$(STR$(sd&&))
  90.  
  91. FUNCTION convert2Fraction$ (decimal$)
  92.     dot%% = INSTR(decimal$, ".")
  93.     IF dot%% > 0 THEN
  94.         whole$ = MID$(decimal$, 1, dot%% - 1)
  95.         IF VAL(whole$) = 0 THEN whole$ = ""
  96.         p%% = LEN(decimal$) - dot%%
  97.         n&& = VAL(MID$(decimal$, dot%% + 1))
  98.         d&& = 10 ^ p%%
  99.         g&& = gcd&&(n&&, d&&): sn&& = n&& / g&&: sd&& = d&& / g&&
  100.         convert2Fraction$ = whole$ + " " + _TRIM$(STR$(sn&&)) + "/" + _TRIM$(STR$(sd&&))
  101.     ELSE
  102.         convert2Fraction$ = decimal$
  103.     END IF
  104.  
  105. FUNCTION gcd&& (a&&, b&&)
  106.     'a and b will be changed unless make copies
  107.     c&& = a&&: d&& = b&&
  108.     WHILE c&& <> 0 AND d&& <> 0
  109.         IF c&& > d&& THEN c&& = c&& MOD d&& ELSE d&& = d&& MOD c&&
  110.     WEND
  111.     gcd&& = c&& + d&&
  112.  
  113.  
  114. FUNCTION divide$ (n, d)
  115.     'n = original product or numerator (preserve value of n)
  116.     'd = divisor  (also preserve value)
  117.     c = n 'copy of n to be reduced until <= d, c will be the remainder part of division
  118.     a = 0 'a is for answer or accumulate, the integer part of the division result
  119.  
  120.     'find lowest power of 10 such that: d * 10^p > n
  121.     p = 0 'power of 10
  122.     WHILE d * (10 ^ p) < n
  123.         p = p + 1
  124.     WEND
  125.     WHILE c >= d
  126.         IF c = d THEN a = a + 1: c = 0: EXIT WHILE
  127.         p = p - 1
  128.         IF p >= 0 THEN
  129.             m = 0
  130.             WHILE d * m * 10 ^ p < c
  131.                 m = m + 1
  132.             WEND
  133.             m = m - 1
  134.             c = c - d * m * 10 ^ p
  135.             a = a + m * 10 ^ p
  136.         END IF
  137.     WEND
  138.  
  139.     'Now for the decimal expansion isolating the repeating part if one
  140.     IF c <> 0 THEN
  141.         DIM b(d)
  142.         b$ = "."
  143.         WHILE c <> 0
  144.  
  145.             'emergency bug out!
  146.             loopct = loopct + 1 'loop count should not exceed 1000 for numbers I am testing
  147.             IF loopct > 1000 THEN PRINT "Error: loop too long, bugging out! ": GOTO skip
  148.  
  149.             'track repeats  b() tracks been here once, b2() tracks been here twice
  150.             IF b(c) = 1 THEN 'been here!
  151.                 IF rFlag = 1 THEN 'been here twice!
  152.                     IF b2(c) = 1 THEN EXIT WHILE 'strike 3, we're out of here
  153.                     b2(c) = 1
  154.                 ELSE
  155.                     rFlag = 1
  156.                     DIM b2(d)
  157.                     b$ = b$ + " repeat "
  158.                     b2(c) = 1
  159.                 END IF
  160.             ELSE
  161.                 b(c) = 1
  162.             END IF
  163.  
  164.             'c was last remainder, mult by 10 and see if some m * d > can reduce it
  165.             tc = 10 * c
  166.             flag = 0
  167.             FOR m = 0 TO 9
  168.                 IF ((tc - m * d) >= 0) AND ((tc - (m + 1) * d) < 0) THEN
  169.                     flag = 1: b$ = b$ + LTRIM$(STR$(m))
  170.                     EXIT FOR
  171.                 END IF
  172.             NEXT
  173.             IF flag = 0 THEN b$ = b$ + "0": m = 0
  174.             c = tc - d * m
  175.         WEND
  176.     END IF
  177.  
  178.     'OK either d divided n eventually or there is a repeated pattern recorded in b$
  179.     skip: '< needed for debugging
  180.     r$ = STR$(a)
  181.     IF b$ <> "" THEN r$ = r$ + b$
  182.     divide$ = r$
  183.  
  184.  
1_79th Convert to r notation and back.PNG
* 1_79th Convert to r notation and back.PNG (Filesize: 37.34 KB, Dimensions: 883x655, Views: 241)
« Last Edit: April 24, 2019, 09:19:29 pm by bplus »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #18 on: April 25, 2019, 11:20:08 am »
I crashed it with 1/62
[edit]
No, I think its just me. I am not on the new version yet. I replaced _trim$ with ltrim$. Seems that didn't work.
« Last Edit: April 25, 2019, 11:22:03 am by Jack002 »
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #19 on: April 25, 2019, 12:00:43 pm »
I crashed it with 1/62
[edit]
No, I think its just me. I am not on the new version yet. I replaced _trim$ with ltrim$. Seems that didn't work.

Hi Jack002,

Oh! looks like I was allowing repeat sections up to 15 because 1/62 was 15. OK on my system QB64 v1.3 on Windows 10,   the code works and arrives back at 1/62.
1 over 62.PNG
« Last Edit: April 25, 2019, 12:05:08 pm by bplus »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #20 on: April 25, 2019, 04:11:13 pm »
I'm on V 1.2, I need to get the new 1.3 version installed.
(all the posts here seem to use the new functions of 1.3, so this version is no good for any of those)
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #21 on: April 25, 2019, 04:21:08 pm »
I'm on V 1.2, I need to get the new 1.3 version installed.
(all the posts here seem to use the new functions of 1.3, so this version is no good for any of those)


I think _TRIM$ is the only new function I am using from v1.3 and replacing it with LTRIM$ should be fine, the only thing it is doing is formatting the output display, it shouldn't interfere with main code. (Though I don't want to discourage you from updating. :)

Jack002 are you using Windows 10 for OS?

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #22 on: April 25, 2019, 10:59:12 pm »
Yes, windows 10 and I changed _trim$ to ltrim$ and it fails.
crash.jpg
* crash.jpg (Filesize: 66.92 KB, Dimensions: 672x462, Views: 241)
« Last Edit: April 25, 2019, 11:03:10 pm by Jack002 »
QB64 is the best!

FellippeHeitor

  • Guest
Re: Convert Decimal to Fraction
« Reply #23 on: April 25, 2019, 11:12:29 pm »
_TRIM$() stands for RTRIM$(LTRIM$()). Try that.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #24 on: April 26, 2019, 10:21:01 am »
It's not _trim$ that is causing problem for 1/62 because, I changed them to LTRIM$ and get same results with extra space on right in display.
 
It's not _trim$.PNG


A variable type is different, I can see that in the negative numbers in Jack002's last line of output and also adj&& is different, I have 10, Jack002s' code has way more for adj&&. Perhaps Jack002 changed more than _TRIM$??

I'd like to compare the code Jack002 is running with mine through WinMerge.

« Last Edit: April 26, 2019, 10:24:50 am by bplus »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #25 on: April 26, 2019, 04:51:46 pm »
No, all I changed was trim. Here it is just how I ran it
[edit]
I just now changed all my ltrims to rtrim$(ltrim$()), no change. It still does the same thing
Code: QB64: [Select]
  1. _TITLE "n slash d to R notation and back again" '2019-04-24
  2. ' from:"Decimal Expansion of Division without Dividing  by bplus 2017-12-03"
  3. ' dove tailing Adrians and my recent dividing programs
  4.  
  5. ' 2019-04-24
  6. ' I want to isolate the repeated section immediately not write the fraction out and then repeat the repeated section
  7. ' not 1/6 = .16R6 >>> but .1R6
  8.  
  9. 'hmm... look like have to cycle through twice to get the length of the repeat section
  10. ' but before returning backup to start of repeat section insert the R where it starts the first time
  11. ' and end it where " repeat " starts.
  12.  
  13. '2019-04-24
  14. ' just for kicks, convert the R notation back to fraction if possible.
  15. ' Had to revert back to the redundant " repeat " form of R notation.
  16.  
  17. DEFLNG A-Z
  18.     PRINT: PRINT "Enter 2 integers < 3200, numerator / denominator, 0's quit, don't forget / "
  19.     INPUT nd$
  20.     slash = INSTR(nd$, "/")
  21.     IF slash THEN
  22.         dvsr = VAL(MID$(nd$, slash + 1))
  23.         IF dvsr = 0 THEN PRINT "Divisor is 0, bye.": END
  24.         numerator = VAL(MID$(nd$, 1, slash - 1))
  25.         IF numerator = 0 THEN PRINT "Numerator is 0, bye.": END
  26.     ELSE
  27.         PRINT "No slash found, bye.": END
  28.     END IF
  29.     CLS
  30.     d$ = divide$(numerator, dvsr)
  31.     PRINT numerator; " / "; dvsr; " = "; d$
  32.  
  33.     'and now arttemp"t to convert back to fraction
  34.     PRINT: PRINT "Check if can convert back to fraction:"
  35.     result$ = convertRnotation2Fraction$(d$)
  36.     IF result$ <> "" THEN PRINT result$
  37.  
  38. FUNCTION convertRnotation2Fraction$ (rNotedDecimal$)
  39.     'check if R in the decimal
  40.     dotPos = INSTR(rNotedDecimal$, ".")
  41.     IF dotPos = 0 THEN convertRnotation2Fraction$ = rNotedDecimal$: EXIT FUNCTION
  42.     RPos = INSTR(rNotedDecimal$, " repeat ")
  43.     IF RPos = 0 THEN convertRnotation2Fraction$ = convert2Fraction$(rNotedDecimal$): EXIT FUNCTION
  44.  
  45.     'still here? we have an R and a decimal
  46.     whole$ = MID$(rNotedDecimal$, 1, dotPos - 1)
  47.     IF VAL(whole$) = 0 THEN whole$ = ""
  48.  
  49.     p = LEN(rNotedDecimal$) - RPos - LEN(" repeat ") + 1
  50.     PRINT "Debug: repeat length ="; p
  51.     IF p > 15 THEN
  52.         PRINT "The length of the repeat section of: "
  53.         PRINT rNotedDecimal$
  54.         PRINT " is too long to convert back to fraction."
  55.         EXIT FUNCTION
  56.     END IF
  57.  
  58.     dec$ = MID$(rNotedDecimal$, dotPos)
  59.     PRINT "Debug: converting dec$ "; dec$
  60.  
  61.     'remove " repeat "
  62.     RPos = INSTR(dec$, " repeat ")
  63.     dec1$ = MID$(dec$, 1, RPos - 1) + MID$(dec$, RPos + LEN(" repeat "))
  64.     dec2$ = MID$(dec$, 1, RPos - 1)
  65.     PRINT "Debug: dec1$ (double repeat), dec2$ (single repeat) = "; dec1$; ", "; dec2$
  66.  
  67.     'mult by 10^p to get the 2nd repeat part in dec1$ aligned to 1st repeat part  in dec2$
  68.     vd1## = VAL(dec1$) * 10 ^ p
  69.     vd2## = VAL(dec2$)
  70.     n## = vd1## - vd2## 'subtract dec2$ from dec1$
  71.  
  72.     adj&& = 1 'convert to whole numbers
  73.     WHILE n## <> INT(n##)
  74.         adj&& = adj&& * 10
  75.         n## = n## * 10
  76.     WEND
  77.  
  78.     'reevaluate to avoid rounding errors from crazy floating point math
  79.     n1&& = vd1## * adj&& - vd2## * adj&&
  80.     PRINT "Debug values: vd1, vd2, adj&&, n1&& (difference * adj&& for whole number):"
  81.     PRINT vd1##; ", "; vd2##; ", "; adj&&; ", "; n1&&
  82.  
  83.     d&& = (10 ^ p - 1) * adj&&
  84.     PRINT "Debug: Giant numerator, denominator "; n1&&; ", "; d&& 'giant numbers
  85.  
  86.     'reduce giant numbers by Gretaest Common Divisor between them
  87.     g&& = gcd&&(n1&&, d&&): sn&& = n1&& / g&&: sd&& = d&& / g&&
  88.  
  89.     convertRnotation2Fraction$ = whole$ + " " + LTRIM$(STR$(sn&&)) + "/" + LTRIM$(STR$(sd&&))
  90.  
  91. FUNCTION convert2Fraction$ (decimal$)
  92.     dot%% = INSTR(decimal$, ".")
  93.     IF dot%% > 0 THEN
  94.         whole$ = MID$(decimal$, 1, dot%% - 1)
  95.         IF VAL(whole$) = 0 THEN whole$ = ""
  96.         p%% = LEN(decimal$) - dot%%
  97.         n&& = VAL(MID$(decimal$, dot%% + 1))
  98.         d&& = 10 ^ p%%
  99.         g&& = gcd&&(n&&, d&&): sn&& = n&& / g&&: sd&& = d&& / g&&
  100.         convert2Fraction$ = whole$ + " " + LTRIM$(STR$(sn&&)) + "/" + LTRIM$(STR$(sd&&))
  101.     ELSE
  102.         convert2Fraction$ = decimal$
  103.     END IF
  104.  
  105. FUNCTION gcd&& (a&&, b&&)
  106.     'a and b will be changed unless make copies
  107.     c&& = a&&: d&& = b&&
  108.     WHILE c&& <> 0 AND d&& <> 0
  109.         IF c&& > d&& THEN c&& = c&& MOD d&& ELSE d&& = d&& MOD c&&
  110.     WEND
  111.     gcd&& = c&& + d&&
  112.  
  113.  
  114. FUNCTION divide$ (n, d)
  115.     'n = original product or numerator (preserve value of n)
  116.     'd = divisor  (also preserve value)
  117.     c = n 'copy of n to be reduced until <= d, c will be the remainder part of division
  118.     a = 0 'a is for answer or accumulate, the integer part of the division result
  119.  
  120.     'find lowest power of 10 such that: d * 10^p > n
  121.     p = 0 'power of 10
  122.     WHILE d * (10 ^ p) < n
  123.         p = p + 1
  124.     WEND
  125.     WHILE c >= d
  126.         IF c = d THEN a = a + 1: c = 0: EXIT WHILE
  127.         p = p - 1
  128.         IF p >= 0 THEN
  129.             m = 0
  130.             WHILE d * m * 10 ^ p < c
  131.                 m = m + 1
  132.             WEND
  133.             m = m - 1
  134.             c = c - d * m * 10 ^ p
  135.             a = a + m * 10 ^ p
  136.         END IF
  137.     WEND
  138.  
  139.     'Now for the decimal expansion isolating the repeating part if one
  140.     IF c <> 0 THEN
  141.         DIM b(d)
  142.         b$ = "."
  143.         WHILE c <> 0
  144.  
  145.             'emergency bug out!
  146.             loopct = loopct + 1 'loop count should not exceed 1000 for numbers I am testing
  147.             IF loopct > 1000 THEN PRINT "Error: loop too long, bugging out! ": GOTO skip
  148.  
  149.             'track repeats  b() tracks been here once, b2() tracks been here twice
  150.             IF b(c) = 1 THEN 'been here!
  151.                 IF rFlag = 1 THEN 'been here twice!
  152.                     IF b2(c) = 1 THEN EXIT WHILE 'strike 3, we're out of here
  153.                     b2(c) = 1
  154.                 ELSE
  155.                     rFlag = 1
  156.                     DIM b2(d)
  157.                     b$ = b$ + " repeat "
  158.                     b2(c) = 1
  159.                 END IF
  160.             ELSE
  161.                 b(c) = 1
  162.             END IF
  163.  
  164.             'c was last remainder, mult by 10 and see if some m * d > can reduce it
  165.             tc = 10 * c
  166.             flag = 0
  167.             FOR m = 0 TO 9
  168.                 IF ((tc - m * d) >= 0) AND ((tc - (m + 1) * d) < 0) THEN
  169.                     flag = 1: b$ = b$ + LTRIM$(STR$(m))
  170.                     EXIT FOR
  171.                 END IF
  172.             NEXT
  173.             IF flag = 0 THEN b$ = b$ + "0": m = 0
  174.             c = tc - d * m
  175.         WEND
  176.     END IF
  177.  
  178.     'OK either d divided n eventually or there is a repeated pattern recorded in b$
  179.     skip: '< needed for debugging
  180.     r$ = STR$(a)
  181.     IF b$ <> "" THEN r$ = r$ + b$
  182.     divide$ = r$
  183.  
  184.  
  185.  
« Last Edit: April 26, 2019, 04:57:02 pm by Jack002 »
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #26 on: April 26, 2019, 05:19:41 pm »
Thanks for your reply Jack002.

Yeah, I run very same and do get back to 1/62 on last line.

Don't know what our difference is????

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #27 on: April 26, 2019, 10:17:56 pm »
I blame Obama

LOL
[edit]

It fails on this line
 g&& = gcd&&(n1&&, d&&)
I don't follow all the logic here, once you make vd1 and vd2 etc you start to lose me.
Does it matter that the value of vd1 is not an integer? Its 16129032258064.52
« Last Edit: April 26, 2019, 11:19:34 pm by Jack002 »
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #28 on: April 27, 2019, 12:18:58 am »
VD1 is not an integer until we multiply it and VD2 by adj&&

Here is an example of converting 1/6 in English:
Quote
Convert a repeating decimal back to fraction:

1/6 = .16R6  = x

10x = 1.666...   VD1
- x =    .166...   VD2
=============
9x   = 1.5      VD1 - VD2

adj = 10  a multiplier to get rid of decimal

90x = 15 or x = 15 /90

GCD(15, 90) = greatest common denominator of 15 and 90 is 15

15/15 = 1   and 90/15 = 6 so 15/90 = 1/6

x = 1/6

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Convert Decimal to Fraction
« Reply #29 on: April 27, 2019, 09:17:23 am »
Ok so vd1 is larger than vd2. You multiply both by adj and take a difference. Won't that number be positive? If so, why is this negative?
 
crash.jpg
QB64 is the best!