Skip to content

Commit

Permalink
Add /-r, /-c and /-f
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoPascal31 committed Mar 3, 2025
1 parent 11d6fb0 commit d127beb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ Most functions of this module only handle ```decimal``` type. ```integer```s are
* ```(defun round* (decimals:integer x:decimal)``` : Reversed round function
* ```(defun floor* (decimals:integer x:decimal)``` : Reversed floor function
* ```(defun ceiling* (decimals:integer x:decimal)``` : Reversed ceiling function
* ```(defun /-r (decimals:integer x:decimal y:decimal)```: Rounded division
* ```(defun /-f (decimals:integer x:decimal y:decimal)```: Floored division
* ```(defun /-c (decimals:integer x:decimal y:decimal)```: Ceiled division
* ```(defun log10:decimal (x)``` : Returns the log of x base 10, rounded to 12 decimals
* ```(defun safe-log (x y default)``` : Log of Y base X, but returns default when y <= 0
* ```(defun safe-ln (x:decimal default:decimal)``` : Natural log of x, but returns default when x <= 0
Expand Down
41 changes: 41 additions & 0 deletions docs/source/util-math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,47 @@ As a result ``safe-/`` never fails (Division by *0.0* is not possible anymore).
0.0
In Pact, the native divide ``(/)`` function can produce up to 255 decimals. That's why
it's almost mandatory to "post-process" the division by rounding, ceiling or flooring.

The three functions below help for doing in a single operation: the division and the post-processing.

/-r
~~~
*decimals* ``integer`` *x* ``decimal`` *y* ``decimal`` ** ``decimal``

Divide and round to a given number of decimals.

.. code:: lisp
pact> (/-r 4 2.0 3.0)
0.6667
/-f
~~~
*decimals* ``integer`` *x* ``decimal`` *y* ``decimal`` ** ``decimal``

Divide and floor to a given number of decimals.

.. code:: lisp
pact> (/-f 4 2.0 3.0)
0.6666
/-c
~~~
*decimals* ``integer`` *x* ``decimal`` *y* ``decimal`` ** ``decimal``

Divide and ceil to a given number of decimals.

.. code:: lisp
pact> (/-c 4 2.0 3.0)
0.6667
Average and Median
-------------------
Expand Down
12 changes: 12 additions & 0 deletions pact/contracts/util-math.pact
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@
"Reversed ceiling function"
(ceiling x decimals))

(defun /-r (decimals:integer x:decimal y:decimal)
"Rounded division"
(round (/ x y) decimals))

(defun /-f (decimals:integer x:decimal y:decimal)
"Floored division"
(floor (/ x y) decimals))

(defun /-c (decimals:integer x:decimal y:decimal)
"Ceiled division"
(ceiling (/ x y) decimals))

;;; Log functions
(defun log10:decimal (x)
"Returns the log of x base 10, rounded to 12 decimals"
Expand Down

0 comments on commit d127beb

Please sign in to comment.