From 55ff7f3a93ae7030959986577e555310c4a923ae Mon Sep 17 00:00:00 2001
From: Revar Desmera <revarbat@gmail.com>
Date: Sat, 4 May 2019 20:10:23 -0700
Subject: [PATCH] Added sqr() and approx()

---
 math.scad | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/math.scad b/math.scad
index fb83f91..bcbbc59 100644
--- a/math.scad
+++ b/math.scad
@@ -56,6 +56,18 @@ function quantup(x,y) = ceil(x/y)*y;
 function constrain(v, minval, maxval) = min(maxval, max(minval, v));
 
 
+// Function: approx()
+// Usage:
+//   approx(a,b,[eps])
+// Description:
+//   Compares two numbers or vectors, and returns true if they are closer than `eps` to each other.
+// Arguments:
+//   a = First value.
+//   b = Second value.
+//   eps = The maximum allowed difference between `a` and `b` that will return true.
+function approx(a,b,eps=EPSILON) = let(c=a-b) (is_num(c)? c : norm(c)) <= eps;
+
+
 // Function: min_index()
 // Usage:
 //   min_index(vals);
@@ -121,6 +133,16 @@ function modrange(x, y, m, step=1) =
 	) [for (i=[a:step:c]) (i%m+m)%m];
 
 
+// Function: sqr()
+// Usage:
+//   sqr(x);
+// Description: Returns the square of the given number.
+// Examples:
+//   sqr(3);   // Returns: 9
+//   sqr(-4);  // Returns: 16
+function sqr(x) = x*x;
+
+
 // Function: gaussian_rand()
 // Usage:
 //   gaussian_rand(mean, stddev)