Giter VIP home page Giter VIP logo

Comments (3)

GabrielBoehme13 avatar GabrielBoehme13 commented on August 10, 2024 2

I would like to propose the following…

intDivBy : Int -> Int -> Maybe Int

modBy : Int -> Int -> Maybe Int

remainderBy : Int -> Int -> Maybe Int

unsafeIntDivBy : Int -> Int -> Int

unsafeModBy : Int -> Int -> Int

unsafeRemainderBy : Int -> Int -> Int

…with the unsafe variants available for use in performance-sensitive code. This correctly promotes the safe behavior as the default, and highlights the unsafe nature of the high-performance options.

The unsafe variants would either crash upon division by zero (my preference), or return an undocumented integer result for performance reasons. Either approach (program crash, data corruption) seems reasonable, since the programmer is opting into the unsafe behavior.

It seems several other languages specialcase n / 0 == 0. Would that be an option?

Please, no. I was bitten by an unexpected n // 0 == 0 in an Elm project. I would prefer to have Gren crash on any integer divide-by-zero operation (or even have the // operator removed from the language), rather than getting unexpected zeroes silently corrupting my results. 🙂

from core.

boxed avatar boxed commented on August 10, 2024 1

I find it very annoying that I get crashes for divide by zero in python but I also think that just returning zero would cause even more subtle bugs that would be difficult to track down.

I would for example also like to be able to set it so that any production of NaN in python would raise instead. Much nicer than a garbage value that pollutes the execution flow dowstream some arbitrary amount until it is (hopefully!) caught.

Same here with divide by zero and mod. Yea it's annoying but the right thing and the easy thing should be aligned. That's why I would be against "checked" versions: it makes it opt in to do the right thing. You could call that broken by default too.

from core.

robinheghan avatar robinheghan commented on August 10, 2024

I started a discussion on the elm #language-design channel about this. It seems several other languages specialcase n / 0 == 0. Would that be an option? modBy would then follow the same behaviour.

I was also made aware that Roc has Checked versions of such functions. So divChecked or modByChecked, which would return a Maybe. Would that work?

Are there math-functions, other than div and modBy, that we should look at as well?

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.