Comment #0 by bearophile_hugs — 2014-06-18T09:49:49Z
std.bigint.Bigint has toInt/toLong methods:
const pure long toLong();
Returns the value of this BigInt as a long, or +- long.max if outside the representable range.
const pure int toInt();
Returns the value of this BigInt as an int, or +- int.max if outside the representable range.
I think it's also useful a "toSize_t" method, to use bigInts for array indexes.
Another unrelated idea is to change the behavour for the cases when the value is outside the representable range, returning a Nullable!int, Nullable!long or Nullable!size_t that is null in case of overflow.
(In general I think Phobos should start using std.typecons.Nullable more often.)
Comment #1 by schveiguy — 2017-03-31T23:13:11Z
trivial to implement, but not trivial to decide whether this should happen.
cast(size_t)someBigInt works, so I'm not sure it's needed in any case.
Comment #2 by robert.schadek — 2024-12-01T16:21:28Z