Created
February 11, 2014 10:54
-
-
Save mtrojanowski/8932814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Create an operation which will behave as the following | |
* set of functions (without using "if" statements): | |
* | |
* f(x) = 0, for x < 0; | |
* f(x) = x, for x >= 0 and x < 1; | |
* f(x) = 1, for x >= 1 and x < 2; | |
* f(x) = -x + 3, for x >=2 and x < 3; | |
* f(x) = 0, for x >= 3; | |
* | |
*/ | |
function f($x) | |
{ | |
$constZero = !($x < 0 || $x >= 3); | |
$ascendingFunction = ($x >= 0 && $x < 1); | |
$constValue = ($x >= 1 && $x < 2); | |
$descendingFunction = ($x >= 2 && $x < 3); | |
return $constZero * ($ascendingFunction*$x + $constValue*1 + $descendingFunction*(-$x + 3)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sweeeet! Now it seems so trivial...