Skip to content

Instantly share code, notes, and snippets.

@chrisgraham
Created February 3, 2015 23:48
Show Gist options
  • Save chrisgraham/791bfb84d7d04e4cdb1d to your computer and use it in GitHub Desktop.
Save chrisgraham/791bfb84d7d04e4cdb1d to your computer and use it in GitHub Desktop.
diff --git a/FunctionRegistry.php b/FunctionRegistry.php
index 2e6eebd..67b0cdc 100644
--- a/FunctionRegistry.php
+++ b/FunctionRegistry.php
@@ -1399,17 +1399,17 @@ class ILess_FunctionRegistry
public function screen(ILess_Node_Color $color1, ILess_Node_Color $color2)
{
/*
- $rs1 = ILess_Math::substract('255', $color1->getRed(true));
- $rs2 = ILess_Math::substract('255', $color2->getRed(true));
- $r = ILess_Math::substract('255', ILess_Math::divide(ILess_Math::multiply($rs1, $rs2), '255'));
+ $rs1 = ILess_Math::subtract('255', $color1->getRed(true));
+ $rs2 = ILess_Math::subtract('255', $color2->getRed(true));
+ $r = ILess_Math::subtract('255', ILess_Math::divide(ILess_Math::multiply($rs1, $rs2), '255'));
- $rs1 = ILess_Math::substract('255', $color1->getGreen(true));
- $rs2 = ILess_Math::substract('255', $color2->getGreen(true));
- $g = ILess_Math::substract('255', ILess_Math::divide(ILess_Math::multiply($rs1, $rs2), '255'));
+ $rs1 = ILess_Math::subtract('255', $color1->getGreen(true));
+ $rs2 = ILess_Math::subtract('255', $color2->getGreen(true));
+ $g = ILess_Math::subtract('255', ILess_Math::divide(ILess_Math::multiply($rs1, $rs2), '255'));
- $rs1 = ILess_Math::substract('255', $color1->getBlue(true));
- $rs2 = ILess_Math::substract('255', $color2->getBlue(true));
- $b = ILess_Math::substract('255', ILess_Math::divide(ILess_Math::multiply($rs1, $rs2), '255'));
+ $rs1 = ILess_Math::subtract('255', $color1->getBlue(true));
+ $rs2 = ILess_Math::subtract('255', $color2->getBlue(true));
+ $b = ILess_Math::subtract('255', ILess_Math::divide(ILess_Math::multiply($rs1, $rs2), '255'));
*/
// Formula: Result Color = 255 - [((255 - Top Color)*(255 - Bottom Color))/255]
diff --git a/Math.php b/Math.php
index a24a5b7..1e7e2d8 100644
--- a/Math.php
+++ b/Math.php
@@ -50,6 +50,10 @@ class ILess_Math
return;
}
+ if (!function_exists('bcscale')) {
+ return;
+ }
+
if ($defaultPrecision) {
self::$defaultPrecision = (int)$defaultPrecision;
}
@@ -73,6 +77,10 @@ class ILess_Math
*/
public static function restore()
{
+ if (!function_exists('bcscale')) {
+ return;
+ }
+
if (self::$oldPrecision !== null
&& function_exists('ini_set')
) {
@@ -103,6 +111,10 @@ class ILess_Math
*/
public static function add($left_operand, $right_operand, $precision = null)
{
+ if (!function_exists('bcadd')) {
+ return strval(floatval($left_operand) + floatval($right_operand));
+ }
+
return is_null($precision) ? bcadd($left_operand, $right_operand) : bcadd($left_operand, $right_operand, $precision);
}
@@ -116,6 +128,17 @@ class ILess_Math
*/
public static function compare($left_operand, $right_operand, $precision = null)
{
+ if (!function_exists('bccomp')) {
+ $ret = floatval($left_operand) - floatval($right_operand);
+ if ($ret < 0) {
+ return -1;
+ }
+ if ($ret > 0) {
+ return 1;
+ }
+ return 0;
+ }
+
return is_null($precision) ? bccomp($left_operand, $right_operand) : bccomp($left_operand, $right_operand, $precision);
}
@@ -188,6 +211,10 @@ class ILess_Math
*/
public static function sqrt($operand, $precision = null)
{
+ if (!function_exists('bcsqrt')) {
+ return strval(sqrt(floatval($operand)));
+ }
+
return self::clean(is_null($precision) ? bcsqrt($operand) : bcsqrt($operand, $precision));
}
@@ -201,6 +228,10 @@ class ILess_Math
*/
public static function divide($left_operand, $right_operand, $precision = null)
{
+ if (!function_exists('bcdiv')) {
+ return strval(floatval($left_operand) / floatval($right_operand));
+ }
+
return is_null($precision) ? bcdiv($left_operand, $right_operand) : bcdiv($left_operand, $right_operand, $precision);
}
@@ -231,6 +262,12 @@ class ILess_Math
*/
public static function modulus($left_operand, $modulus)
{
+ if (!function_exists('bcdiv')) {
+ $ret = strval(floatval($left_operand) % floatval($right_operand));
+ if ($ret == '0') return NULL;
+ return $ret;
+ }
+
return bcmod($left_operand, $modulus);
}
@@ -244,6 +281,10 @@ class ILess_Math
*/
public static function multiply($left_operand, $right_operand, $precision = null)
{
+ if (!function_exists('bcmul')) {
+ return strval(floatval($left_operand) * floatval($right_operand));
+ }
+
return is_null($precision) ? bcmul($left_operand, $right_operand) : bcmul($left_operand, $right_operand, $precision);
}
@@ -257,19 +298,27 @@ class ILess_Math
*/
public static function power($left_operand, $right_operand, $precision = null)
{
+ if (!function_exists('bcpow')) {
+ return strval(floatval($left_operand) ^ floatval($right_operand));
+ }
+
return is_null($precision) ? bcpow($left_operand, $right_operand) : bcpow($left_operand, $right_operand, $precision);
}
/**
- * Substract one arbitrary precision number from another
+ * Subtract one arbitrary precision number from another
*
* @param string $left_operand The left operand, as a string.
* @param string $right_operand The right operand, as a string.
* @param integer $precision This optional parameter is used to set the number of digits after the decimal place in the result.
* @return string Returns the result as a string.
*/
- public static function substract($left_operand, $right_operand, $precision = null)
+ public static function subtract($left_operand, $right_operand, $precision = null)
{
+ if (!function_exists('bcsub')) {
+ return strval(floatval($left_operand) - floatval($right_operand));
+ }
+
return is_null($precision) ? bcsub($left_operand, $right_operand) : bcsub($left_operand, $right_operand, $precision);
}
@@ -357,7 +406,7 @@ class ILess_Math
return self::add($value, '0.' . str_repeat('0', $precision) . '5', $precision);
}
- return self::substract($value, '0.' . str_repeat('0', $precision) . '5', $precision);
+ return self::subtract($value, '0.' . str_repeat('0', $precision) . '5', $precision);
}
/**
@@ -420,7 +469,7 @@ class ILess_Math
if (!self::isNegative($value)) {
$value = self::add($value, '1', 0);
} else {
- $value = self::substract($value, '0', 0);
+ $value = self::subtract($value, '0', 0);
}
return self::clean(self::divide($value, $multiplier, $precision));
@@ -447,7 +496,7 @@ class ILess_Math
if (!self::isNegative($number)) {
$number = self::add($number, '0', 0);
} else {
- $number = self::substract($number, '1', 0);
+ $number = self::subtract($number, '1', 0);
}
return self::clean(self::divide($number, $multiplier, $precision));
@@ -510,7 +559,7 @@ class ILess_Math
case '+':
return self::clean(self::add($a, $b));
case '-':
- return self::clean(self::substract($a, $b));
+ return self::clean(self::subtract($a, $b));
case '*':
return self::clean(self::multiply($a, $b));
case '/':
diff --git a/Node/Operation.php b/Node/Operation.php
index 03bf14d..429433c 100644
--- a/Node/Operation.php
+++ b/Node/Operation.php
@@ -92,7 +92,7 @@ class ILess_Node_Operation extends ILess_Node implements ILess_Node_VisitableInt
$b = $a;
$a = $temp;
} else {
- throw new ILess_Exception_Compiler('Can\'t substract or divide a color from a number.');
+ throw new ILess_Exception_Compiler('Can\'t subtract or divide a color from a number.');
}
}
@chrisgraham
Copy link
Author

Whoops, couple of bugs in the above.

diff --git a/Math.php b/Math.php
index 1e7e2d8..7a8c854 100644
--- a/Math.php
+++ b/Math.php
@@ -262,8 +262,8 @@ class ILess_Math
      */
     public static function modulus($left_operand, $modulus)
     {
-        if (!function_exists('bcdiv')) {
-            $ret = strval(floatval($left_operand) % floatval($right_operand));
+        if (!function_exists('bcmod')) {
+            $ret = strval(floatval($left_operand) % floatval($modulus));
             if ($ret == '0') return NULL;
             return $ret;
         }
@@ -299,7 +299,7 @@ class ILess_Math
     public static function power($left_operand, $right_operand, $precision = null)
     {
         if (!function_exists('bcpow')) {
-            return strval(floatval($left_operand) ^ floatval($right_operand));
+            return strval(pow(floatval($left_operand), floatval($right_operand)));
         }

         return is_null($precision) ? bcpow($left_operand, $right_operand) : bcpow($left_operand, $right_operand, $precision);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment