diff options
author | Koki Fushimi <paalon1936@gmail.com> | 2018-06-04 15:09:17 +0900 |
---|---|---|
committer | Koki Fushimi <paalon1936@gmail.com> | 2018-06-04 15:09:17 +0900 |
commit | f849db1c5c38a36c32628e3e95f3e9e8cee55d20 (patch) | |
tree | 42617b8e37794c3736864a944b80ef809e6142ea /lib/pure | |
parent | d7913419d7a7626ffa774529ae7e20b360d6257e (diff) | |
download | Nim-f849db1c5c38a36c32628e3e95f3e9e8cee55d20.tar.gz |
Generalize and add test
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/math.nim | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 6969b55e3..cd7bf5fe2 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -141,8 +141,17 @@ when not defined(JS): # C proc ln*(x: float32): float32 {.importc: "logf", header: "<math.h>".} proc ln*(x: float64): float64 {.importc: "log", header: "<math.h>".} ## Computes the natural log of `x` - proc log*(b, x: float32): float32 = ln(x) / ln(b) - ## Computes the logarithm base ``b`` of ``x`` +else: # JS + proc sqrt*(x: float32): float32 {.importc: "Math.sqrt", nodecl.} + proc sqrt*(x: float64): float64 {.importc: "Math.sqrt", nodecl.} + + proc ln*(x: float32): float32 {.importc: "Math.log", nodecl.} + proc ln*(x: float64): float64 {.importc: "Math.log", nodecl.} + +proc log*[T: SomeFloat](b, x: T): T = ln(x) / ln(b) + ## Computes the logarithm base ``b`` of ``x`` + +when not defined(JS): # C proc log10*(x: float32): float32 {.importc: "log10f", header: "<math.h>".} proc log10*(x: float64): float64 {.importc: "log10", header: "<math.h>".} ## Computes the common logarithm (base 10) of `x` @@ -200,11 +209,6 @@ when not defined(JS): # C ## Computes the inverse hyperbolic tangent of `x` else: # JS - proc sqrt*(x: float32): float32 {.importc: "Math.sqrt", nodecl.} - proc sqrt*(x: float64): float64 {.importc: "Math.sqrt", nodecl.} - - proc ln*(x: float32): float32 {.importc: "Math.log", nodecl.} - proc ln*(x: float64): float64 {.importc: "Math.log", nodecl.} proc log10*(x: float32): float32 {.importc: "Math.log10", nodecl.} proc log10*(x: float64): float64 {.importc: "Math.log10", nodecl.} proc log2*(x: float32): float32 {.importc: "Math.log2", nodecl.} @@ -665,3 +669,6 @@ when isMainModule: doAssert floorMod(8.0, -3.0) ==~ -1.0 doAssert floorMod(-8.5, 3.0) ==~ 0.5 + + block: # log + doAssert log(3.0, 4.0) == log(4.0) / log(3.0) |