从double转换为float

时间:2014-11-08 19:15:53

标签: c++ visual-studio-2010 visual-studio-2012

我正在编写一个基本上调用func1和func2并打印

的程序

我在func1,我在func2,

然后询问用户输入值,

计算该值的平方根。由于某种原因,我得到了

1>c:\users\user\documents\visual studio 2010\projects\lab7.c\lab7.c\lab7.c(23): error C2143: syntax error : missing ')' before 'type'
1>c:\users\user\documents\visual studio 2010\projects\lab7.c\lab7.c\lab7.c(23): error C2198: 'sqrt' : too few arguments for call
1>c:\users\user\documents\visual studio 2010\projects\lab7.c\lab7.c\lab7.c(23): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


#include <stdio.h>

#include <math.h>

#define Pi 3.14159
float func3(float); // function's prototype
void func1(void);
void func2(void);


 main()
{
float r, s;

func1();
func2();

printf("Enter a number: ");
scanf ("%f", &r);
fflush(stdin);
s = func3(r); // function call
printf("\nThe square root is %.2f",s);
getchar();
}


void func1(void)
{
    printf("\n I'm in function 1");  

}


void func2(void)
{
    printf("\n I'm in function 2");  

}


float func3(float r) //function
{
float a;

  printf("\n I'm in function 3");

a=sqrt(double(r*r));
return a;
}

1 个答案:

答案 0 :(得分:0)

float隐式转换为double,因此您甚至无需转换它,您可以将area功能更改为以下内容。

float area(float r) //function
{
    printf("\n I'm in function 3");    
    return PI * r * r;  // Area = PI * r^2
}

另外,你的数学不正确,请参阅上面的圆形区域函数。你基本上有

a = sqrt(r^2)

基本上只是

a = r
相关问题