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
?>
Mathematical Functions
- 简介
- 安装/配置
- 预定义常量
- Math 函数
- abs — 绝对值
- acos — 反余弦
- acosh — 反双曲余弦
- asin — 反正弦
- asinh — 反双曲正弦
- atan2 — 两个参数的反正切
- atan — 反正切
- atanh — 反双曲正切
- base_convert — 在任意进制之间转换数字
- bindec — 二进制转换为十进制
- ceil — 进一法取整
- cos — 余弦
- cosh — 双曲余弦
- decbin — 十进制转换为二进制
- dechex — 十进制转换为十六进制
- decoct — 十进制转换为八进制
- deg2rad — 将角度转换为弧度
- exp — 计算 e 的指数
- expm1 — 返回 exp(number) - 1,甚至当 number 的值接近零也能计算出准确结果
- floor — 舍去法取整
- fmod — 返回除法的浮点数余数
- getrandmax — 显示随机数最大的可能值
- hexdec — 十六进制转换为十进制
- hypot — 计算一直角三角形的斜边长度
- is_finite — 判断是否为有限值
- is_infinite — 判断是否为无限值
- is_nan — 判断是否为合法数值
- lcg_value — 组合线性同余发生器
- log10 — 以 10 为底的对数
- log1p — 返回 log(1 + number),甚至当 number 的值接近零也能计算出准确结果
- log — 自然对数
- max — 找出最大值
- min — 找出最小值
- mt_getrandmax — 显示随机数的最大可能值
- mt_rand — 生成更好的随机数
- mt_srand — 播下一个更好的随机数发生器种子
- octdec — 八进制转换为十进制
- pi — 得到圆周率值
- pow — 指数表达式
- rad2deg — 将弧度数转换为相应的角度数
- rand — 产生一个随机整数
- round — 对浮点数进行四舍五入
- sin — 正弦
- sinh — 双曲正弦
- sqrt — 平方根
- srand — 播下随机数发生器种子
- tan — 正切
- tanh — 双曲正切
Hayley Watson ¶
3 months ago
Anonymous: ¶
4 years ago
Here is a simple number digit counter function, I used it to check if a number is a palindrome or not, Plus a Distance function, slope formula, sum from one to an integer, sum of odd numbers from one to specified number, and last but not least the sum of the squares of the values of the sine of x, and the cosine of y.
<?php
function digit_count($num){
for($i=0;pow(10,$i)<=$num;$i++){
if(pow(10,$i+1)>$num){
$num_digits = $i+1;
}
}
return $num_digits;
}
function distance($x_1,$y_1,$x_2,$y_2){
return sqrt(pow($x_1-$x_2,2)+pow($y_1-$y_2,2));
}
function slope($x_1,$y_1,$x_2,$y_2){
return ($y_2-$y_1)/($x_2-$x_1);
}
// example sum(20)=210=1+2+3+...+20
function sum($x){
return ($x)($x+1)/2;
}
// example odd(15)=225=1+3+5+7+9+...+15
function odd($x){
if(($x%2) == 0){
$x-=1;
}
$y=($x+1)/2;
return pow($y,2); //Same as $y*$y;
}
function ($x,$y){
if($x=90-$y){
echo "(Sine of $x)^2 + (Cosine of $y)^2=1";
$value = 1;
}else{
$value = pow(sin($x),2)+pow(cos($y),2);
}
return $value;
}
?>
Sanjay Ichalkaranje ¶
4 years ago
This is the only function I searched that I did not find anywhere on the Internet. This function calculates standard normal cumulative distribution for a particular value. This is NORMSDIST(x) in MS-Excel or OpenOffice.org Calc program.
<?
function normalDist($zScore) {
$p = floatval(0.2316419);
$b1 = floatval(0.319381530);
$b2 = floatval(-0.356563782);
$b3 = floatval(1.781477937);
$b4 = floatval(-1.821255978);
$b5 = floatval(1.330274429);
$t = 1/(1 + ($p * floatval($zScore)));
$zx = (1/(sqrt(2 * pi())) * (exp(0 - pow($zScore, 2)/2)));
$px = 1 - floatval($zx) * (($b1 * $t) + ($b2 * pow($t, 2)) + ($b3 * pow($t, 3)) + ($b4 * pow($t, 4)) + ($b5 * pow($t,5)));
return $px;
}
?>
