当前位置:首页 科普知识 平方根计算

平方根计算

发布时间:2023-09-06 12:31:23

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

平方根计算详细介绍

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

平方根计算

函数原型: 在VC6.0中的math.h头文件的函数原型为double sqrt(double);

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

平方根计算程序例

#include<math.h>#include<stdio.h>int main(void){    double x = 4.0,result;    result = sqrt(x); //result*result=x    printf("Thesquarerootof%fis%fn",x,result);    return 0;}

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

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

John Carmack's sqrt

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

static float CarmackSqrt (float x){       float xhalf = 0.5f * x;               int i = *(int*)&x;           // get bits for floating VALUE        i = 0x5f3759df - (i>>1);     // gives initial guess y0       x = *(float*)&i;             // convert bits BACK to float       x = x*(1.5f - xhalf*x*x);    // Newton step, repeating increases accuracy       x = x*(1.5f - xhalf*x*x);    // Newton step, repeating increases accuracy       x = x*(1.5f - xhalf*x*x);    // Newton step, repeating increases accuracy       return (1 / x);}

平方根计算pascal

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

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

平方根计算

c := sqrt(sqr(x-x)+sqr(y-y));

平方根计算gcc

Linux 中使用gcc编译器 需要加 -lm 作为链接,调用数学函数库math.h

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

例如:

#include<stdio.h>#include<math.h>//#include<stdlib.h>void main(){    double x;    double n=rand()%100;    printf("%lfn",n);    x=sqrt(n);    printf("%lfn",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 <iostream>//这里的cmath等价于C的math.h#include <cmath>using namespace std;int main(){     double x, result;     cin>>x;     result=sqrt(x);     cout<<x<<"的平方根是"<<result<<endl;     return 0;}//cmath等价于math.h,其就是using math.h的函数//VC 2008后为重载函数,原型为 float sqrt (float),double sqrt (double),long double sqrt(long double)
//注意没有sqrt (int),但是返回值可以为int

温馨提示:
本文【平方根计算】由作者 百科科普 转载提供。 该文观点仅代表作者本人, 自学教育网 信息发布平台,仅提供信息存储空间服务, 若存在侵权问题,请及时联系管理员或作者进行删除。
(c)2008-2025 自学教育网 All Rights Reserved 汕头市灵创科技有限公司
粤ICP备2024240640号-6