文字

hypot

(PHP 4 >= 4.1.0, PHP 5)

hypot 计算一直角三角形的斜边长度

说明

float hypot ( float $x , float $y )

hypot() 函数将会跟据直角三角形的两直解边长度 xy 计算其斜边的长度。或者是从标点 (x, y) 到原点的距离。该函数的算法等同于 sqrt(x*x + y*y)

参数

x

第一条边的长度

y

第二条边的长度

返回值

计算斜边的长度

用户评论:

[#1] robinv at ecosse dot net [2004-01-07 09:18:26]

A simpler approach would be to allow an arbitrary number of parameters. That would allow for whatever number of dimensions you want *and* it would be backwards compatible with the current implementation.

<?php

function hypo() 
{
    
$sum 0;
    foreach (
func_get_args() as $dimension) {
        if (!
is_numeric($dimension)) return -1;
        
$sum += pow($dimension2);
    }
    return 
sqrt($sum);
}

print 
hypo();          // vector in 0 dimensions, magnitude = 0.
print hypo(1);         // vector in 1 dimension,  magnitude = 1.
print hypo(34);       // vector in 2 dimensions, magnitude = 5.
print hypo(236);     // vector in 3 dimensions, magnitude = 7.

?>

上一篇: 下一篇: