常用数学函数
计算两个给定数平方和的平方根 (√x2+y2)
std::hypot,
std::hypotf,
std::hypotl
定义于头文件 | ||
float hypotf( float x, float y ); | (1) | (C99 起) |
double hypot( double x, double y ); | (2) | (C99 起) |
long double hypotl( long double x, long double y ); | (3) | (C99 起) |
定义于头文件 | ||
#define hypot( x, y ) | (4) | (C99 起) |
1-3) 计算 x
与 y
平方和的平方根,而不会在计算的中间阶段有过度的上溢或下溢。
4) 泛型宏:若任何参数拥有 long double 类型,则调用函数的 long double 版本。否则,若任何参数拥有整数类型或 double 类型,则调用函数的 double 版本。否则,调用函数的 float 版本。
此函数计算的值是直角边长度为 x
和 y
的直角三角形的斜边长,或点 (x,y)
距原点 (0,0)
的距离,或复数 x+iy
的绝对值。
参数
x | - | 浮点值 |
y | - | 浮点值 |
返回值
若不出现错误,则返回直角三角形的斜边, √x2
+y2
。
若出现上溢所致的值域错误,则返回 +HUGE_VAL
、 +HUGE_VALF
或 +HUGE_VALL
。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- hypot(x, y) 、 hypot(y, x) 及 hypot(x, -y) 等价
- 若参数之一为 ±0 ,则
hypot
等价于以非零参数调用 fabs - 若参数之一为 ±∞ ,则
hypot
返回 +∞ ,即使另一参数为 NaN - 否则,若任何参数为 NaN ,则返回 NaN
注意
实现通常保证小于 1 ulp (最后位置单位)的精度: GNU 、 BSD 、 Open64 。
hypot(x, y) 等价于 cabs(x + I*y) 。
POSIX 指定仅若二个参数均为非正规且正确结果亦为非正规才可以出现下溢(这禁止朴素实现)。
hypot(INFINITY, NAN) 返回 +∞ ,但 sqrt(INFINITY*INFINITY+NAN*NAN) 返回 NaN 。
调用示例
#include <iostream>
#include <cstdlib>
#include <typeinfo>
#include <cinttypes>
#include <cmath>
int main()
{
//1-3) 计算 base 的 exponent 次幂。
const float fNumberX = 1.314;
const float fNumberY = 13.14;
std::cout << "typeid(float).name(): " << typeid(float).name() << std::endl;
for (int i = 0; i < 100; i += 10)
{
std::cout << "std::hypot(" << fNumberX + i << "," << fNumberY + i << "): "
<< std::hypot(fNumberX + i, fNumberY + i) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < 100; i += 10)
{
std::cout << "std::hypot(" << fNumberX - i << "," << fNumberY - i << "): "
<< std::hypot(fNumberX - i, fNumberY - i) << std::endl;
}
std::cout << std::endl;
const double dNumberX = 13.14;
const double dNumberY = 131.4;
std::cout << "typeid(double).name(): " << typeid(double).name() << std::endl;
for (int i = 0; i < 100; i += 10)
{
std::cout << "std::hypotf(" << dNumberX + i << "," << dNumberY + i << "): "
<< std::hypotf(dNumberX + i, dNumberY + i) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < 100; i += 10)
{
std::cout << "std::hypotf(" << dNumberX - i << "," << dNumberY - i << "): "
<< std::hypotf(dNumberX - i, dNumberY - i) << std::endl;
}
std::cout << std::endl;
const long double ldNumberX = 131.4;
const long double ldNumberY = 131.452;
std::cout << "typeid(long double).name(): " << typeid(long double).name() << std::endl;
for (int i = 0; i < 100; i += 10)
{
std::cout << "std::hypotl(" << ldNumberX + i << "," << ldNumberY + i << "): "
<< std::hypotl(ldNumberX + i, ldNumberY + i) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < 100; i += 10)
{
std::cout << "std::hypotl(" << ldNumberX - i << "," << ldNumberY - i << "): "
<< std::hypotl(ldNumberX - i, ldNumberY - i) << std::endl;
}
std::cout << std::endl;
return 0;
}
输出
typeid(float).name(): f
std::hypot(1.314,13.14): 13.2055
std::hypot(11.314,23.14): 25.7578
std::hypot(21.314,33.14): 39.4024
std::hypot(31.314,43.14): 53.3069
std::hypot(41.314,53.14): 67.3105
std::hypot(51.314,63.14): 81.3621
std::hypot(61.314,73.14): 95.4404
std::hypot(71.314,83.14): 109.535
std::hypot(81.314,93.14): 123.641
std::hypot(91.314,103.14): 137.754
std::hypot(1.314,13.14): 13.2055
std::hypot(-8.686,3.14): 9.23614
std::hypot(-18.686,-6.86): 19.9054
std::hypot(-28.686,-16.86): 33.2738
std::hypot(-38.686,-26.86): 47.0964
std::hypot(-48.686,-36.86): 61.0654
std::hypot(-58.686,-46.86): 75.0993
std::hypot(-68.686,-56.86): 89.1674
std::hypot(-78.686,-66.86): 103.256
std::hypot(-88.686,-76.86): 117.357
typeid(double).name(): d
std::hypotf(13.14,131.4): 132.055
std::hypotf(23.14,141.4): 143.281
std::hypotf(33.14,151.4): 154.985
std::hypotf(43.14,161.4): 167.066
std::hypotf(53.14,171.4): 179.449
std::hypotf(63.14,181.4): 192.075
std::hypotf(73.14,191.4): 204.899
std::hypotf(83.14,201.4): 217.886
std::hypotf(93.14,211.4): 231.009
std::hypotf(103.14,221.4): 244.245
std::hypotf(13.14,131.4): 132.055
std::hypotf(3.14,121.4): 121.441
std::hypotf(-6.86,111.4): 111.611
std::hypotf(-16.86,101.4): 102.792
std::hypotf(-26.86,91.4): 95.265
std::hypotf(-36.86,81.4): 89.3567
std::hypotf(-46.86,71.4): 85.4039
std::hypotf(-56.86,61.4): 83.6841
std::hypotf(-66.86,51.4): 84.334
std::hypotf(-76.86,41.4): 87.3007
typeid(long double).name(): e
std::hypotl(131.4,131.452): 185.864
std::hypotl(141.4,141.452): 200.007
std::hypotl(151.4,151.452): 214.149
std::hypotl(161.4,161.452): 228.291
std::hypotl(171.4,171.452): 242.433
std::hypotl(181.4,181.452): 256.575
std::hypotl(191.4,191.452): 270.717
std::hypotl(201.4,201.452): 284.859
std::hypotl(211.4,211.452): 299.002
std::hypotl(221.4,221.452): 313.144
std::hypotl(131.4,131.452): 185.864
std::hypotl(121.4,121.452): 171.722
std::hypotl(111.4,111.452): 157.58
std::hypotl(101.4,101.452): 143.438
std::hypotl(91.4,91.452): 129.296
std::hypotl(81.4,81.452): 115.154
std::hypotl(71.4,71.452): 101.012
std::hypotl(61.4,61.452): 86.8695
std::hypotl(51.4,51.452): 72.7274
std::hypotl(41.4,41.452): 58.5852