Skip to content

Conversation

@frozolotl
Copy link
Contributor

While the issue #2677 that prompted me to implement these functions has been solved already, Euclidean division and remainder still seem like sensible additions to me.

I'm not sure if I like the documentation, but to me, it appears surprisingly hard to describe these functions in a precise and succinct manner.

@Enivex
Copy link
Collaborator

Enivex commented Nov 13, 2023

What exactly does this add over quo and rem?

@Dherse
Copy link
Collaborator

Dherse commented Nov 13, 2023

I think the advantage is that this is integer based? Does it make a difference knowing the cost of eval will be higher than that of the float operation(s)?

@frozolotl
Copy link
Contributor Author

What exactly does this add over quo and rem?

These are different operations:

  • -7 / -3 == 2.(3)
  • calc.quo(-7, -3) == 2
  • calc.rem(-7, -3) == -1
  • calc.div-euclid(-7, -3) == 3
  • calc.rem-euclid(-7, -3) == 2

Euclidean division and remainder are used in a few algorithms.
You can see https://en.wikipedia.org/wiki/Euclidean_division for more information on Euclidean division in general.

@Enivex
Copy link
Collaborator

Enivex commented Nov 13, 2023

What exactly does this add over quo and rem?

These are different operations:

* `-7 / -3 == 2.(3)`

* `calc.quo(-7, -3) == 2`

* `calc.rem(-7, -3) == -1`

* `calc.div-euclid(-7, -3) == 3`

* `calc.rem-euclid(-7, -3) == 2`

Euclidean division and remainder are used in a few algorithms. You can see https://en.wikipedia.org/wiki/Euclidean_division for more information on Euclidean division in general.

I'm fully aware what the euclidean algorithm is, but the only difference here is that rem will return a negative representative for negative numbers. Having both seems redundant, when it's straight-forward to translate between the two.

@frozolotl
Copy link
Contributor Author

frozolotl commented Nov 13, 2023

Having both seems redundant, when it's straight-forward to translate between the two.

While that is true, I think the extra bit of convenience for a relatively common mathematical operation is worth it. I consider it similar to functions like min, clamp, or gcd in that sense.

@laurmaedje
Copy link
Member

It's always hard to draw the line what's in and out of scope with things like this (and math text operators). Here's the relevant discussion for Rust as a reference: rust-lang/rfcs#2169

@laurmaedje laurmaedje merged commit 43f90b2 into typst:main Nov 17, 2023
@laurmaedje
Copy link
Member

Whatever, I don't mind too much either way. Thanks!

@frozolotl frozolotl deleted the euclidean-remainder branch November 17, 2023 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants