sqrt() of int type in C

前端 未结 4 2004
春和景丽
春和景丽 2020-12-11 19:27

I am programming in the c language on mac os x. I am using sqrt, from math.h, function like this:

int start = Data -> start_number;
double localSum;

for          


        
4条回答
  •  没有蜡笔的小新
    2020-12-11 19:43

    C-compilers do some automatic casting with double and int. you could also do the following:

    int start = Data -> start_number;
    int localSum;
    
    for (start; start <= end; start++) {
       localSum += sqrt(start);
    }
    

    Even if the localSum is an int this will still work, but it will always cut of anything beyond the point.
    For example if the return value of sqrt() is 1.365, it will be stored as a simple 1.

提交回复
热议问题