- Contract / Module:
- free.util-math
- Dependencies:
- free.util-lists
decimal
pact> PI
3.141592653589793
decimal
Euler's number
pact> E 2.718281828459045
decimal
Golden Ratio (Phi)
pact> GOLDEN-RATIO
1.6180339887498949025257388711906969547271728515625
x <a[integer,decimal]>
→ decimal
Starred version of (*dec)
. Convert an integer to a decimal, but does not fail
if x is already a decimal.
pact> (dec* 3)
3.0
pact> (dec* 3.0)
3.0
a decimal
b decimal
x decimal
→ bool
Return true is a <= x <= b.
pact> (between -1.0 1.0 0.2)
true
pact> (between -1.0 1.0 1.2)
false
xdecimal
→decimal
pact> (sign 2.0)
1.0
pact> (sign -2.0)
-1.0
pact> (sign 0.0)
0.0
y integer
→ bool
Return true if x is even.
pact> (is-even 4)
true
pact> (is-even 5)
false
y integer
→ bool
Return true if x is odd.
pact> (is-odd 4)
false
pact> (is-odd 5)
true
y integer
→ integer
Returns the storage size of a positive integer in bytes
pact> (sizeof 1)
1
pact> (sizeof 255)
1
pact> (sizeof 256)
2
pact> (sizeof 65536)
3
x integer
→ integer
Increment an integer
pact> (++ 1)
2
x integer
→ integer
Decrement an integer
pact> (-- 1)
0
a integer
b integer
→ integer
Return the greatest common divisor of a and b.
Zeros are allowed for a or b or both.
(gcd 0 0)
returns 0.
Negative numbers are allowed.
pact> (gcd 8 0)
8
pact> (gcd 0 0)
0
pact> (gcd 21 9)
3
pact> (gcd -9 21)
3
a integer
b integer
→ integer
Return the least common multiple of a and b.
Zeros are not allowed for any of the arguments.
Negative numbers are allowed.
pact> (lcm 6 16)
48
pact> (lcm 16 -6)
48
pact> (lcm 3 5)
15
pact> (lcm 3 0)
util-math.pact:185:4: Arguments can't be 0
x decimal
y decimal
→ decimal
Return the min of 2 values.
pact> (min 1.1 5.1)
1.1
x decimal
y decimal
→ decimal
Return the max of 2 values.
pact> (max 1.1 5.1)
5.1
x decimal
y decimal
z decimal
→ decimal
Return the min of 3 values.
pact> (min3 1.1 8.2 5.1)
1.1
x decimal
y decimal
z decimal
→ decimal
Return the max of 3 values.
pact> (max3 1.1 8.2 5.1)
8.2
x decimal
y decimal
z decimal
zz decimal
→ decimal
Return the min of 4 values.
pact> (min4 1.1 8.2 -1.0 5.1)
1.1
x decimal
y decimal
z decimal
zz decimal
→ decimal
Return the max of 4 values.
pact> (max4 1.1 8.2 -1.0 5.1)
8.2
x [decimal]
→ decimal
Return the min of a list.
pact> (min-list [1.1 8.2 -1.0 5.1])
1.1
x [decimal]
→ decimal
Return the max of a list.
pact> (max-list [1.1 8.2 -1.0 5.1])
8.2
x [decimal]
→ integer
Return the min index of a list.
pact> (amin [1.1 8.2 -1.0 5.1])
2
x [decimal]
→ integer
Return the max index of a list.
pact> (amax [1.1 8.2 -1.0 5.1])
1
low-limit decimal
up-limit decimal
x decimal
→ decimal
Clamp x between low-limit and up-limit:
- if x > up-limit => Return up-limit
- if x < low-limit => Return low-limit
- if x is between low-limit and up-limit => Return x
Equivalent to MAX(low-limit, MIN(up-limit, x))
pact> (clamp 10.0 20.0 12.0)
12.0
pact> (clamp 10.0 20.0 8.0)
10.0
pact> (clamp 10.0 20.0 25.0)
20.0
- Remarks:
- There is no function to add 2 decimals here. Indeed the native (+ x y) can be used.
x decimal
y decimal
z decimal
→ decimal
Return the sum of 3 values.
pact> (sum3 1.0 2.0 3.0)
6.0
x decimal
y decimal
z decimal
zz decimal
→ decimal
Return the sum of 4 values.
pact> (sum4 1.0 2.0 3.0 4.0)
10.0
x [decimal]
→ decimal
Return the sum of a list.
pact> (sum [1.0 2.0 3.0 4.0])
10.0
x decimal
y decimal
z decimal
→ decimal
Return the product of 3 values.
pact> (prod3 2.0 3.0 4.0)
24.0
x decimal
y decimal
z decimal
zz decimal
→ decimal
Return the product of 4 values.
pact> (prod4 2.0 3.0 4.0 1.5)
36.0
x [decimal]
→ decimal
Return the product of a list.
pact> (prod [2.0 3.0 4.0 1.5])
36.0
x decimal
→ decimal
Return the square of x.
pact> (square 6.0)
36.0
x decimal
y decimal
default decimal
→ decimal
Divide x by y but returns default if y is 0.0.
As a result safe-/
never fails (Division by 0.0 is not possible anymore).
pact> (safe-/ 3.0 2.0 0.0)
1.5
pact> (safe-/ 3.0 0.0 0.0)
0.0
x [decimal]
→ decimal
Returns the average of a list.
pact> (avg [3.0 0.0 1.0 2.0])
1.5
x [decimal]
→ decimal
Returns the median of a list: if the length of the list is even return the (n/2 -1)th element.
pact> (med [3.0 0.0 1.0 2.0 4.0])
2.0
pact> (med [3.0 0.0 1.0 2.0])
1.0
x [decimal]
→ decimal
Returns the median of a list: if the length of the list is even return the average of the (n/2 -1)th and (n/2)th elements.
pact> (med* [3.0 0.0 1.0 2.0 4.0])
2.0
pact> (med* [3.0 0.0 1.0 2.0])
1.5
x integer
→ decimal
Return 10^x, rounded to 12 decimals (rounding is important when x is negative).
pact> (pow10 2)
100.000000000000
pact> (pow10 -2)
0.010000000000
x decimal
y integer
→ decimal
Return x.10^y, rounded to 12 decimals.
pact> (xEy 4.0 2)
400.000000000000
pact> (xEy 4.0 -2)
0.040000000000
x <a[integer,decimal]>
→ decimal
Return the log of x base 10, rounded to 12 decimals.
pact> (log10 100)
2.000000000000
pact> (log10 0.001)
-3.000000000000
x <a[integer,decimal]>
y <a[integer,decimal]>
default <a[integer,decimal]>
→ <a[integer,decimal]>
Log of y base x, but returns default when y is negative.
As a result safe-log
never fails (Log of negative numbers not possible anymore).
pact> (safe-log 2 1024.0 0.0)
10.0
pact> (safe-log 2 -1.0 0.0)
0.0
x decimal
default decimal
→ decimal
Natural log of x, but returns default when x <= 0.0.
As a result safe-ln
never fails (Log of negative numbers is not possible anymore).
pact> (safe-ln 2.0 0.0)
0.69314718055994528622676398299518041312694549560546875
pact> (safe-ln -2.0 0.0)
0.0
x <a[integer,decimal]>
default decimal
→ decimal
Returns the log of x base 10, rounded to 12 decimals but returns default when x is negative.
As a result safe-log10
never fails (Log of negative numbers is not possible anymore)
pact> (safe-log10 20.0 0.0)
1.301029995664
pact> (safe-log10 -20.0 0.0)
0.0