问题描述
我正在尝试计算一个非常“大”向量的总和,我知道 big()
函数可用于计算大数,
我什至在下面使用了它(并且它有效) .
但是,如果我尝试总和使用它,则它不起作用。
我尝试了 big(sum(test,dims=1))
和 sum(big(test),dims=1))
,但收到以下错误:InexactError: Int64(-3331427209747016990720)
test = Tuple{Int,Int}[]
N = 80
Iterations = 60
for i in 1:10000
push!(test,(big(largeNumber1(N,Iterations)) * big(largeNumber2(N,Iterations)),0))
end
# this just transforms test into a vector
test = hcat(first.(test),last.(test)) * [1,0]
sum(test,dims=1) # here is where the code "breaks"
<output> 1-element Vector{Int64}:
-5233167026984513820
很可能我使用了 big()
错误
解决方法
您没有显示 largeNumber1() 的代码,但您似乎在求和之前将大整数放入 Int64 元组向量中。试试
test = Tuple{BigInt,BigInt}[]