Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up1 % 0 is an intentional runtime exception #909
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
process-bot
Sep 26, 2017
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!
Here is what to expect next, and if anyone wants to comment, keep these things in mind.
process-bot
commented
Sep 26, 2017
|
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it! Here is what to expect next, and if anyone wants to comment, keep these things in mind. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Sep 26, 2017
Contributor
Maybe worth referencing the "integer division by zero" item from https://github.com/elm-lang/core/issues/721 here. (https://github.com/elm-lang/core/issues/590)
|
Maybe worth referencing the "integer division by zero" item from https://github.com/elm-lang/core/issues/721 here. (https://github.com/elm-lang/core/issues/590) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
gogocurtis
Dec 20, 2017
I've noticed it is possible to crash the browser with code that is not called but does a divide by zero.
-- this will divide by zero when it is defined
unsafeFun =
let
start = 0
end = 3
len = List.length []
indexes = List.range start end |> List.map (\idx -> idx % len)
in
indexes
gogocurtis
commented
Dec 20, 2017
|
I've noticed it is possible to crash the browser with code that is not called but does a divide by zero.
|
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
brian-carroll
Dec 23, 2017
@gogocurtis that is actually not a function definition, it's a constant value definition.
If you're used to languages that have functions with no arguments, Elm doesn't have that. All functions have arguments. Anything that doesn't have arguments is a constant, whose value will be evaluated on startup.
If you want to define a value but delay evaluation until you need it, the usual way is to use the empty tuple () as the argument type of a function, and just ignore it in the function body. (Usually naming it "_")
brian-carroll
commented
Dec 23, 2017
|
@gogocurtis that is actually not a function definition, it's a constant value definition. If you're used to languages that have functions with no arguments, Elm doesn't have that. All functions have arguments. Anything that doesn't have arguments is a constant, whose value will be evaluated on startup. If you want to define a value but delay evaluation until you need it, the usual way is to use the empty tuple () as the argument type of a function, and just ignore it in the function body. (Usually naming it "_") |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Tracked in #377. |
rtfeldman commentedSep 26, 2017
•
edited
Edited 5 times
-
rtfeldman
edited Feb 24, 2018 (most recent)
-
rtfeldman
edited Feb 24, 2018
-
rtfeldman
edited Feb 24, 2018
-
rtfeldman
edited Feb 24, 2018
-
rtfeldman
edited Feb 24, 2018
This was a design decision from 2013 based on how Haskell did it, which predated Elm's No Runtime Exceptions design guideline.
Creating an issue for this just to track it in https://github.com/elm-lang/core/issues/377
According to IEEE:
Since
NaNis already an Elm value due to IEEE's influence on hardware design, it seems in line with how Elm's design goals have evolved to remove the following conditionalthrowin favor of letting JavaScript's%returnNaNin the case of division by zero, which is the normal behavior of JS%.https://github.com/elm-lang/core/blob/80bbcc901b748f2af250b92a6a3aa5b43a73edcf/src/Elm/Kernel/Basics.js#L23-L25
A counter-argument to this is that in a world where Elm compiled to Assembly, although it could trap
SIGFPEfrom the processor (supported on Windows as well as on POSIX) for integer division by zero, floating-point division by zero is considered well-defined by IEEE to beInfinity. So there would have to be some runtime overhead to check forInfinity, and any runtime overhead on arithmetic operations has the potential to be multiplied by a massive quantity of operations.Interestingly, in Rust, both
x % 0andx / 0crash the program, indicating that generating an extra automatic comparison with 0 (in order to crash if detected) was considered a worthwhile cost to pay, even for a language with a primary design goal of "have no runtime overhead."