From d7913419d7a7626ffa774529ae7e20b360d6257e Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Mon, 4 Jun 2018 15:03:08 +0900 Subject: [PATCH 1/8] Add log proc for base b of x --- lib/pure/math.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 6be19a3395..6969b55e3c 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -141,6 +141,8 @@ when not defined(JS): # C proc ln*(x: float32): float32 {.importc: "logf", header: "".} proc ln*(x: float64): float64 {.importc: "log", header: "".} ## Computes the natural log of `x` + proc log*(b, x: float32): float32 = ln(x) / ln(b) + ## Computes the logarithm base ``b`` of ``x`` proc log10*(x: float32): float32 {.importc: "log10f", header: "".} proc log10*(x: float64): float64 {.importc: "log10", header: "".} ## Computes the common logarithm (base 10) of `x` @@ -372,7 +374,7 @@ when not defined(JS): # C proc `mod`*(x, y: float32): float32 {.importc: "fmodf", header: "".} proc `mod`*(x, y: float64): float64 {.importc: "fmod", header: "".} - ## Computes the modulo operation for float operators. + ## Computes the modulo operation for float operators. else: # JS proc hypot*[T: float32|float64](x, y: T): T = return sqrt(x*x + y*y) proc pow*(x, y: float32): float32 {.importC: "Math.pow", nodecl.} From f849db1c5c38a36c32628e3e95f3e9e8cee55d20 Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Mon, 4 Jun 2018 15:09:17 +0900 Subject: [PATCH 2/8] Generalize and add test --- lib/pure/math.nim | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 6969b55e3c..cd7bf5fe2e 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: "".} proc ln*(x: float64): float64 {.importc: "log", header: "".} ## 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: "".} proc log10*(x: float64): float64 {.importc: "log10", header: "".} ## 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) From abf8ee049a00676e2b45faaf79b5eff44f20f467 Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Mon, 4 Jun 2018 16:48:11 +0900 Subject: [PATCH 3/8] Broaden the argument types --- lib/pure/math.nim | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index cd7bf5fe2e..9cadfb6b74 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -148,8 +148,13 @@ else: # JS 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) +proc log*[B, X: SomeFloat](b: B, x: X): auto = ## Computes the logarithm base ``b`` of ``x`` + when B is float64 or X is float64 + var r: float64 + else: + var r: float32 + result = ln(x) / ln(b) when not defined(JS): # C proc log10*(x: float32): float32 {.importc: "log10f", header: "".} From 05f81482f44215d6694298f351296ccab4e10dc3 Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Tue, 5 Jun 2018 00:22:40 +0900 Subject: [PATCH 4/8] Change to use log(x, base) --- lib/pure/math.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 9cadfb6b74..84d4005c30 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -148,13 +148,13 @@ else: # JS proc ln*(x: float32): float32 {.importc: "Math.log", nodecl.} proc ln*(x: float64): float64 {.importc: "Math.log", nodecl.} -proc log*[B, X: SomeFloat](b: B, x: X): auto = - ## Computes the logarithm base ``b`` of ``x`` +proc log*[X, B: SomeFloat](x: X, base: B): auto = + ## Computes the logarithm ``base`` of ``x`` when B is float64 or X is float64 var r: float64 else: var r: float32 - result = ln(x) / ln(b) + result = ln(x) / ln(base) when not defined(JS): # C proc log10*(x: float32): float32 {.importc: "log10f", header: "".} From 41601e93074c1ff657b5e696f9d5e73e57ac660c Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Tue, 5 Jun 2018 00:27:04 +0900 Subject: [PATCH 5/8] Bug fix --- lib/pure/math.nim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 84d4005c30..9fe24e358e 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -150,11 +150,12 @@ else: # JS proc log*[X, B: SomeFloat](x: X, base: B): auto = ## Computes the logarithm ``base`` of ``x`` - when B is float64 or X is float64 + when B is float64 or X is float64: var r: float64 else: var r: float32 - result = ln(x) / ln(base) + r = ln(x) / ln(base) + return r when not defined(JS): # C proc log10*(x: float32): float32 {.importc: "log10f", header: "".} From 090ffa386961459d529c2561d3e196be6df83c50 Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Tue, 5 Jun 2018 02:32:39 +0900 Subject: [PATCH 6/8] Use concrete expression --- lib/pure/math.nim | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 9fe24e358e..95a0a345c5 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -148,14 +148,13 @@ else: # JS proc ln*(x: float32): float32 {.importc: "Math.log", nodecl.} proc ln*(x: float64): float64 {.importc: "Math.log", nodecl.} -proc log*[X, B: SomeFloat](x: X, base: B): auto = +proc log*[T: SomeFloat](x, base: T): T = ## Computes the logarithm ``base`` of ``x`` - when B is float64 or X is float64: - var r: float64 - else: - var r: float32 - r = ln(x) / ln(base) - return r + ln(x) / ln(base) +proc log*(x: float64, base: float32): float64 = + ln(x) / ln(base) +proc log*(x: float32, base: float64): float64 = + ln(x) / ln(base) when not defined(JS): # C proc log10*(x: float32): float32 {.importc: "log10f", header: "".} From 5fc5e3719457846964bff8a77078598f01b56974 Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Thu, 14 Jun 2018 13:17:17 +0900 Subject: [PATCH 7/8] Use one same type for two parameters --- lib/pure/math.nim | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 95a0a345c5..52ed0a0a59 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -151,10 +151,6 @@ else: # JS proc log*[T: SomeFloat](x, base: T): T = ## Computes the logarithm ``base`` of ``x`` ln(x) / ln(base) -proc log*(x: float64, base: float32): float64 = - ln(x) / ln(base) -proc log*(x: float32, base: float64): float64 = - ln(x) / ln(base) when not defined(JS): # C proc log10*(x: float32): float32 {.importc: "log10f", header: "".} @@ -676,4 +672,4 @@ when isMainModule: doAssert floorMod(-8.5, 3.0) ==~ 0.5 block: # log - doAssert log(3.0, 4.0) == log(4.0) / log(3.0) + doAssert log(4.0, 3.0) == log(4.0) / log(3.0) From 5332da2e7cdf7a52e607f7b90acd67dc16fe2754 Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Thu, 14 Jun 2018 13:37:37 +0900 Subject: [PATCH 8/8] Fix a test --- lib/pure/math.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 52ed0a0a59..60750138f3 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -672,4 +672,4 @@ when isMainModule: doAssert floorMod(-8.5, 3.0) ==~ 0.5 block: # log - doAssert log(4.0, 3.0) == log(4.0) / log(3.0) + doAssert log(4.0, 3.0) == ln(4.0) / ln(3.0)