From 58f4d4b4f3de5aa6d31754f3b2cc6e9eb54f901c Mon Sep 17 00:00:00 2001 From: coaljoe Date: Tue, 27 Oct 2020 11:29:39 +0300 Subject: [PATCH] work on fixed --- isotest/bits/fixed/fixed.v | 18 +++++++++++++++++- isotest/bits/fixed/fixed_test.v | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 isotest/bits/fixed/fixed_test.v diff --git a/isotest/bits/fixed/fixed.v b/isotest/bits/fixed/fixed.v index f003b0d..5b4d722 100644 --- a/isotest/bits/fixed/fixed.v +++ b/isotest/bits/fixed/fixed.v @@ -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} -} \ No newline at end of file +} + +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) +} diff --git a/isotest/bits/fixed/fixed_test.v b/isotest/bits/fixed/fixed_test.v new file mode 100644 index 0000000..8a4e2c3 --- /dev/null +++ b/isotest/bits/fixed/fixed_test.v @@ -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") +} \ No newline at end of file