crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.2k stars 1.61k forks source link

Adding a Difference method to the Math module #14740

Open foxwyn opened 1 week ago

foxwyn commented 1 week ago

Discussion

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.

HertzDevil commented 1 week ago

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

foxwyn commented 1 week ago

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 commented 3 days ago

@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.