_TITLE "n slash d to R notation and back again" '2019-04-24 ' from:"Decimal Expansion of Division without Dividing by bplus 2017-12-03"
' dove tailing Adrians and my recent dividing programs
' 2019-04-24
' I want to isolate the repeated section immediately not write the fraction out and then repeat the repeated section
' not 1/6 = .16R6 >>> but .1R6
'hmm... look like have to cycle through twice to get the length of the repeat section
' but before returning backup to start of repeat section insert the R where it starts the first time
' and end it where " repeat " starts.
'2019-04-24
' just for kicks, convert the R notation back to fraction if possible.
' Had to revert back to the redundant " repeat " form of R notation.
'2019-04-27 Thanks to Jack002's comments and replies this code to calculate adj&& may have been improved.
PRINT:
PRINT "Enter 2 integers < 3200, numerator / denominator, 0's quit, don't forget / " numerator
= VAL(MID$(nd$
, 1, slash
- 1)) d$ = divide$(numerator, dvsr)
PRINT numerator;
" / "; dvsr;
" = "; d$
'and now arttemp"t to convert back to fraction
PRINT:
PRINT "Check if can convert back to fraction:" result$ = convertRnotation2Fraction$(d$)
FUNCTION convertRnotation2Fraction$
(rNotedDecimal$
) 'check if R in the decimal
dotPos
= INSTR(rNotedDecimal$
, ".") RPos
= INSTR(rNotedDecimal$
, " repeat ") IF RPos
= 0 THEN convertRnotation2Fraction$
= convert2Fraction$
(rNotedDecimal$
):
EXIT FUNCTION
'still here? we have an R and a decimal
whole$
= MID$(rNotedDecimal$
, 1, dotPos
- 1)
p
= LEN(rNotedDecimal$
) - RPos
- LEN(" repeat ") + 1 PRINT "Debug: repeat length ="; p
PRINT "The length of the repeat section of: " PRINT " is too long to convert back to fraction."
dec$
= MID$(rNotedDecimal$
, dotPos
) PRINT "Debug: converting dec$ "; dec$
'remove " repeat "
RPos
= INSTR(dec$
, " repeat ") dec1$
= MID$(dec$
, 1, RPos
- 1) + MID$(dec$
, RPos
+ LEN(" repeat ")) dec2$
= MID$(dec$
, 1, RPos
- 1) PRINT "Debug: dec1$ (double repeat), dec2$ (single repeat) = "; dec1$;
", "; dec2$
'mult by 10^p to get the 2nd repeat part in dec1$ aligned to 1st repeat part in dec2$
vd1##
= VAL(dec1$
) * 10 ^ p
n## = vd1## - vd2## 'subtract dec2$ from dec1$
'adj&& = 1 'convert to whole numbers
'WHILE n## <> INT(n##)
' adj&& = adj&& * 10
' n## = n## * 10
'WEND
'calculate adj&& from length of decimal
adj&& = 10 ^ p2
'reevaluate to avoid rounding errors from crazy floating point math
n1&& = vd1## * adj&& - vd2## * adj&&
PRINT "Debug values: vd1, vd2, adj&&, n1&& (difference * adj&& for whole number):" PRINT vd1##;
", "; vd2##;
", "; adj&&;
", "; n1&&
d&& = (10 ^ p - 1) * adj&&
PRINT "Debug: Giant numerator, denominator "; n1&&;
", "; d&&
'giant numbers
'reduce giant numbers by Gretaest Common Divisor between them
g&& = gcd&&(n1&&, d&&): sn&& = n1&& / g&&: sd&& = d&& / g&&
dot%%
= INSTR(decimal$
, ".") whole$
= MID$(decimal$
, 1, dot%%
- 1) p%%
= LEN(decimal$
) - dot%%
n&&
= VAL(MID$(decimal$
, dot%%
+ 1)) d&& = 10 ^ p%%
g&& = gcd&&(n&&, d&&): sn&& = n&& / g&&: sd&& = d&& / g&&
convert2Fraction$ = decimal$
'a and b will be changed unless make copies
c&& = a&&: d&& = b&&
gcd&& = c&& + d&&
'n = original product or numerator (preserve value of n)
'd = divisor (also preserve value)
c = n 'copy of n to be reduced until <= d, c will be the remainder part of division
a = 0 'a is for answer or accumulate, the integer part of the division result
'find lowest power of 10 such that: d * 10^p > n
p = 0 'power of 10
p = p + 1
p = p - 1
m = 0
m = m + 1
m = m - 1
c = c - d * m * 10 ^ p
a = a + m * 10 ^ p
'Now for the decimal expansion isolating the repeating part if one
b$ = "."
'emergency bug out!
loopct = loopct + 1 'loop count should not exceed 1000 for numbers I am testing
IF loopct
> 1000 THEN PRINT "Error: loop too long, bugging out! ":
GOTO skip
'track repeats b() tracks been here once, b2() tracks been here twice
IF rFlag
= 1 THEN 'been here twice! b2(c) = 1
rFlag = 1
b$ = b$ + " repeat "
b2(c) = 1
b(c) = 1
'c was last remainder, mult by 10 and see if some m * d > can reduce it
tc = 10 * c
flag = 0
IF ((tc
- m
* d
) >= 0) AND ((tc
- (m
+ 1) * d
) < 0) THEN IF flag
= 0 THEN b$
= b$
+ "0": m
= 0 c = tc - d * m
'OK either d divided n eventually or there is a repeated pattern recorded in b$
skip: '< needed for debugging
divide$ = r$