diff --git a/compiler/damlc/daml-stdlib-src/DA/Math.daml b/compiler/damlc/daml-stdlib-src/DA/Math.daml index 836776c0affb..94b35746dd0b 100644 --- a/compiler/damlc/daml-stdlib-src/DA/Math.daml +++ b/compiler/damlc/daml-stdlib-src/DA/Math.daml @@ -25,7 +25,10 @@ infixr 8 ** -- | Take a power of a number Example: `2.0 ** 3.0 == 8.0`. (**) : Decimal -> Decimal -> Decimal -x ** y = exp (y * log x) +x ** y + | y == 0.0 = 1.0 + | x == 0.0 = 0.0 + | otherwise = exp (y * log x) -- | Calculate the square root of a Decimal. -- @@ -34,7 +37,9 @@ x ** y = exp (y * log x) -- 1.2 -- ``` sqrt : Decimal -> Decimal -sqrt x = x ** 0.5 +sqrt x + | x < 0.0 = error "sqrt is defined only for non-negative numbers" + | otherwise = x ** 0.5 -- | The exponential function. Example: `exp 0.0 == 1.0` exp : Decimal -> Decimal @@ -47,7 +52,9 @@ exp x -- | The natural logarithm. Example: `log 10.0 == 2.30258509299` log : Decimal -> Decimal -log x = logt10k x / 10000.0 +log x + | x <= 0.0 = error "logarithm is defined only for positive numbers" + | otherwise = logt10k x / 10000.0 -- | The logarithm of a number to a given base. Example: `log 10.0 100.0 == 2.0` logBase : Decimal -> Decimal -> Decimal diff --git a/compiler/damlc/tests/daml-test-files/Math.daml b/compiler/damlc/tests/daml-test-files/Math.daml index a6dff9180566..49e34046104e 100644 --- a/compiler/damlc/tests/daml-test-files/Math.daml +++ b/compiler/damlc/tests/daml-test-files/Math.daml @@ -2,7 +2,7 @@ -- All rights reserved. -- @INFO range=47:22-47:32; Use sqrt --- @INFO range=57:15-57:25; Use sqrt +-- @INFO range=66:15-66:25; Use sqrt module Math where @@ -45,6 +45,15 @@ powerTest = scenario do let sqrt2 = 1.4142135623 : Decimal assert(abs(sqrt2 - 2.0 ** 0.5) <= 0.0000000001) + 0.0 ** 0.0 === 1.0 + 0.0 ** 1.0 === 0.0 + 42.0 ** 0.0 === 1.0 + +sqrtTest = scenario do + let + sqrt2 = 1.4142135623 : Decimal + assert(abs(sqrt2 - sqrt 2.0) <= 0.0000000001) + sqrt 0.0 === 0.0 trigTest = scenario do let