Move safe_mul_div macro and make it generic on integer types (#1334)

Move safe_mul_div macro and make it generic on integer types
This commit is contained in:
Lexi
2024-08-09 16:54:26 -04:00
committed by GitHub
parent d997445284
commit 696d39b922
3 changed files with 141 additions and 5 deletions

View File

@@ -164,4 +164,34 @@ fn void! test() @test
assert(math::round_to_decimals(radians, 3) == 0.785);
assert(math::round_to_decimals(radians_f, 3) == 0.785f);
}
fn void! test_mult_div()
{
char a = 20;
assert(a.mult_div(20, 10) == 40);
ichar b = 20;
assert(b.mult_div(20, -10) == -40);
short c = 16000;
assert(c.mult_div(4, 2) == 32000);
ushort d = 16000;
assert(d.mult_div(8, 2) == 64000);
int e = 1_000_000;
assert(e.mult_div(40000, 10000) == 4_000_000);
uint f = 3_000_000_000u;
assert(f.mult_div(110, 100) == 3_300_000_000u);
long g = 1_000_000_000_000i64;
assert(g.mult_div(2_000_000_000_000i64, 1_000_000_000i64) == 2_000_000_000_000_000i64);
ulong h = 1_000_000_000_000u64;
assert(h.mult_div(2_000_000_000_000u64, 1_000_000_000u64) == 2_000_000_000_000_000u64);
char[<4>] i = {20, 30, 40, 50};
assert(i.mult_div(12,10) == char[<4>] {24, 36, 48, 60});
assert(i.mult_div(char[<4>]{11, 12, 13, 14}, char[<4>]{10,10,10,10}) == char[<4>]{22, 36, 52, 70});
long[<4>] j = {1_000_000_000_000i64, 2_000_000_000_000i64, 3_000_000_000_000i64, 4_000_000_000_000i64};
assert(j.mult_div(2_000_000_000_000i64, 1_000_000_000i64) == long[<4>]{2_000_000_000_000_000i64, 4_000_000_000_000_000i64, 6_000_000_000_000_000i64, 8_000_000_000_000_000i64});
ichar[<4>] k = {20, 30, 40, 50};
assert(k.mult_div(20,-10) == ichar[<4>]{-40,-60,-80,-100});
}