Don't make me REPETEND myself...
#14
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
If eggs are brain food, Biden takes his scrambled.
Reply


Messages In This Thread
Don't make me REPETEND myself... - by Pete - 07-31-2022, 08:57 PM
RE: Don't make me REPETEND myself... - by bplus - 07-31-2022, 11:20 PM
RE: Don't make me REPETEND myself... - by Pete - 08-01-2022, 01:00 AM
RE: Don't make me REPETEND myself... - by bplus - 08-01-2022, 03:57 PM
RE: Don't make me REPETEND myself... - by Pete - 08-01-2022, 07:10 PM
RE: Don't make me REPETEND myself... - by Jack - 08-01-2022, 07:47 PM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 12:30 AM
RE: Don't make me REPETEND myself... - by Jack - 08-03-2022, 01:39 AM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 02:49 AM
RE: Don't make me REPETEND myself... - by Stuart - 08-03-2022, 08:21 AM
RE: Don't make me REPETEND myself... - by Dav - 08-03-2022, 10:37 AM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 05:39 PM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 06:48 PM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 10:43 PM
RE: Don't make me REPETEND myself... - by Stuart - 08-04-2022, 07:13 AM
RE: Don't make me REPETEND myself... - by Pete - 08-04-2022, 08:37 AM
RE: Don't make me REPETEND myself... - by Jack - 08-04-2022, 09:23 AM
RE: Don't make me REPETEND myself... - by Stuart - 08-04-2022, 10:30 AM
RE: Don't make me REPETEND myself... - by bplus - 08-04-2022, 01:26 PM
RE: Don't make me REPETEND myself... - by Pete - 08-04-2022, 06:30 PM
RE: Don't make me REPETEND myself... - by Pete - 08-04-2022, 09:17 PM
RE: Don't make me REPETEND myself... - by Jack - 08-04-2022, 10:37 PM
RE: Don't make me REPETEND myself... - by Pete - 08-05-2022, 10:05 AM
RE: Don't make me REPETEND myself... - by Pete - 08-14-2022, 11:48 AM



Users browsing this thread: 9 Guest(s)