[WurstScript] Logarithm 1.2

Logarithm is a library for WurstScript which provides an easy way to calculate different logarithms.

There are no requirements except for the obligatory WurstScript StandardLib.

Code:

Tests:

GitHub:

Logarithm

Changelog:

1.0 - Initial release

1.1 - Now returns the logarithm of the absolute value of the argument. Crashs the thread if 0 is passed as an argument (debugmode only!).

1.2 - Changed the behavior for invalid input. Still crashs the thread in debugmode. Added inf constant (thanks to looking_for_help).

Credits:

- The awesome penguinking

Logarithm is a library for WurstScript which provides an easy way to calculate different logarithms.

There are no requirements except for the obligatory WurstScript StandardLib.

Code:

Code:

```
/**
* A package for calculating the logarithm to different bases.
*/
package Logarithm
/**
* Eulers number e.
*/
public constant real E = 2.718282
constant real INV_LOG_2_E = 0.6931472
constant real INV_LOG_2_10 = 0.3010300
constant real inf = Pow(2.0, 128.0)
/**
* Calculates the logarithm to base 2 of the absolute value of the argument.
* This is the binary logarithm or logarithm dualis.
*
* real a - the argument of the logarithm
*
* returns the result of log2|a|
*
* error - a = 0.0
*/
public function log2(real a) returns real
real x = a
real sign = 1
real res = 0.0
real p = 0.5
if x == 0.0
error("Cannot calculate the logarithm of 0.0!")
return -inf
if x < 0.0
x = -x
if x < 1
x = 1.0 / x
sign = -1.0
while x >= 2
res += 1
x *= 0.5
for int i = 1 to 23
x *= x
if x >= 2.
x *= 0.5
res += p
p *= 0.5
return sign * res
/**
* This is just a wrapper for log2(a).
*
* real a - the argument of the logarithm
*
* returns the result of lb|a|
*
* error - a = 0
*/
public function lb(real a) returns real
return log2(a)
/**
* This is just a wrapper for log2(a).
*
* real a - the argument of the logarithm
*
* returns the result of ld|a|
*
* error - a = 0
*/
public function ld(real a) returns real
return log2(a)
/**
* Calculates the logarithm to base 10.
*
* real a - the argument of the logarithm
*
* returns the result of lg|a|
*
* error - a = 0
*/
public function lg(real a) returns real
return log2(a) * INV_LOG_2_10
/**
* Calculates the logarithm to base e.
* This is the natural logarithm.
*
* real a - the argument of the logarithm
*
* returns the result of ln|a|
*
* error - a = 0
*/
public function ln(real a) returns real
return log2(a) * INV_LOG_2_E
/**
* Calculates the logarithm to an arbitrary base.
*
* real a - the argument of the logarithm
* real b - the base of the logarithm
*
* returns the result of log_{|b|}(|a|)
*
* error - b = 0
* - b = 1
* - b = -1
* - a = 0
*/
public function log(real a, real b) returns real
if (b == 1.0) or (b == -1.0) or (b == 0.0)
error(b.toString() + " is not a viable base!")
if (b == 0)
return 0.0
else if (a.abs() > 1)
return inf
else if (a.abs() < 1)
return -inf
return 1.0
return log2(a) / log2(b)
```

Code:

```
package LogarithmTest
import NoWurst
import Logarithm
import Wurstunit
function generalTest()
real delta = 0.00001
real r
r = log(-5, 2)
r.assertEquals(2.321928094887, delta)
r = log(1, 2)
r.assertEquals(0.0, delta)
@test function log2Test()
real delta = 0.00001
real r
r = log2(2.0)
r.assertEquals(1.0, delta)
r = log2(64.0)
r.assertEquals(6.0, delta)
r = log2(1.2345)
r.assertEquals(0.303926836480, delta)
r = log2(10000.0)
r.assertEquals(13.28771237955, delta)
@test function lgTest()
real delta = 0.00001
real r
r = lg(10.0)
r.assertEquals(1.0)
r = lg(10000.0)
r.assertEquals(4.0, delta)
r = lg(1.2345)
r.assertEquals(0.091491094267, delta)
@test function lnTest()
real delta = 0.00001
real r
r = ln(E)
r.assertEquals(1.0, delta)
r = ln(148.413159)
r.assertEquals(5.0, delta)
@test function logTest()
real delta = 0.00001
real r
r = log(4.0, 4.0)
r.assertEquals(1.0, delta)
r = log(10000.0, 2.0)
r.assertEquals(13.28771237955, delta)
```

Logarithm

Changelog:

1.0 - Initial release

1.1 - Now returns the logarithm of the absolute value of the argument. Crashs the thread if 0 is passed as an argument (debugmode only!).

1.2 - Changed the behavior for invalid input. Still crashs the thread in debugmode. Added inf constant (thanks to looking_for_help).

Credits:

- The awesome penguinking

Last edited by a moderator: