So next up is a way to convert decimals to fractions. This is a test but if it works in all cases, it would be easy to set it up as a conversion calculator. Of course for now, I've left out the string math, so it is limited to around a dozen digits before VAL() craps out. That won't be a problem once it is plugged into the string math routine, later...
Code: (Select All)
repetend_class$ = "mixed"
n$ = ".076923076923" ' 1/13th.
IF repetend_class$ = "single" THEN
' Infinite repeating of one digit...
' Loop until the first digit after the decimal is non-zero: i%
i% = 0
DO
i% = i% + 1
x$ = MID$(n$, 1 + i%, 1)
IF VAL(x$) THEN EXIT DO
IF i% = LEN(n$) THEN BEEP: EXIT DO ' Error all zeros.
LOOP
a$ = LTRIM$(STR$(INT(VAL(n$) * 10 ^ (i% + 1)) - INT(VAL(n$) * 10 ^ i%)))
b$ = LTRIM$(STR$(10 ^ (i% + 1) - 10 ^ i%))
ELSE
' Infinite repeating of two or more digits...
i% = 0
DO
i% = i% + 1
x$ = MID$(n$, 1 + i%, 1)
IF VAL(x$) THEN EXIT DO
IF i% = LEN(n$) THEN BEEP: EXIT DO ' Error all zeros.
LOOP
a$ = LTRIM$(STR$(VAL(MID$(n$ + MID$(n$, 2), 2, LEN(n$) - 1 + i%)) - VAL(MID$(n$, 2, i%))))
b$ = LTRIM$(STR$(10 ^ (LEN(n$) - 1 + i%) - 10 ^ i%))
END IF
' GFC algorithm. -------------------------------------------------------------
gfca$ = a$: gfcb$ = b$
IF VAL(gfca$) < VAL(gfcb$) THEN SWAP gfca$, gfcb$
DO
i = i + 1
x = VAL(gfca$) MOD VAL(gfcb$)
SWAP gfca$, gfcb$: gfcb$ = LTRIM$(STR$(x))
IF x = 0 THEN EXIT DO
LOOP
PRINT "Decimal: "; n$
PRINT "Loops:"; i; " "
PRINT "Conversion: "; a$; " / "; b$; " "
PRINT "GFC = "; gfca$; " "
PRINT "Fraction: "; LTRIM$(STR$(VAL(a$) / VAL(gfca$))) + "/" + LTRIM$(STR$(VAL(b$) / VAL(gfca$)))
END
Shoot first and shoot people who ask questions, later.