コード例 #1
0
ファイル: general.php プロジェクト: Nycto/Round-Eights
 public function testSwap()
 {
     $var1 = "test";
     $var2 = "other";
     \r8\swap($var1, $var2);
     $this->assertEquals("test", $var2);
     $this->assertEquals("other", $var1);
 }
コード例 #2
0
ファイル: numbers.php プロジェクト: Nycto/Round-Eights
/**
 * Keeps a value within a range by wrapping values that fall outside the boundaries.
 *
 * Unlike intWrap, this function handles float values. However, because of this,
 * it means that the limits are considered equal.
 *
 * @param Integer|Float $value The value to be wrapped
 * @param Integer|Float $lower The lower limit of the wrap range
 * @param Integer|Float $upper The upper limit of the wrap range
 * @param Boolean $useLower Because the limits are equal, this toggles whether the upper or lower limit will be returned if the value becomes equal to one of them.
 *
 * @return Integer|Float Returns the value, wrapped to within the given range
 */
function numWrap($value, $lower, $upper, $useLower = TRUE)
{
    $value = \r8\numVal($value);
    $lower = \r8\numVal($lower);
    $upper = \r8\numVal($upper);
    $useLower = (bool) $useLower;
    if ($lower == $upper) {
        return $lower;
    }
    if ($upper < $lower) {
        \r8\swap($upper, $lower);
    }
    if (\r8\num\between($value, $lower, $upper)) {
        $out = $value;
    } else {
        $delta = $upper - $lower;
        if ($value < $lower) {
            $distance = $lower - $value;
            if ($distance > $delta) {
                $distance -= floor($distance / $delta) * $delta;
            }
            $out = $upper - $distance;
        } else {
            $distance = $value - $upper;
            if ($distance > $delta) {
                $distance -= floor($distance / $delta) * $delta;
            }
            $out = $lower + $distance;
        }
    }
    if ($useLower && $out == $upper) {
        return $lower;
    } else {
        if (!$useLower && $out == $lower) {
            return $upper;
        }
    }
    return $out;
}