-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.blue
More file actions
64 lines (56 loc) · 1 KB
/
math.blue
File metadata and controls
64 lines (56 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Fraction library
# Basic support
function gcd x y:
if y=0:
return x
return gcd y,(x%y)
# Will modify x,y -- they will be divided by gcd.
function lcm x y:
set g=gcd x,y
debugger
set x/=g
set y/=g
return x*y*g
# value = fraction.value = .a/.b
class fraction:
init:
set this.a=0
set this.b=1
function set a b:
set this.a=a
set this.b=b
function value:
return this.a/this.b
function simple:
set g=gcd this.a,this.b
set this.a/=g
set this.b/=g
function connect with:
set l=lcm this.b,with.b
set this.a*=l/this.b
set with.a*=l/with.b
set this.b=l
set with.b=l
function reverse:
return make_fraction this.b,this.a
function make_fraction fx fy:
set s=new fraction
s.set fx,fy
return s
function f_add fx fy:
fx.connect ^fy
set nf=fx
set nf.a+=fy.a
return nf
function f_minus fx fy:
fx.connect ^fy
set nf=fx
set nf.a-=fy.a
return nf
function f_times fx fy:
set fx.a*=fy.a
set fx.b*=fy.b
fx.simple
return fx
function f_divide fx fy:
return f_times fx,fy.reverse