summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-06-14 14:02:57 +0200
committerGitHub <noreply@github.com>2018-06-14 14:02:57 +0200
commit2d186f18ec66b3152ef129690c326a676f587e72 (patch)
tree1d433180fbf8383b2f09a09a72c9fad2ba5747f4 /lib/pure
parent80a1da951d6b6badd71eb7d540fb7c3658a46d00 (diff)
parent5332da2e7cdf7a52e607f7b90acd67dc16fe2754 (diff)
downloadNim-2d186f18ec66b3152ef129690c326a676f587e72.tar.gz
Merge pull request #7950 from Paalon/math-log
Add log for base b of x
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/math.nim20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index 8ea8ee203..b921b1841 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -147,6 +147,18 @@ 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`
+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](x, base: T): T =
+  ## Computes the logarithm ``base`` of ``x``
+  ln(x) / ln(base)
+
+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`
@@ -204,11 +216,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.}
@@ -682,3 +689,6 @@ when isMainModule:
 
     doAssert floorMod(8.0, -3.0) ==~ -1.0
     doAssert floorMod(-8.5, 3.0) ==~ 0.5
+
+  block: # log
+    doAssert log(4.0, 3.0) == ln(4.0) / ln(3.0)