IF x <> INT(x) THEN 'check to see if its a decimal
FOR top = 1 TO 500000
FOR btm = 1 TO 500000
IF top / btm = x THEN GOTO prnt
NEXT
NEXT
END IF
prnt: PRINT STR$(top) + "/" + STR$(btm)
This would be incredibly slow though, i would just leave it as a decimal
Hi,
Using STR$, MID$, INSRT, and VAL you can convert the number to a string, extract the decimal part, convert it back to a whole number and figure out the denominator. The denominator will alwasy be a power of ten. For instance .101 is equal to 101/1000.
Hope this gives you a clue.
BTW you have to reduce to get 1/4 from .25 (25/100).
Pappy
You learn new something everyday.
I wrote a program a long time ago that estimates a decimal value using an integer fraction. It works on the following principle: if you invert the fraction, then you are left with an expression of the form 1/x, where x is the *inverse* of the fraction. Furthermore, if you take away the integer part of the fraction, then you are left with another fraction to compensate for, so x is of the form (a + y), where a is the integer part and y is the compensation for the fractional part. You can then repeat with 'y' and end up with an expression of the form:
[tt]
0 + 1
-------------------
5 + 1
-------------
2 + 1
-----
...
[/tt]
(for the fraction 0.18723 -- this approximation yields 0.1875 after 4 levels, the above fraction simplifying to 3/16). This nested fraction is easy to convert back to a simple fraction: you simply start with the deepest nesting, incorporate the integer part into the fraction, then "invert and multiply" to do the division. The result is one less level with the same structure.
Here is the program itself:
[tt] DIM intPart%(20)
fraction# = .1835718238#
levels% = 10 FOR i% = 1 TO levels%
compensation%(i%) = INT(fraction# + .00001#) IF ABS(intPart%(i%) - fraction#) < .00001 THEN
levels% = i% EXIT FOR
END IF
fraction# = 1# / (fraction# - intPart%(i%)) NEXT i%
numerator& = intPart%(levels%)
denominator& = 1&
FOR i% = levels% - 1 TO 1 STEP -1
temp& = numerator&
numerator& = intPart%(i%) * numerator& + denominator&
denominator& = temp& NEXT i%
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.