Bug 2289 – Stack overflow on very large BigInt to string.
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-08-17T13:56:00Z
Last change time
2015-06-09T01:20:08Z
Assigned to
bugzilla
Creator
dsimcha
Comments
Comment #0 by dsimcha — 2008-08-17T13:56:03Z
import std.stdio, std.bigint;
void main() {
auto result = factorial(10_000);
writefln(stderr, "Calculated 10,000!"); //Works to this point.
auto resultString = result.toString; //Generates stack overflow.
}
BigInt factorial(uint N) {
BigInt result = 1;
for(uint i = 2; i <= N; i++) {
result *= i;
}
return result;
}
Seems to happen at ~20k to 25k decimal digits.
A fairly minor bug, since it is very unlikely that too many people will run into it. (I only found it because I was testing the speed of std.bigint on very large numbers.) If this can't be fixed easily, maybe just a better error message such as "Error: Too many digits." would be good enough.
Comment #1 by andrei — 2008-08-17T14:58:56Z
Thanks for the report. I could not reproduce the bug, but looked over the implementation of toString and it uses O(N) stack gratuitously. I replaced that implementation, and I'm pretty sure it solves the issue, so I'll preemptively close the bug.
Comment #2 by andrei — 2008-08-17T15:07:44Z
Oh, and I committed that to svn, so you can get and build phobos from dsource if in a hurry.
Comment #3 by clugdbug — 2008-08-18T03:38:38Z
(In reply to comment #0)
> import std.stdio, std.bigint;
>
> void main() {
> auto result = factorial(10_000);
> writefln(stderr, "Calculated 10,000!"); //Works to this point.
> auto resultString = result.toString; //Generates stack overflow.
> }
>
> BigInt factorial(uint N) {
> BigInt result = 1;
> for(uint i = 2; i <= N; i++) {
> result *= i;
> }
> return result;
> }
>
> Seems to happen at ~20k to 25k decimal digits.
>
> A fairly minor bug, since it is very unlikely that too many people will run
> into it. (I only found it because I was testing the speed of std.bigint on very
> large numbers.)
You'll find the speed of std.bigint is pretty awful. But don't worry, it will improve _signficantly_. The version in Tango (work in progress) is 40X faster for small numbers, rising to 100s of times faster as the number of digits increases.