平方根计算

平方根计算

功 能: 一个非负实数的平方根

函式原型: 在VC6.0中的math.h头档案的函式原型为double sqrt(double);

说明:sqrt系Square Root Calculations(平方根计算),通过这种运算可以考验CPU的浮点能力。

    • 中文名:平方根函式
    • 外文名:sqrt
    • 功能:计算一个非负实数的平方根
    • 函式原型:double sqrt(double)
    • 套用:考验CPU的浮点能力
    • 头档案:math.h
    • 本质:程式函式

程式例

#include#includeintmain(void){doublex=4.0,result;result=sqrt(x);//result*result=xprintf("Thesquarerootof%fis%f\n",x,result);return0;}

VC 2008后为重载函式,原型为 float sqrt (float),double sqrt (double),double long sqrt(double long)

注意没有sqrt (int),但是返回值可以为int

John Carmack's sqrt [C/C++]

Carmack的sqrt计算函式在批量计量时的耗时比系统库函式还要少,优异的性能的根本原因就是那个令无数人膜拜的魔数0x5F3759DF。

staticfloatCarmackSqrt(floatx){floatxhalf=0.5f*x;inti=*(int*)&x;//getbitsforfloatingVALUEi=0x5f3759df-(i>>1);//givesinitialguessy0x=*(float*)&i;//convertbitsBACKtofloatx=x*(1.5f-xhalf*x*x);//Newtonstep,repeatingincreasesaccuracyx=x*(1.5f-xhalf*x*x);//Newtonstep,repeatingincreasesaccuracyx=x*(1.5f-xhalf*x*x);//Newtonstep,repeatingincreasesaccuracyreturn(1/x);}

pascal

a := sqrt(sqr(x-x[j])+sqr(y-y[j]));

b := sqrt(sqr(x-x[k])+sqr(y-y[k]));

c := sqrt(sqr(x[j]-x[k])+sqr(y[j]-y[k]));

gcc

Linux 中使用gcc编译器 需要加 -lm 作为连结,调用数学函式馆math.h

rand()函式是产生随机数的一个随机函式。函式包含在头档案stdlib.h

例如:

/*档案名称test.c*/#include#include//#includevoidmain(){doublex;doublen=rand()%100;printf("%lf\n",n);x=sqrt(n);printf("%lf\n",x);}

EXCEL函式

返回正平方根。 语法 SQRT(number) Number 要计算平方根的数。 说明 如果参数 Number 为负值,函式 SQRT 返回错误值 #Num!。

示例

Python函式

#!/usr/bin/env python

import math # This will import math module

print("math.sqrt(100) is:", math.sqrt(100))

C++

#include//这里的cmath等价于C的math.h#includeusingnamespacestd;intmain(){doublex,result;cin>>x;result=sqrt(x);cout<

相关词条

相关搜索

其它词条