oscbyspro / Ultimathnum

Binary arithmetic reimagined in Swift
Apache License 2.0
4 stars 1 forks source link

Negative and infinite small-storage multiplication edge case #3

Closed oscbyspro closed 1 month ago

oscbyspro commented 1 month ago

I wrote some more multiplication tests and found this bug. I'll fix it tomorrow. It's 22:40.

Test().same((~InfiniInt<I8>(U8.max)).times(256), Fallible(~65535, error: false)) // value: 0 ⚠️
Test().same((~InfiniInt<U8>(U8.max)).times(256), Fallible(~65535, error: true )) // value: 0 ⚠️
Test().same((~InfiniInt<IX>(U8.max)).times(256), Fallible(~65535, error: false))
Test().same((~InfiniInt<UX>(U8.max)).times(256), Fallible(~65535, error: true ))

It's in the small storage fast path, where the body in (body: 0, appendix: 1) zeros the product.

oscbyspro commented 1 month ago

This issue is solved by 25c4a7a74173d671585b6dbe7ca4eebb6add8d46 and a78dded1f3acf1d5382c4f013cd719aa3a077906. The latter adds tests that check all combinations for inputs in I12.min through I12.max. It uses InfiniInt<I8> and InfiniInt<U8> so some inputs are 2 elements wide.

oscbyspro commented 1 month ago

One really nice thing about this abstraction is that I just changed the test signature then ran the exact same tests for I64 and U64 too. It's a shame that nobody cares about abstractions, because Swift has a really nice generic type system. It's always go brrr or go home. The same can be said about complexity and code size :(