Ren*_*ene 4 integer biginteger multiplication int128 julia
在 Julia 1.7.2 中,3037000691和3037000693的乘法返回负积-9223370870501072753。我期望的产品是9223373203208478863。
function m(a::BigInt, b::BigInt)::BigInt
a * b
end
Run Code Online (Sandbox Code Playgroud)
该函数给出相同的负结果。
关于如何在 Julia 1.7.2 中获得正确答案有什么想法吗3037000691 * 3037000693?
PS
我确实遇到了这个问题,对(大)孪生素数进行了一些数学计算。
尝试像这样调用你的函数:
\njulia> m(BigInt(3037000691), BigInt(3037000693))\n9223373203208478863\nRun Code Online (Sandbox Code Playgroud)\n或者将您的功能更改为以下任意一个:
\n# TL;DR: Use this it is the fastest.\nfunction m1(a::Int64, b::Int64)::Int128\n Int128(a) * Int128(b)\nend\n\nfunction m2(a, b)::BigInt\n BigInt(a) * BigInt(b)\nend\n\nfunction m3(a::Int64, b::Int64)::BigInt\n BigInt(a) * BigInt(b)\nend\nRun Code Online (Sandbox Code Playgroud)\n请注意,从技术上讲,您只需要强制转换a或b,但我认为这会降低可读性。
用法示例:
\njulia> m1(3037000691, 3037000693)\n9223373203208478863\njulia> m2(3037000691, 3037000693)\n9223373203208478863\njulia> m3(3037000691, 3037000693)\n9223373203208478863\nRun Code Online (Sandbox Code Playgroud)\n在这种情况下,您应该使用m1,它Int128仅使用 s,因为不可能将两个Int64值相乘来溢出 an Int128:
julia> m1(9223372036854775807, 9223372036854775807) # Max val for Int64 squared.\n85070591730234615847396907784232501249\nRun Code Online (Sandbox Code Playgroud)\n一般来说,BigInt尽可能不使用 's 并明确函数中的类型将带来显着的性能提升:
julia> using BenchmarkTools\n\njulia> @benchmark m1(3037000691, 3037000693)\nBenchmarkTools.Trial: 10000 samples with 1000 evaluations.\n Range (min \xe2\x80\xa6 max): 0.049 ns \xe2\x80\xa6 7.780 ns \xe2\x94\x8a GC (min \xe2\x80\xa6 max): 0.00% \xe2\x80\xa6 0.00%\n Time (median): 0.060 ns \xe2\x94\x8a GC (median): 0.00%\n Time (mean \xc2\xb1 \xcf\x83): 0.064 ns \xc2\xb1 0.078 ns \xe2\x94\x8a GC (mean \xc2\xb1 \xcf\x83): 0.00% \xc2\xb1 0.00%\n\n \xe2\x96\x81 \xe2\x96\x88 \xe2\x96\x83\xe2\x96\x86\xe2\x96\x83 \xe2\x96\x82 \xe2\x96\x81\n \xe2\x96\x86\xe2\x96\x88\xe2\x96\x86\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x88\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x88\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x84\xe2\x96\x86\xe2\x96\x84\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x81\xe2\x96\x88 \xe2\x96\x88\n 0.049 ns Histogram: log(frequency) by time 0.1 ns <\n\n Memory estimate: 0 bytes, allocs estimate: 0.\n\njulia> @benchmark m2(3037000691, 3037000693)\nBenchmarkTools.Trial: 10000 samples with 199 evaluations.\n Range (min \xe2\x80\xa6 max): 413.317 ns \xe2\x80\xa6 2.404 ms \xe2\x94\x8a GC (min \xe2\x80\xa6 max): 0.00% \xe2\x80\xa6 64.79%\n Time (median): 507.080 ns \xe2\x94\x8a GC (median): 0.00%\n Time (mean \xc2\xb1 \xcf\x83): 1.957 \xce\xbcs \xc2\xb1 42.299 \xce\xbcs \xe2\x94\x8a GC (mean \xc2\xb1 \xcf\x83): 26.95% \xc2\xb1 1.29%\n\n \xe2\x96\x87\xe2\x96\x86\xe2\x96\x88\xe2\x96\x87\xe2\x96\x86\xe2\x96\x85\xe2\x96\x85\xe2\x96\x84\xe2\x96\x84\xe2\x96\x83\xe2\x96\x82\xe2\x96\x82\xe2\x96\x81\xe2\x96\x82\xe2\x96\x81 \xe2\x96\x82\n \xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x87\xe2\x96\x88\xe2\x96\x87\xe2\x96\x86\xe2\x96\x86\xe2\x96\x86\xe2\x96\x85\xe2\x96\x86\xe2\x96\x84\xe2\x96\x84\xe2\x96\x86\xe2\x96\x84\xe2\x96\x85\xe2\x96\x85\xe2\x96\x81\xe2\x96\x84\xe2\x96\x84\xe2\x96\x85\xe2\x96\x84\xe2\x96\x84\xe2\x96\x81\xe2\x96\x83\xe2\x96\x84\xe2\x96\x84\xe2\x96\x84\xe2\x96\x84\xe2\x96\x83\xe2\x96\x83\xe2\x96\x81\xe2\x96\x84\xe2\x96\x81\xe2\x96\x84\xe2\x96\x83\xe2\x96\x83\xe2\x96\x84\xe2\x96\x84\xe2\x96\x81\xe2\x96\x83\xe2\x96\x84\xe2\x96\x81\xe2\x96\x84\xe2\x96\x84\xe2\x96\x84 \xe2\x96\x88\n 413 ns Histogram: log(frequency) by time 2.39 \xce\xbcs <\n\n Memory estimate: 128 bytes, allocs estimate: 7.\n\njulia> @benchmark m3(3037000691, 3037000693)\nBenchmarkTools.Trial: 10000 samples with 199 evaluations.\n Range (min \xe2\x80\xa6 max): 414.774 ns \xe2\x80\xa6 2.487 ms \xe2\x94\x8a GC (min \xe2\x80\xa6 max): 0.00% \xe2\x80\xa6 64.53%\n Time (median): 496.080 ns \xe2\x94\x8a GC (median): 0.00%\n Time (mean \xc2\xb1 \xcf\x83): 1.895 \xce\xbcs \xc2\xb1 41.026 \xce\xbcs \xe2\x94\x8a GC (mean \xc2\xb1 \xcf\x83): 24.60% \xc2\xb1 1.20%\n\n \xe2\x96\x84 \xe2\x96\x82\xe2\x96\x81 \xe2\x96\x82\xe2\x96\x88\xe2\x96\x84 \n \xe2\x96\x88\xe2\x96\x84\xe2\x96\x88\xe2\x96\x88\xe2\x96\x87\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x87\xe2\x96\x87\xe2\x96\x87\xe2\x96\x84\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x83\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x82\xe2\x96\x81\xe2\x96\x82\xe2\x96\x82\xe2\x96\x81\xe2\x96\x82\xe2\x96\x82 \xe2\x96\x83\n 415 ns Histogram: frequency by time 1.14 \xce\xbcs <\n\n Memory estimate: 128 bytes, allocs estimate: 7.\nRun Code Online (Sandbox Code Playgroud)\n