Comments (6)
This issue should be reopened, toSignedInt
only provides conversion to signed integers. I'd also argue that it should be renamed to toInt
and also support unsigned integers (rather than adding a new toUnsignedInt
function).
from bigints.
The only workaround yet is to use the keyword
cast[int]
. But this does not always do what we want.
That never does what you want. I suggest you read the documentation for cast
, it merely reinterprets the underlying bits.
I generally agree that functions to convert a BigInt
to some integer types are useful, but how should we handle the case of the BigInt
being out of range? I see several possibilities:
- raise an exception
- return an
Option
- return the lowest/highest value (especially for floats, where we have
Inf
/-Inf
) - cut off the higher bits (i.e. return
x mod 2^n
wheren
is the number of bits of the resut)
EDIT: Added a 4th option.
from bigints.
@Araq: you might be interested in this discussion, i.e. give your opinion on how to best proceed with this.
from bigints.
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.
The first solution forces the user to wrap any potentially dangerous cast in a try .. except .. finally
clause.
I have not used Option
in my programs yet, if I understand the user will have to check (everytime ?) whether the Option contains a value ?
from bigints.
IMO the prototype should be:
proc toFixedInt*[T](b: BigInt): Option[T]
with the hopefully obvious semantics.
from bigints.
I also really need pred
and succ
. Implementation can be as simple as:
proc pred*(a: BigInt; def = 1): BigInt =
a - initBigInt(def)
proc succ*(a: BigInt; def = 1): BigInt =
a + initBigInt(def)
from bigints.
Related Issues (20)
- [Feature Request] Random BigInt HOT 3
- Add benchmarks HOT 1
- Version is 1.0.0 but latest tag is 0.5.0 HOT 5
- initBigInt converts minus sign "-" to zero. HOT 2
- Some functions are not being imported HOT 2
- bump new version HOT 6
- Division causes assert failure HOT 18
- Rational numbers HOT 4
- nimdocs.com going away HOT 9
- [Feature request] Add more examples : factorization, find modular square-root HOT 1
- roadmap for next versions of BigInts (>=0.6.0) HOT 3
- Bitwise `not` HOT 2
- Add probabilistic and deterministic primality tests HOT 4
- Design of internal functions taking out parameters (`addition`, `additionInt`, etc.) HOT 4
- Tests of invmod fail with arc & orc GC HOT 7
- `div`/`mod` behaviour HOT 2
- Compound assignment operators shouldn't use `template`s HOT 8
- Base multiplication returns wrong results for some big numbers HOT 5
- [Bug] `toSignedInt[int64]` produces wrong results in certain cases
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bigints.