Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatic Fractions
#21
The simplest way to do it is find the GCD of the degrees and 180.
In QB it would be something like
Code:
Divisor = GCD( Degrees, 180)
Numerator = Degrees / Divisor
Denominator = 180 / Divisor
Print Numerator; "/"; Divisor; "Pi"

There are ways to convert decimals to fractions, but repeating decimals are handled differently than terminating decimals. You would have to come up with an algorithm to recognise repeating decimals, then handle them appropriately. Examples:

Code:
' Use the same number of 9's as digits in the pattern

0.3333... = 3/9                      ' Repeats one digit.  Use one 9.
          = 1/3

0.142857142857... = 142857/999999    ' Repeats six digits. Use six 9's.
                  = 1/7

' If there is a non-repeating part it has to be handled separately as a terminating decimal,
' and zeros (the same number as the non-repeating part) added after the nines

2.35142857142857... = 2.35 + 0.00142857142857...
                    = 235/100 + 142857/99999900
                    = 235/100 + 1/700
                    = 1645/700 + 1/700
                    = 1646/700
                    =  823/350
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Reply
#22
So THAT'S why the calculator couldn't find the GCD of .333333333... ! Would the answer come out the same if the decimal was rounded to #.# instead of .#^n ?
ovaProgramming.

One night I had a dream where I was breaking balls. The next morning, BALLSBREAKER was born.

Quote: Excellent. Now you can have things without paying for them.

BALLSBREAKER 2
~-_-Status Report-_-~
Engine: 94%
Graphics: 95%
Sound: 100%
A Severe Error has crippled BB2 for the time being... I have to figure it out, but until then you won't see much of it Sad.
-----------------------------
Reply
#23
The GCD function needs two numbers. They don't need to be whole numbers. Fraction GCD is the same as any other GCD, except .333333 is not equal to 1/3. 1/3 is equal to 1/3.

Here's a program that I made for a similar question at qbasic.com:

Code:
DECLARE FUNCTION gcd% (n%, m%)
CLS

numerator1% = 2: denominator1% = 5
numerator2% = -3: denominator2% = -5


denominator3% = gcd((denominator1%), (denominator2%))

numerator3% = numerator1% * denominator3% \ denominator1%
numerator3% = numerator3% + numerator2% * denominator3% \ denominator2%

factor2% = gcd((numerator3%), (denominator3%))

IF factor2% = 0 THEN
PRINT "0"
ELSE
numerator3% = numerator3% \ factor2%
denominator3% = denominator3% \ factor2%
IF denominator3% = 1 THEN
PRINT numerator3%
ELSE PRINT LTRIM$(STR$(numerator3%)) + "/" + LTRIM$(STR$(denominator3%))
END IF

END IF

FUNCTION gcd% (n%, m%)
IF m% = 0 THEN EXIT FUNCTION
IF n% = 0 THEN EXIT FUNCTION
'get the GCD
IF m% < n% THEN SWAP m%, n%
DO
r% = m% MOD n%
IF r% = 0 THEN EXIT DO
m% = n%
n% = r%
LOOP
gcd% = n%
END FUNCTION
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)