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

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #45 on: April 30, 2019, 10:46:48 am »
Well in summary, converting back to fractions is very, very limited.

The new method of calculating adj&& is ruined when STR$(n##) produces a string with scientific or exponential notation.
Code: QB64: [Select]
  1.     ns$ = STR$(n##)
  2.     dot = INSTR(ns$, ".")
  3.     p2 = LEN(ns$) - dot
  4.     adj&& = 10 ^ p2
  5.  

Also the longer the string of zeros to the right of the decimal the less repeat length this convert back to fraction can tolerate.

I actually started reconsidering picking up string math to attempt larger fraction conversions but then I would need GCD function for number strings also... just doesn't seem worth the trouble. Like Pete said, who uses such cumbersome fractions anyway?

Just neat to see that they could (if only in theory) be converted back (but not with rounding).

I can see rounding decimals to closest 1/2, 1/4, 1/8, 1/16, 1/32, 1/64 of a unit as being useful.

« Last Edit: April 30, 2019, 12:13:52 pm by bplus »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Convert Decimal to Fraction
« Reply #46 on: April 30, 2019, 02:24:45 pm »
hello bplus
just for fun, expand the decimal to continued fraction convergents https://en.wikipedia.org/wiki/Continued_fraction, see the comments
you will wind up with successive approximations to Pi in this instance
3/1, 22/7, 333/106, 355/113
the only problem is how to know when you reached the optimum
Code: [Select]
DIM n1 AS DOUBLE, n2 AS DOUBLE, n3 AS DOUBLE
DIM d1 AS DOUBLE, d2 AS DOUBLE, d3 AS DOUBLE
DIM fp AS DOUBLE, ip AS DOUBLE

'expand a decimal number to a fraction using continued fraction convergents
'take Pi for instance
'int(Pi) = 3, frac = Pi-3 = .1415926535897932384626433832795
'    | 3
'0 1 |
'1 0 |
'3 * 1 + 0 = 3
'3 * 0 + 1 = 1
'    | 3 |
'0 1 | 3 |
'1 0 | 1 |
'frac = 1/ frac : int(frac) = 7 : frac = frac - 7
'
'7 * 3 + 1 = 22
'7 * 1 + 0 = 7
'    | 3 | 7
'0 1 | 3 | 22
'1 0 | 1 |  7
'frac = 1/ frac : int(frac) = 15 : frac = frac - 15
'
'15 * 22 + 3 = 333
'15 * 7  + 1 = 106
'    | 3 |  7 |  15
'0 1 | 3 | 22 | 333
'1 0 | 1 |  7 | 106
'frac = 1/ frac : int(frac) = 1 : frac = frac - 1
'
'1 * 333 + 22 = 355
'1 * 106 + 7  = 113
'    | 3 |  7 |  15 | 1
'0 1 | 3 | 22 | 333 | 355
'1 0 | 1 |  7 | 106 | 113

n1 = 0: n2 = 1
d1 = 1: d2 = 0
fp = 3.1415926535897932384626433832795
'fp = .125125
FOR i = 1 TO 6
    ip = INT(fp)
    n3 = ip * n2 + n1
    d3 = ip * d2 + d1
    n1 = n2: n2 = n3
    d1 = d2: d2 = d3
    fp = 1 / (fp - ip)
    PRINT n3; "/"; d3
NEXT
« Last Edit: April 30, 2019, 02:51:38 pm by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Convert Decimal to Fraction
« Reply #47 on: April 30, 2019, 03:06:02 pm »
Hi jack,

Would that sequence continue to get better, if you could extend the precision of the calculations?

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Convert Decimal to Fraction
« Reply #48 on: April 30, 2019, 04:12:37 pm »
yes.