work on fixed
This commit is contained in:
parent
365d3d88aa
commit
58f4d4b4f3
|
@ -6,12 +6,16 @@ import math
|
||||||
|
|
||||||
const (
|
const (
|
||||||
n_places = 7
|
n_places = 7
|
||||||
|
scale = i64(10 * 10 * 10 * 10 * 10 * 10 * 10)
|
||||||
)
|
)
|
||||||
|
|
||||||
pub struct Fixed {
|
pub struct Fixed {
|
||||||
fp i64
|
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 {
|
pub fn new_i(i i64, n u32) Fixed {
|
||||||
mut ii := i
|
mut ii := i
|
||||||
mut nn := n
|
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)))
|
ii = ii * i64(math.pow(10, int(n_places-nn)))
|
||||||
|
|
||||||
return Fixed{fp: ii}
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
Loading…
Reference in New Issue