简体   繁体   English

Math.h function 问题在 c in code:: blocks

[英]Math.h function problem in c in code :: blocks

I wrote this code in C and it shows me this error.我在 C 中写了这段代码,它向我显示了这个错误。 I don't know what it is or how can I solve it.我不知道它是什么或我该如何解决它。

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

int main()
{
    int z;
    scanf("%d", &z);
    double x1 , x2 , x3 , x4 , y1 , y2 , y3 , y4;
    for(int i = 0;i<=z;i++)
        {
    scanf("%lf %lf", &x1 , &y1);
    scanf("%lf %lf", &x2 , &y2);
    scanf("%lf %lf", &x3 , &y3);
    scanf("%lf %lf", &x4 , &y4);
    double tule_parekhat1 = sqrt(pow(y2-y1, 2) + (pow(x2-x1), 2));
    double tule_parekhat2 = sqrt(pow(y3-y2, 2) + (pow(x3-x2), 2));
    double tule_parekhat3 = sqrt(pow(y4-y1, 2) + (pow(x4-x1), 2));
    double tule_parekhat4 = sqrt(pow(y4-y3, 2) + (pow(x4-x3), 2));

    }

}

I get the error (line 15, error: too few arguments to function 'pow')我收到错误(第 15 行,错误:arguments 到 function 'pow' 太少)

I don't know what it is.我不知道那是什么。

You have wrong usage of parentheses.您错误地使用了括号。

double tule_parekhat1 = sqrt(pow(y2-y1, 2) + (pow(x2-x1), 2));

in the (pow(x2-x1), 2)) part, it should be(pow(x2-x1), 2))部分,它应该是

double tule_parekhat1 = sqrt(pow(y2-y1, 2) + pow(x2-x1, 2));

function pow sees only x2-x1 function pow只看到x2-x1

Your code contains:您的代码包含:

double tule_parekhat1 = sqrt(pow(y2-y1, 2) + (pow(x2-x1), 2));

with 2 call of pow function. In first call you correctly write pow(y2-y1, 2) , actually calling the function with 2 parameters.两次调用pow function。在第一次调用中,您正确编写了pow(y2-y1, 2) ,实际上是使用 2 个参数调用 function。 But in second one, you write:但是在第二个中,你写:

(pow(x2-x1), 2)

Here you only call pow with the single x2-x1 parameter and then use the comma ( , ) operator to discard that value and only retain the second one.在这里,您仅使用单个x2-x1参数调用pow ,然后使用逗号 ( , ) 运算符丢弃该值并仅保留第二个值。 This is incorrect and the compiler correctly raises an error.这是不正确的,编译器会正确地引发错误。

You should write pow(x2-x1, 2) as you did for first call.您应该像第一次调用时那样编写pow(x2-x1, 2)


BTW, and IMHO you are abusing the pow function here.顺便说一句,恕我直言,您在这里滥用pow function。 It is intended to be used for complex operation where both the operands are floating point values.它旨在用于两个操作数都是浮点值的复杂运算。 Here the idiomatic way would be:这里惯用的方式是:

double tule_parekhat1 = sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
...

using only addition and product operators.仅使用加法运算符和乘积运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM