diff options
author | shirleyquirk <31934565+shirleyquirk@users.noreply.github.com> | 2023-12-15 06:49:07 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-15 07:49:07 +0100 |
commit | a4628532b27857d095e69b7b162b453fc2b8373c (patch) | |
tree | a3a66ef302c373285d77069803f40f819cdfeb29 /lib | |
parent | 94f7e9683fb5c9f643b7e4af367a3a6457d5ad7f (diff) | |
download | Nim-a4628532b27857d095e69b7b162b453fc2b8373c.tar.gz |
rationals: support Rational[SomeUnsignedInt] (#23046)
fixes #22227 rationale: - `3u - 4u` is supported why not`3u.toRational - 4u.toRational` - all of rationals' api is on SomeInteger, looks like unsigned is declared as supported - math on unsigned rationals is meaningful and useful.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/rationals.nim | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/lib/pure/rationals.nim b/lib/pure/rationals.nim index ab05bcc25..45902b7cd 100644 --- a/lib/pure/rationals.nim +++ b/lib/pure/rationals.nim @@ -40,16 +40,16 @@ func reduce*[T: SomeInteger](x: var Rational[T]) = reduce(r) doAssert r.num == 1 doAssert r.den == 2 - + if x.den == 0: + raise newException(DivByZeroDefect, "division by zero") let common = gcd(x.num, x.den) if x.den > 0: x.num = x.num div common x.den = x.den div common - elif x.den < 0: - x.num = -x.num div common - x.den = -x.den div common - else: - raise newException(DivByZeroDefect, "division by zero") + when T isnot SomeUnsignedInt: + if x.den < 0: + x.num = -x.num div common + x.den = -x.den div common func initRational*[T: SomeInteger](num, den: T): Rational[T] = ## Creates a new rational number with numerator `num` and denominator `den`. |