crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Adding a Difference method to the Math module

Open foxwyn opened this issue 1 year ago • 3 comments

Discussion

  • I would like to see the addition of a Difference method added to the Math module.
  • Reason being, I'm surprised to not find it available anywhere in stdlib, despite being a fundamental math operation.
  • I propose adding a Math.diff(a,b) method to the Math module for each type (ints, floats, etc).

Difference is just the smaller number subtracted from the larger number to return the gap between.

Math.max(a,b) - Math.min(a,b)

I would be willing to make a pr.

foxwyn avatar Jun 21 '24 08:06 foxwyn

That would be just (a - b).abs, which I don't think is that difficult to type out? Which other stdlibs have this method?

HertzDevil avatar Jun 21 '24 09:06 HertzDevil

That would be just (a - b).abs, which I don't think is that difficult to type out? Which other stdlibs have this method?

I went digging for a difference method after trying (a-b).abs first. I had assumed I misunderstood the math and needed something else, but it would seem my error was somewhere else in my code, because '(a-b).abs' works in isolation as I would expect it to.

So that's my mistake, I suppose that is sort of silly now.

That being said, I don't know of it being a thing in other languages stdlibs. But it seems like such a fundamental operation for two numbers, I guess I'm just surprised it isn't inbuilt - especially when there are tons of trig functions. If there are absolute basics of plus, minus, multiply, and divide, and beyond basics of trig functions, you'd think difference falling somewhere in-between would be included.

foxwyn avatar Jun 21 '24 09:06 foxwyn

@ysbaddaden Glad you reference #14587, because that was also one of the solutions I tried, and was disappointed to find it didn't work as expected.

foxwyn avatar Jun 28 '24 16:06 foxwyn

Given the solution (a-b).abs, does it make sense to keep this issue open? cc/ @foxwyn

beta-ziliani avatar Sep 04 '24 13:09 beta-ziliani