Comments (3)
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.
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.
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)
- `Result.Err` recovering
- `Process.kill` repeatedly → js error HOT 3
- Provide a way to unmount, or cleanup, a Gren application once it's started
- `Array` documentation example incorrect: `indexedMap (,) [ 3, 3, 3 ] == [ ( 0, 3 ), ( 1, 3 ), ( 2, 3 ) ]` HOT 1
- decoding implementation produces runtime exception → `Nothing`
- different `Bytes` lengths on `==` can still give `True` HOT 2
- `Bytes.encoder/decoder` add
- long `String` `|> Bytes.Decode.string` → heap footprint large
- invalid utf8 `|> Bytes.Decode.string` → nonsense `String`
- Add `Array.update` or similar HOT 1
- Documentation still references automatic record constructor
- Explore === undefined as alternative to Array bounds check
- Use new JS copyable methods for modifying arrays
- Improve Bytes API
- Add functions for folding over codepoints in Strings
- Use .count for when returning length of a collection isn't O(1)
- Use append and prepend for combining sequential collections
- Add indexed operations to Array
- Add `Task.execute`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.