From 4dbf333ab95b9cbf7c487046f6167b7a168ba609 Mon Sep 17 00:00:00 2001 From: coaljoe Date: Tue, 27 Oct 2020 17:36:43 +0300 Subject: [PATCH] work on fixed --- isotest/bits/fixed/fixed.v | 26 ++++++++++++++++++++++++++ isotest/bits/fixed/fixed_test.v | 22 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/isotest/bits/fixed/fixed.v b/isotest/bits/fixed/fixed.v index d9bf55e..1dbb55e 100644 --- a/isotest/bits/fixed/fixed.v +++ b/isotest/bits/fixed/fixed.v @@ -59,6 +59,32 @@ pub fn (f Fixed) sub(b Fixed) Fixed { return Fixed{fp: f.fp - b.fp} } +pub fn (f Fixed) mul(b Fixed) Fixed { + // XXX + f0 := b + + fp_a := f.fp / c_scale + fp_b := f.fp % c_scale + + fp0_a := f0.fp / c_scale + fp0_b := f0.fp % c_scale + + mut result := i64(0) + + if fp0_a != 0 { + result = fp_a*fp0_a*c_scale + fp_b*fp0_a + } + if fp0_b != 0 { + result = result + (fp_a * fp0_b) + ((fp_b)*fp0_b)/c_scale + } + + return Fixed{fp: result} +} + +pub fn (f Fixed) div(b Fixed) Fixed { + return new_f(f.float() / b.float()) +} + pub fn (f Fixed) float() f64 { return f64(f.fp) / f64(c_scale) } diff --git a/isotest/bits/fixed/fixed_test.v b/isotest/bits/fixed/fixed_test.v index ffb918b..e2ef69e 100644 --- a/isotest/bits/fixed/fixed_test.v +++ b/isotest/bits/fixed/fixed_test.v @@ -37,6 +37,28 @@ fn test_fixed() { println("f4.float: ${f4.float()}") println("f5.float: ${f5.float()}") } + + { + println("mul:") + + f1 := new_f(1.1) + f2 := new_f(10) + + r := f1.mul(f2) + + println("r.float: ${r.float()}") + } + + { + println("dev:") + + f1 := new_f(1.1) + f2 := new_f(2.0) + + r := f1.div(f2) + + println("r.float: ${r.float()}") + } println("done") } \ No newline at end of file