Login
7 branches 0 tags
Ben (M2 MBA) Optimized benchmarks 30d94a0 2 years ago 1077 Commits
nujel / stdlib / math / math.nuj
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Some convenient math functions and constants

(def PI 3.141592653589793)
(def π PI)

(defn inc (x)
      :inline
      "Return a number 1 greater than x"
      (+ 1 x))

(defn dec (x)
      :inline
      "Return a number 1 less than x"
      (- x 1))

(defmacro inc! (i v)
          "Decrement I by V (defaults to 1) and store the result in I"
          `(set! ~i (+ ~i ~(or v 1))))

(defmacro dec! (i v)
          "Decrement I by V and store the result in I"
          `(set! ~i (- ~i ~(or v 1))))

(defn +x (α)
      "Return a function that adds α to it's argument, useful for mapping"
      (fn (β)
          (+ α β)))

(defn radians (degrees)
      "Convert a quantity in degrees to radians"
      (/ (* π degrees) 180.0))

(defn min args
      "Returns the minimum value of its arguments

      args: A varible amount of numbers

      The smallest value"
      :cat :math

      (deftest 2 (min 2 4 9 25))
      (deftest 1 (apply min '(1 4 9 25)))
      (deftest 8.2 (reduce [32 9 8.2] min))
      (deftest 4 (min 25.3 4 9.1))
      (deftest 1 (apply min '(25 4 9 1)))
      (deftest 1 (reduce [25 4 9 1] min))
      (deftest #nil (min))
      (deftest 4 (min 4))
      (deftest 4 (min 4 9))

      (reduce args (fn (a b) (if (< a b) a b))))

(defn max args
      "Returns the maximum value of its arguments

      args: A variable amount of numbers

      The biggest value"
      :cat :math

      (deftest #nil (max))
      (deftest 1 (max 1))
      (deftest 9.1 (max 4 9.1))
      (deftest 25 (max 1 4 9 25))
      (deftest 25 (apply max '(1 4 9 25)))
      (deftest 25 (reduce [1 4 9 25] max))
      (deftest 31.0 (max 31.0 4 9 1))
      (deftest 25 (apply max '(25 4 9 1)))
      (deftest 25 (reduce [25 4 9 1] max))

      (reduce args (fn (a b) (if (> a b) a b))))