HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.14k stars 656 forks source link

I have no idea how to write code for multiplying an int128 #11749

Closed SomeGuyWhoLovesCoding closed 1 month ago

SomeGuyWhoLovesCoding commented 1 month ago
/**
    Returns the product of `a` and `b`.
    The code is different from Int64's `mul(a, b)` because it causes problems for values that represent higher than 64 bits.
**/
@:op(A * B)
public static #if !lua inline #end function mul(a:Int128, b:Int128):Int128 {
    return make(b.high * a.high, (a.low * b.low));
}

This here is the current multiplication function. When it goes up to 18 quintillion (which is the maximum unsigned 64 bit number), it's supposed to increment the high value. But, it appears that I can't really do that. The trace if you want to see what's going on:

MULTIPLY TEST
10
Low: 100
100
Low: 1000
1000
Low: 10000
10000
Low: 100000
100000
Low: 1000000
1000000
Low: 10000000
10000000
Low: 100000000
100000000
Low: 1000000000
1000000000
Low: 10000000000
10000000000
Low: 100000000000
100000000000
Low: 1000000000000
1000000000000
Low: 10000000000000
10000000000000
Low: 100000000000000
100000000000000
Low: 1000000000000000
1000000000000000
Low: 10000000000000000
10000000000000000
Low: 100000000000000000
100000000000000000
Low: 1000000000000000000
1000000000000000000
(This is where the number bugs out)
Low: -8446744073709551616
10000000000000000000
Low: 7766279631452241920
7766279631452241920

I'm trying to write an Int128 for haxe btw. Here are the files: Int128.txt Int128Helper.txt Int64.txt Int64Helper.txt

Simn commented 1 month ago

See #11750