LB Booster
« more fractions »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:45am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: more fractions  (Read 135 times)
tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx more fractions
« Thread started on: Mar 4th, 2017, 10:26pm »

Set of functions to work with fractions (+-*/, printing as improper fractions / as mixed numbers, reducing to lower terms)

Feel free to break it ;)
Code:
'set of functions to work with fractions
'tested along this page
'https://www.varsitytutors.com/hotmath/hotmath_help/topics/fraction-operations
'tsh73 march 2017

'1) fractions to be stored as string, "numenator denominator" (to be used by val(word$()))
'   "-" is to be stored at first part
'2) add sub div mul to be created as functions
'3) to visualise, fracToStr$ (actually improper fraction, like 4/3)
'4) to visualise, fracToMixed$ ( 1 1/3)
'5) to use in computations, fracVal

'============================
f1$="1 3"
print fracToStr$(f1$)

f2$=frac$(5, 3)
print fracToStr$(f2$)
print fracToMixed$(f2$)
print fracVal(f2$)
print
print fracToMixed$("8 4")
print fracToMixed$("2 4")
print fracToMixed$("0 4")
print
print toLowestTerms$("3 7")
print toLowestTerms$("45 75")
print
print fracToMixed$(toLowestTerms$("3 7"))
print fracToMixed$(toLowestTerms$("45 75"))
print fracToMixed$(toLowestTerms$("75 45"))
print
print fracAdd$("1 3", "3 7")
print fracAdd$("1 3", "1 2")
print fracAdd$("1 2", "-1 3" )  'hackety hack
print fracAdd$("1 3", "5 3")
print fracToMixed$(fracAdd$("1 3", "5 3"))
print
print fracNegate$("1 2")
print fracNegate$("2 2")
print fracNegate$("3 2")
print fracNegate$("0 2")
print fracToMixed$(fracNegate$("1 2"))
print fracToMixed$(fracNegate$("2 2"))
print fracToMixed$(fracNegate$("3 2"))
print fracToMixed$(fracNegate$("0 2"))
print
print fracSub$("1 3", "3 7")
print fracSub$("1 3", "1 2")
print fracSub$("1 2", "-1 2" )
print fracSub$("1 2", "1 2" )
print fracSub$("1 3", "5 3")
print fracToMixed$(fracSub$("1 3", "5 3"))
print
print fracMul$("1 4", "5 6")
print fracRecipr$("3 4")
print fracDiv$("3 4", "5 7")
print fracToMixed$(fracDiv$("3 4", "5 7"))


'============================
function fracToStr$(frac$)
    num=val(word$(frac$,1))
    if num = 0 then fracToStr$="0": exit function
    'else
    fracToStr$ = word$(frac$,1);"/"; word$(frac$,2)
end function

function frac$(num, denom)
'from numenator denominator
    frac$ = num;" ";denom
end function

function fracToMixed$(frac$)
    fracToMixed$=fracToStr$(frac$)  'default
    num=val(word$(frac$,1))
    denom=val(word$(frac$,2))
    if num <0 then sgn$ = "-": num = 0-num
    if num >= denom then
        intPart = int(num / denom)
        fracToMixed$=sgn$;str$( intPart)
        num = num mod denom
        if num<>0 then fracToMixed$=fracToMixed$;" ";num;"/";denom
    end if
end function

function fracVal(frac$)
    num=val(word$(frac$,1))
    denom=val(word$(frac$,2))
    fracVal = num/denom
end function

function toLowestTerms$(frac$)
    toLowestTerms$ = frac$ 
    num=val(word$(frac$,1))
    denom=val(word$(frac$,2))
    if num <0 then sgn$ = "-": num = 0-num
    gcd = gcd(num, denom)
    if gcd>1 then
        num=num/gcd
        denom=denom/gcd
        toLowestTerms$ = sgn$;num;" ";denom
    END IF
end function

function gcd(a,b)
    while a<>0 and b<>0
        if a>b then
            a = a mod b
        else
            b = b mod a
        end if
    wend
    gcd=a+b
end function

function lcm(m,n)
' least common multiple ( LCM ), for  least common denominator
    lcm = m*n/gcd(m,n)
end function

function fracAdd$(f1$, f2$)
    num1=val(word$(f1$,1))
    denom1=val(word$(f1$,2))
    num2=val(word$(f2$,1))
    denom2=val(word$(f2$,2))
    denom = denom1*denom2
    num = num1*denom2+num2*denom1
    fracAdd$=toLowestTerms$(frac$(num, denom))
end function

function fracNegate$(frac$)
    num=val(word$(frac$,1))
    denom=val(word$(frac$,2))
    fracNegate$ = frac$(0-num, denom)
end function

function fracSub$(f1$, f2$)
    fracSub$ = fracAdd$(f1$, fracNegate$(f2$))
end function

function fracMul$(f1$, f2$)
    num1=val(word$(f1$,1))
    denom1=val(word$(f1$,2))
    num2=val(word$(f2$,1))
    denom2=val(word$(f2$,2))
    denom = denom1*denom2
    num = num1*num2
    fracMul$=toLowestTerms$(frac$(num, denom))
end function

function fracRecipr$(frac$)
    'fracToStr$ = word$(frac$,2);" "; word$(frac$,1)
    num=val(word$(frac$,1))
    denom=val(word$(frac$,2))
    if num <0 then sgn$ = "-": num = 0-num
    fracRecipr$ = sgn$;frac$(denom, num)
end function

function fracDiv$(f1$, f2$)
    fracDiv$=fracMul$(f1$, fracRecipr$(f2$))
end function
 
« Last Edit: Mar 4th, 2017, 10:33pm by tsh73 » User IP Logged

SarmedNafi
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 93
xx Re: more fractions
« Reply #1 on: Mar 6th, 2017, 2:32pm »

Quote:
Feel free to break it wink




Interested functions indeed.
Thank you, Anatoly.

Regards,
Sarmed
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls