Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math operations might throw errors #274

Open
wende opened this issue Feb 1, 2018 · 11 comments
Open

Math operations might throw errors #274

wende opened this issue Feb 1, 2018 · 11 comments

Comments

@wende
Copy link
Owner

wende commented Feb 1, 2018

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

@OvermindDL1
Copy link

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.

@wende
Copy link
Owner Author

wende commented Feb 1, 2018

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
Copy link
Owner Author

wende commented Feb 23, 2018

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

@OvermindDL1
Copy link

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). ^.^;

@wende
Copy link
Owner Author

wende commented Feb 23, 2018

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

@OvermindDL1
Copy link

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

@loganmac
Copy link

loganmac commented Mar 2, 2018

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

@wende
Copy link
Owner Author

wende commented Mar 2, 2018

@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
Copy link
Owner Author

wende commented Mar 2, 2018

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.:// ?

@loganmac
Copy link

loganmac commented Mar 2, 2018

@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

@wende
Copy link
Owner Author

wende commented Mar 2, 2018

@loganmac Oh I like the /?!

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

No branches or pull requests

3 participants