Julia 中 2 个正数的乘积为负数

Multiplication of 2 positive numbers in Julia gives a negative product

在 Julia 1.7.2 中,30370006913037000693 returns 的负积 -9223370870501072753 相乘。我期望的产品是 9223373203208478863.

function m(a::BigInt, b::BigInt)::BigInt
    a * b
end

此函数给出相同的否定结果。

关于如何在 Julia 1.7.2 中获得正确答案的任何想法 3037000691 * 3037000693

P.S.
我 运行 对这个问题做了一些关于(大)孪生素数的数学运算。

尝试这样调用您的函数:

julia> m(BigInt(3037000691), BigInt(3037000693))
9223373203208478863

或将您的函数更改为以下任何一项:

# TL;DR: Use this it is the fastest.
function m1(a::Int64, b::Int64)::Int128
    Int128(a) * Int128(b)
end

function m2(a, b)::BigInt
    BigInt(a) * BigInt(b)
end

function m3(a::Int64, b::Int64)::BigInt
    BigInt(a) * BigInt(b)
end

请注意,从技术上讲,您只需要转换 ab,但我认为这会降低可读性。

用法示例:

julia> m1(3037000691, 3037000693)
9223373203208478863
julia> m2(3037000691, 3037000693)
9223373203208478863
julia> m3(3037000691, 3037000693)
9223373203208478863

在这种情况下,您应该使用 m1,它仅使用 Int128,因为不可能将两个 Int64 值相乘以溢出 Int128

julia> m1(9223372036854775807, 9223372036854775807) # Max val for Int64 squared.
85070591730234615847396907784232501249

一般来说,尽可能不使用 BigInt 并明确说明函数中的类型将显着提高性能:

julia> using BenchmarkTools

julia> @benchmark m1(3037000691, 3037000693)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  0.049 ns … 7.780 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     0.060 ns             ┊ GC (median):    0.00%
 Time  (mean ± σ):   0.064 ns ± 0.078 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

   ▁          █         ▃▆▃         ▂                       ▁
  ▆█▆▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁███▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▄▆▄▁▁▁▁▁▁▁▁▁█ █
  0.049 ns    Histogram: log(frequency) by time      0.1 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark m2(3037000691, 3037000693)
BenchmarkTools.Trial: 10000 samples with 199 evaluations.
 Range (min … max):  413.317 ns …  2.404 ms  ┊ GC (min … max):  0.00% … 64.79%
 Time  (median):     507.080 ns              ┊ GC (median):     0.00%
 Time  (mean ± σ):     1.957 μs ± 42.299 μs  ┊ GC (mean ± σ):  26.95% ±  1.29%

  ▇▆█▇▆▅▅▄▄▃▂▂▁▂▁                                              ▂
  █████████████████▇█▇▆▆▆▅▆▄▄▆▄▅▅▁▄▄▅▄▄▁▃▄▄▄▄▃▃▁▄▁▄▃▃▄▄▁▃▄▁▄▄▄ █
  413 ns        Histogram: log(frequency) by time      2.39 μs <

 Memory estimate: 128 bytes, allocs estimate: 7.

julia> @benchmark m3(3037000691, 3037000693)
BenchmarkTools.Trial: 10000 samples with 199 evaluations.
 Range (min … max):  414.774 ns …  2.487 ms  ┊ GC (min … max):  0.00% … 64.53%
 Time  (median):     496.080 ns              ┊ GC (median):     0.00%
 Time  (mean ± σ):     1.895 μs ± 41.026 μs  ┊ GC (mean ± σ):  24.60% ±  1.20%

  ▄ ▂▁ ▂█▄                                                      
  █▄██▇████▇▇▇▄▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▂▂ ▃
  415 ns          Histogram: frequency by time         1.14 μs <

 Memory estimate: 128 bytes, allocs estimate: 7.

应该注意的是,您在问题中定义的 m 函数不提供否定结果(考虑到函数的语法,这将是非常错误的)。

julia> m(3037000691, 3037000693)
ERROR: MethodError: no method matching m(::Int64, ::Int64)
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

Julia 不会自动从 Int64 转换为 BigInt。总之,这意味着您已经定义了 m(::Int64, ::Int64)(或只是 m(::Any, ::Any)),并且为您的参数值调用了它。

[其他已接受的答案中描述了该做什么]