1
0
Fork 0

work on fixed

This commit is contained in:
coaljoe 2020-10-27 11:29:39 +03:00
parent 365d3d88aa
commit 58f4d4b4f3
2 changed files with 42 additions and 1 deletions

View File

@ -6,12 +6,16 @@ import math
const (
n_places = 7
scale = i64(10 * 10 * 10 * 10 * 10 * 10 * 10)
)
pub struct Fixed {
fp i64
}
// i: number
// n: precision/fraction amount
// n moves the decimal point N places left
pub fn new_i(i i64, n u32) Fixed {
mut ii := i
mut nn := n
@ -24,4 +28,16 @@ pub fn new_i(i i64, n u32) Fixed {
ii = ii * i64(math.pow(10, int(n_places-nn)))
return Fixed{fp: ii}
}
}
pub fn (f Fixed) add(b Fixed) Fixed {
return Fixed{fp: f.fp + b.fp}
}
pub fn (f Fixed) sub(b Fixed) Fixed {
return Fixed{fp: f.fp - b.fp}
}
pub fn (f Fixed) float() f64 {
return f64(f.fp) / f64(scale)
}

View File

@ -0,0 +1,25 @@
module fixed
fn test_fixed() {
f := new_i(1, 1)
println("f: $f")
println("f.float: ${f.float()}")
{
println("add:")
a := new_i(11, 1)
b := new_i(22, 1)
r := a.add(b)
println("a.float: ${a.float()}")
println("b.float: ${b.float()}")
println("r: $r")
println("r: ${r.float()}")
}
println("done")
}