elchemy icon indicating copy to clipboard operation
elchemy copied to clipboard

Math operations might throw errors

Open wende opened this issue 7 years ago • 11 comments

Right now divide by 0 on Ints returns 0. Divide by 0 on floats returns NaN.

ANY MATH OPERATIONS ON NAN THROW ERROR.

It'd be nice to find a safe way to express 0 division consistently

wende avatar Feb 01 '18 11:02 wende

Right now divide by 0 on Ints returns 0.

In Elixir at least, calling 2/0 (floating division) or div(2, 0) (integral division) both throw an ArithmeticError.

OvermindDL1 avatar Feb 01 '18 15:02 OvermindDL1

For the sake of minimal compatibility with Elm right now it returns :nan and :infinity atoms. But obviously no other function accepts them so it's more than a temporary solution https://github.com/wende/elchemy-core/blob/e3cc1d7029cf2ed1df602f7c43f6fe98470ab3ed/elm/Elchemy/XBasics.elm#L88-L95

wende avatar Feb 01 '18 16:02 wende

How about controversial opinion making division return Maybe instead, but only when importing Elchemy?

wende avatar Feb 23 '18 16:02 wende

Technically I'd think division should return a maybe unless the compiler could otherwise infer it is impossible for the denominator to be 0 (needs some measure of refined typing then). ^.^;

OvermindDL1 avatar Feb 23 '18 17:02 OvermindDL1

@OvermindDL1 Should I create a card for implementing dependent types maybe? ;)

wende avatar Feb 23 '18 17:02 wende

@wende Lol, to do that you've have to re-implement Elm's typing system (or a better one...) before adding the refined typing enhancements. ;-)

OvermindDL1 avatar Feb 23 '18 17:02 OvermindDL1

I like the Maybe from dividing rather than having a specific type that denotes "NonZero". It's the same result (Maybe from divide has to be handled) with less machinery to implement.

See: https://ellie-app.com/436z6GwVva1/0

loganmac avatar Mar 02 '18 19:03 loganmac

@loganmac That's a good point but it could make false assumption of type-safety, when it's not REALLY safe, just contractual (someone could make NonZero 0 and see the world burn)

The problem with making the division return Maybe is with overriding Basics, which cannot be easily done, so we end up with: screen shot 2018-03-02 at 20 56 33

wende avatar Mar 02 '18 19:03 wende

So the only solution I can see right now is: Make Basics./ and Basics.// throw error and make new operators to return a Maybe: Elchemy.:/ and Elchemy.:// ?

wende avatar Mar 02 '18 20:03 wende

@wende I hinted at you not being able to do that by not exposing the constructor, and only the function that can return NonZero types.

Yeah, maybe something like /? to denote safe division that results in a Maybe? And also doesn't clash with Basics

loganmac avatar Mar 02 '18 20:03 loganmac

@loganmac Oh I like the /?!

wende avatar Mar 02 '18 20:03 wende