Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expressing Decimals as Fractions

Status
Not open for further replies.

The

Programmer
Jul 30, 2001
92
CA
Is there a way in QBasic to express a decimal answer (x) as a fraction? So instead of printing .25, it would print 1/4? Thanks
 
Or, if there isn't a way to do that, is there a way to check if x is a decimal? (yes, I am a newbie)
 
The only way I could think ouf would be:

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) + &quot;/&quot; + 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 &quot;invert and multiply&quot; 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%

dashes& = INT(LOG(denominator&) / LOG(10)) + 4
format$ = RIGHT$(SPACE$(17) + STRING$(dashes&, &quot;-&quot;), 17) + &quot; = #.############&quot;

PRINT USING &quot; ###,###,###,###&quot;; numerator&
PRINT USING format$; CDBL(numerator&) / denominator&
PRINT USING &quot; ###,###,###,###&quot;; denominator&
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top