r/purescript • u/hdgarrood • Apr 17 '18
Different forms of integer division
Blogged: “Different forms of integer division” http://harry.garrood.me/blog/integer-division/
I wasn’t able to find an accessible introduction to the concept of integer division and the various subtly different forms it can take in different programming languages, so I had a go at writing one.
Why is this relevant to PureScript, you ask? Well, the EuclideanRing Int instance will probably be changing in an upcoming release to use Euclidean rather than truncating division.
6
Upvotes
View all comments
Show parent comment
2
u/hdgarrood Apr 19 '18
So the
EuclideanRingclass itself isn't changing, it's just theIntinstance which is - there's no effect on theEuclideanRing Numberinstance. Also, the rules I discussed in the post are just a set of rules I think make sense for integer division, not for a generalEuclideanRingclass, although they do of course bear some similarity to theEuclideanRinglaws.I think the
EuclideanRing Numberinstance does obey both of the first two rules, though, at least up to rounding errors. It doesn't obey the third, of course, but I don't think anyone would expectNumberdivision to.As you pointed out in Slack, there is an argument to be made that the numeric hierarchy is a little problematic in that any
Fieldmust havemod = const (const zero)in itsEuclideanRinginstance, which can be a bit surprising, and any use ofmodis almost always an error if you're dealing with a single concrete type which has aFieldinstance, such asNumber. I think that's separate from what I'm talking about here, though.