note 111235 deleted from book.math by cmb
| From: | cmb@php.net | Date: | Sun, 05 Jan 2020 08:38:13 +0000 |
| Subject: | note 111235 deleted from book.math by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213760@lists.php.net to get a copy of this message | ||
Note Submitter: Hayley Watson
----
Provides a function to rescale numbers so that the range [a,b] fits into the range [c,d].
<?php
function rescale($ab, $cd)
{
list($a, $b) = $ab;
list($c, $d) = $cd;
if($a == $b)
{
trigger_error("Invalid scale", E_USER_WARNING);
return false;
}
$o = ($b * $c - $a * $d) / ($b - $a);
$s = ($d - $c) / ($b - $a);
return function($x)use($o, $s)
{
return $s * $x + $o;
};
}
$fahr2celsius = rescale([32, 212], [0, 100]);
echo $fahr2celsius(98.6); // 37°C
?>