nim-lang / bigints

BigInts for Nim
MIT License
124 stars 32 forks source link

Rational numbers #124

Open bobbbob98 opened 1 year ago

bobbbob98 commented 1 year ago

BigInt objects don't work with the rationals module, because initRational expects SomeInteger which BigInt isn't. Looking through the code for rationals, it looks like the only function it needs that bigints doesn't have is lcd. What would be the best way to go about using bigints with rational numbers? Could BigInt be marked as SomeInteger with some tweaking?

konsumlamm commented 1 year ago

SomeInteger only encompasses the builtin integers, there is no way to extend that. It could be changed to a concept, but afaik concepts aren't desired in the stdlib rn. Another option would be to change std/rationals to accept any type that implements a specific interface, without actually checking that the types satisfy that interface (i.e. removing the SomeInteger constraint.

However, I think the most realistic option for now is to duplicate std/rationals to a submodule of bigints, that way we might also optimize some things for BigInts.

It might make sense to also open an issue on the Nim repo or an RFC on this.

bobbbob98 commented 1 year ago

Could initRational be changed to func initRational*[T: SomeInteger|BigInt](num, den: T): Rational[T] = ? I'll test some things out later.

konsumlamm commented 1 year ago

Could initRational be changed to func initRational*[T: SomeInteger|BigInt](num, den: T): Rational[T] = ? I'll test some things out later.

It could if the stdlib would depend on bigints, but it doesn't.

bobbbob98 commented 1 year ago

Maybe it should be in stdlib 😉. But yeah maybe a submodule would be good, I'll try working on that.