-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type conversion for small BigInts into integers #72
Comments
That never does what you want. I suggest you read the documentation for I generally agree that functions to convert a
EDIT: Added a 4th option. |
@Araq: you might be interested in this discussion, i.e. give your opinion on how to best proceed with this. |
We can not return the lowest/highest value for type conversions to integers as a BigInt can be any value that an int can contain. |
IMO the prototype should be: proc toFixedInt*[T](b: BigInt): Option[T] with the hopefully obvious semantics. |
This issue should be reopened, |
I also really need proc pred*(a: BigInt; def = 1): BigInt =
a - initBigInt(def)
proc succ*(a: BigInt; def = 1): BigInt =
a + initBigInt(def)
|
This has been addressed in #100. |
Since BigInt's limb parameter has become private, thus we can not easily cast BigInts with absolute value less than
2^32
to anint
or even auint32
.The only workaround yet is to use the keyword
cast[int]
. But this does not always do what we want.To get only the smallest limb, we also have to define a mask like
2^32 - 1
and to apply an ineffectiveand
.Can we implement some functions like … ?:
toUint32(a: BigInt): uint32
returns the only limb for BigInts between 1-2^32 and +2^32-1 (small BigInts).toInt(a: BigInt): int
cast the BigInt if it is small enough, into an int, according to the sign of BigInt, with correct representation (2's complement) and raises a ValueError if the cast can not be done (if int is stored on 32 bits, it may not fit if the heavy weight bit of the uint32 corresponding to first limb is 1).toUint64(a: BigInt): uint64
returns the uint64 made up of one to two limbs, if the BigInt is small enough.toFloat
like described in the similar issue Feature request: toBiggestInt #34.The text was updated successfully, but these errors were encountered: