Hi all,
I'm pretty new to the language. I tried the following snippet:
import sequtils
import sugar
import bigints
const
n = 15
proc product[T](data: seq[T]) {.thread.} =
echo "data=" , data
echo foldl(data, a * b)
proc sum[T](data: seq[T]) {.thread.} =
echo "data=" , data
echo foldl(data, a + b)
# build a sequence of first n big integers
var firstN1 = toSeq(1..n)
var firstN2 = toSeq(1..n).map(i => initBigInt(i))
sum(firstN1)
sum(firstN2)
product(firstN1)
product(firstN2)
The last function is giving me a runtime error:
data=@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
120
data=@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
120
data=@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
1307674368000
data=@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Error: unhandled exception: index out of bounds, the container is empty [IndexError]
Error: execution of an external program failed: ''post' '
Any hint?
Changing the foldl to:
foldl(data, initBigInt(a) * initBigInt(b), initBigInt(1))
helps.
It could be a bug in bigints. My stacktrace was:
test.nim(24) test
nim/pure/collections/sequtils.nim(727) product
nimble/pkgs/bigints-0.4.3/bigints.nim(395) multiplication
Yes, I think so as well. The comment on the following proc:
# This doesn't work when a = b
proc multiplication(a: var BigInt, b, c: BigInt)
is a pointer, I guess. It is called by:
`*` *(a, b: BigInt): BigInt =
result = zero
multiplication(result, a, b)
which is used inside the foldl template.