From ac7f187d57f0e6af9c0fb2540990e0e1b5d341e9 Mon Sep 17 00:00:00 2001 From: Joris Gutjahr Date: Mon, 16 Dec 2019 19:17:01 +0100 Subject: [PATCH 1/6] Added documentation to all the affected functions of Issue https://github.com/elm/core/issues/1072#issuecomment-565860545 to warn affected developers. --- src/Basics.elm | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Basics.elm b/src/Basics.elm index fa05e402..cdb8b5e7 100644 --- a/src/Basics.elm +++ b/src/Basics.elm @@ -199,6 +199,14 @@ mul = -1 / 4 == -0.25 -5 / 4 == -1.25 +**Be careful:** + + 4 / 0 == Infinity : Float + 3 / 0 == Infinity : Float + +This is a bug and Infinity is not a legitimate value +in Elm, thus you can't match for it. +You'll have to check beforehand wheter the divisor is 0 -} fdiv : Float -> Float -> Float fdiv = @@ -221,6 +229,13 @@ similar to `truncate (3 / 4)`. It may sometimes be useful to pair this with the [`remainderBy`](#remainderBy) function. + +Be careful: + + 4 // 0 == 0 + +This is a bug and you'll have to check before computing the result, +if your divisor is 0. -} idiv : Int -> Int -> Int idiv = @@ -535,6 +550,15 @@ or read Daan Leijen’s [Division and Modulus for Computer Scientists][dm] for m information. [dm]: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf + +Be careful: + + modBy 0 5 + +results in a crash, a **runtime exception** of the sort: +"Error: Cannot perform mod 0. Division by zero error." +You'll have to check, that the first argument isn't 0. +Needless to say, this is a bug! -} modBy : Int -> Int -> Int modBy = @@ -551,6 +575,13 @@ or read Daan Leijen’s [Division and Modulus for Computer Scientists][dm] for m information. [dm]: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf + +Be careful: + + remainderBy 0 5 == NaN : Int + +This is a bug in Elm and `NaN` isn't a legitimate value. +You'll have to check beforehand, that the first argument isn't 0. -} remainderBy : Int -> Int -> Int remainderBy = From b53eaf335298e8aebe1c94e9d23fcf61e1f03660 Mon Sep 17 00:00:00 2001 From: Joris Gutjahr Date: Mon, 16 Dec 2019 19:20:07 +0100 Subject: [PATCH 2/6] Consistent attention seeking. --- src/Basics.elm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Basics.elm b/src/Basics.elm index cdb8b5e7..5f093322 100644 --- a/src/Basics.elm +++ b/src/Basics.elm @@ -230,7 +230,7 @@ similar to `truncate (3 / 4)`. It may sometimes be useful to pair this with the [`remainderBy`](#remainderBy) function. -Be careful: +**Be careful:** 4 // 0 == 0 @@ -551,7 +551,7 @@ information. [dm]: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf -Be careful: +**Be careful:** modBy 0 5 @@ -576,7 +576,7 @@ information. [dm]: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf -Be careful: +**Be careful:** remainderBy 0 5 == NaN : Int From 55fc7baf09f2868b160785431b6bb9a8e0f80da8 Mon Sep 17 00:00:00 2001 From: Joris Gutjahr Date: Fri, 17 Jan 2020 16:14:18 +0100 Subject: [PATCH 3/6] Changed fdiv function, to use Maybe - returning Nothing, if divisor = 0. --- src/Basics.elm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Basics.elm b/src/Basics.elm index 5f093322..253de3d9 100644 --- a/src/Basics.elm +++ b/src/Basics.elm @@ -208,9 +208,10 @@ This is a bug and Infinity is not a legitimate value in Elm, thus you can't match for it. You'll have to check beforehand wheter the divisor is 0 -} -fdiv : Float -> Float -> Float -fdiv = - Elm.Kernel.Basics.fdiv +fdiv : Float -> Float -> Maybe Float +fdiv x divisor = case divisor of + 0 -> Nothing + y -> Elm.Kernel.Basics.fdiv x y {-| Integer division: From 07a9a29d2f842a766d45a2a9580cf6ac138a4e7f Mon Sep 17 00:00:00 2001 From: Joris Gutjahr Date: Fri, 17 Jan 2020 16:15:26 +0100 Subject: [PATCH 4/6] idiv function uses Maybe and returns Nothing if divisor = 0. --- src/Basics.elm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Basics.elm b/src/Basics.elm index 253de3d9..9c5ccdf1 100644 --- a/src/Basics.elm +++ b/src/Basics.elm @@ -238,9 +238,10 @@ function. This is a bug and you'll have to check before computing the result, if your divisor is 0. -} -idiv : Int -> Int -> Int -idiv = - Elm.Kernel.Basics.idiv +idiv : Int -> Int -> Maybe Int +idiv x divisor = case divisor of + 0 -> Nothing + y -> Elm.Kernel.Basics.idiv x y {-| Exponentiation From e6c66097cbe450a30a4195e6e7f87b9ea7ced7d6 Mon Sep 17 00:00:00 2001 From: Joris Gutjahr Date: Fri, 17 Jan 2020 16:17:10 +0100 Subject: [PATCH 5/6] modby function uses Maybe and returns Nothing if divisor = 0. **This prevents a crash** --- src/Basics.elm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Basics.elm b/src/Basics.elm index 9c5ccdf1..28e2b72b 100644 --- a/src/Basics.elm +++ b/src/Basics.elm @@ -562,9 +562,10 @@ results in a crash, a **runtime exception** of the sort: You'll have to check, that the first argument isn't 0. Needless to say, this is a bug! -} -modBy : Int -> Int -> Int -modBy = - Elm.Kernel.Basics.modBy +modBy : Int -> Int -> Maybe Int +modBy x divisor = case divisor of + 0 -> Nothing + y -> Elm.Kernel.Basics.modBy x y {-| Get the remainder after division. Here are bunch of examples of dividing by four: From 3b1967a77ae4e54835a5a3765721174c138b755c Mon Sep 17 00:00:00 2001 From: Joris Gutjahr Date: Fri, 17 Jan 2020 16:19:26 +0100 Subject: [PATCH 6/6] remainderBy uses Maybe and returns Nothing if divisor = 0. SOLVED small error. --- src/Basics.elm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Basics.elm b/src/Basics.elm index 28e2b72b..fb0684dd 100644 --- a/src/Basics.elm +++ b/src/Basics.elm @@ -211,7 +211,7 @@ You'll have to check beforehand wheter the divisor is 0 fdiv : Float -> Float -> Maybe Float fdiv x divisor = case divisor of 0 -> Nothing - y -> Elm.Kernel.Basics.fdiv x y + y -> Just <| Elm.Kernel.Basics.fdiv x y {-| Integer division: @@ -241,7 +241,7 @@ if your divisor is 0. idiv : Int -> Int -> Maybe Int idiv x divisor = case divisor of 0 -> Nothing - y -> Elm.Kernel.Basics.idiv x y + y -> Just <| Elm.Kernel.Basics.idiv x y {-| Exponentiation @@ -565,7 +565,7 @@ Needless to say, this is a bug! modBy : Int -> Int -> Maybe Int modBy x divisor = case divisor of 0 -> Nothing - y -> Elm.Kernel.Basics.modBy x y + y -> Just <| Elm.Kernel.Basics.modBy x y {-| Get the remainder after division. Here are bunch of examples of dividing by four: @@ -586,9 +586,10 @@ information. This is a bug in Elm and `NaN` isn't a legitimate value. You'll have to check beforehand, that the first argument isn't 0. -} -remainderBy : Int -> Int -> Int -remainderBy = - Elm.Kernel.Basics.remainderBy +remainderBy : Int -> Int -> Maybe Int +remainderBy x divisor = case divisor of + 0 -> Nothing + y -> Just <| Elm.Kernel.Basics.remainderBy x y {-| Negate a number.