whit doubles you wil get rounding error's

Code: Select all

`'' bluatigro 2 nov 2018`

'' fractor lib

function gcd( a as long , b as long ) as long

while a <> b

if a < b then

b = b - a

end if

if a > b then

a = a - b

end if

wend

return a

end function

type fractor

dim as long a , b

declare constructor ()

declare constructor ( i as long , j as long )

declare function tostr() as string

end type

constructor fractor()

end constructor

constructor fractor( i as long , j as long )

a = i

b = j

end constructor

function fractor.tostr() as string

return str( a ) + " / " + str( b )

end function

operator + ( f1 as fractor , f2 as fractor ) as fractor

dim as long a = f1.a * f2.b + f2.a * f1.b

dim as long b = f1.b * f2.b

dim as long c = gcd( a , b )

return type( a / c , b / c )

end operator

operator - ( f1 as fractor , f2 as fractor ) as fractor

dim as long a = f1.a * f2.b - f2.a * f1.b

dim as long b = f1.b * f2.b

dim as long c = gcd( a , b )

return type( a / c , b / c )

end operator

operator / ( f1 as fractor , f2 as fractor ) as fractor

dim as long a = f1.a * f2.b

dim as long b = f1.b * f2.a

dim as long c = gcd( a , b )

return type( a / c , b / c )

end operator

operator * ( f1 as fractor , f2 as fractor ) as fractor

dim as long a = f1.a * f2.a

dim as long b = f1.b * f2.b

dim as long c = gcd( a , b )

return type( a / c , b / c )

end operator

dim as fractor f1 , f2 , f3

f1 = fractor( 1 , 4 )

f2 = fractor( 1 , 6 )

f3 = f1 + f2

print "a = " + f1.tostr()

print "b = " + f2.tostr()

print "a + b = " + f3.tostr()

f3 = f1 - f2

print "a - b = " + f3.tostr()

f3 = f1 * f2

print "a * b = " + f3.tostr()

f3 = f1 / f2

print "a / b = " + f3.tostr()

print "[ game over : push return ]"

sleep