标签: sqrt

C 中的 sqrt 函数不返回精确值

我的代码如下:

#include <stdio.h>
#include <math.h>

int distance(int x1, int y1, int x2, int y2){
    double value = sqrt(pow((double)(x2- x1),2.0) + pow((double)(y2 - y1),2.0));
    return value;
}
int main(void){
    int A_x, A_y, B_x, B_y, C_x, C_y;
    printf("Enter 1st point:");
    scanf("%d", &A_x);
    scanf("%d", &A_y);
    printf("Enter 2nd point:");
    scanf("%d", &B_x);
    scanf("%d", &B_y);
    printf("Enter 3rd point:");
    scanf("%d", &C_x);
    scanf("%d", &C_y);
    double AB = distance(A_x, A_y, B_x, B_y);
    printf("%.2lf", AB);
    // double powered = pow((double)(A_x- B_x),2.0) + pow((double)(A_y - B_y),2.0);
    // double squared = sqrt(powered); …
Run Code Online (Sandbox Code Playgroud)

c math function sqrt

0
推荐指数
1
解决办法
59
查看次数

C++问题中的sqrt函数

#include <iostream>
#include <cmath>

using namespace std;

int main(){
    
    int n;
    cin >> n;
    
    int i = sqrt(1 + 2 * n * (n + 1)) - 1;
    
    cout << i;
}
Run Code Online (Sandbox Code Playgroud)

我编写了一个简单的程序,它利用了sqrt()C++ 中的函数。即使输入sqrt()为正,上述程序也会在控制台上打印出 n = 32768 的负值。我尝试将语句从 更改为int i = sqrt(1 + 2 * n * (n + 1)) - 1;
double i = sqrt(1 + 2 * n * (n + 1)) - 1;但错误未解决。

输出:

32768
-2147483648

上面的输出是为了 int i = …

c++ sqrt

0
推荐指数
1
解决办法
58
查看次数

Sqrt 未定义,不知道为什么

我收到这个错误,我不太确定为什么考虑到我在那里有导入数学行。

NameError: name 'sqrt' is not defined

import math
x = float(input())
y = float(input())
z = float(input())
print('{:.2f} {:.2f} {:.2f} {:.2f}'.format(pow(x, z), pow(x, pow(y, z)), abs(x - y), sqrt(pow(x, z))))
Run Code Online (Sandbox Code Playgroud)

编辑:我能够通过使用解决问题,math.sqrt但我不确定为什么在 pow 和 abs 函数工作时需要它。

python sqrt nameerror

0
推荐指数
1
解决办法
88
查看次数

while(i <= sqrt(static_cast <double>(n))

在"没有恐惧的C++:让你感觉聪明的初学者指南"一书中,在第(2)章:决策,决定中,您可以看到这段代码作为素数程序的一部分:

while (i<=sqrt(static_cast<double>(n))
Run Code Online (Sandbox Code Playgroud)

假设"i"被初始化为"2",并且"n"是用户的输入.

为什么我们要比较"n"的"sqrt"而不是"n"呢?

谢谢.

c++ primes sqrt

-1
推荐指数
1
解决办法
543
查看次数

标签 统计

sqrt ×4

c++ ×2

c ×1

function ×1

math ×1

nameerror ×1

primes ×1

python ×1