nim-lang / bigints

BigInts for Nim
MIT License
124 stars 32 forks source link

Add signum function #108

Open rotu opened 2 years ago

konsumlamm commented 2 years ago

Should this really allocate a new BigInt? I feel like this should just return an int, so that the user can choose if they want that extra allocation, by manually calling initBigInt. Do you have a concrete use casein mind for this, where it's more useful to have this return a BigInt?

rotu commented 2 years ago

Should this really allocate a new BigInt? I feel like this should just return an int, so that the user can choose if they want that extra allocation, by manually calling initBigInt. Do you have a concrete use casein mind for this, where it's more useful to have this return a BigInt?

The reason it returns a BigInt is for the invariant x == abs(x) * sgn(x). I'd be okay with returning an int if BigInt arithmetic allowed implicit widening (like other numeric types do).

konsumlamm commented 2 years ago

I think we should support arithmetic where one of the arguments is an int sooner or later.

rotu commented 2 years ago

I think we should support arithmetic where one of the arguments is an int sooner or later.

Any reason why initBigInt(SomeSignedInt): BigInt isn't an implicit conversion? That seems like the easy solution

rotu commented 2 years ago

NVM - opened https://github.com/nim-lang/bigints/pull/109 Feel free to tell me there why this is a bad idea :-p