gpt4 book ai didi

c++ - 如何提高代码的精度

转载 作者:太空狗 更新时间:2023-10-29 23:46:12 25 4
gpt4 key购买 nike

我怎样才能提高以下的准确性(精确度)?

struct Degree_Minutes { signed int degrees; signed int minutes; double seconds; };
Degree_Minutes geo_dec_to_deg (double dec)
{
Degree_Minutes degrees_minutes;
signed int degrees, minutes;
double remainder, temp, seconds;

remainder = fmod(dec, 1);
degrees_minutes.degrees = dec - remainder;
temp = remainder*60;
remainder = fmod(temp,1);
degrees_minutes.minutes = temp-remainder;
degrees_minutes.seconds = remainder*60;

return degrees_minutes;
}

double geo_deg_to_dec (Degree_Minutes degrees)
{
double decimal = degrees.degrees + (degrees.minutes/60) + (degrees.seconds/60);
return decimal;
}

int main(int argc, char **argv)
{
Degree_Minutes deg;
double decimal = 38.898556;

deg = geo_dec_to_deg(decimal);
cout << "Results of geo_dec_to_deg function: \n" << decimal << " was converted to " << deg.degrees << " degrees, " << deg.minutes << " minutes, " << deg.seconds << " seconds.\n";
decimal = geo_deg_to_dec(deg);
cout << "Results of geo_dec_to_deg function: \n" << deg.degrees << " degrees, " << deg.minutes << " minutes, " << deg.seconds << " seconds was converted to " << decimal << "\n";

return EXIT_SUCCESS;
}

编辑:忘记添加这里有一个结构:

struct Degree_Minutes { signed int degrees; signed int minutes; double seconds; };

当您从十进制转换为度/分/秒,然后再转换回十进制时,您最终得到的是 38.9134,而原来的是 38.898556。

最佳答案

由于意外的整数除法,您遇到了舍入/截断错误。要获得正确的 double ,您需要将 degrees.minutesdegrees.seconds 隐式转换为 double,如下所示:

double decimal = degrees.degrees + (degrees.minutes/60.0) + (degrees.seconds/3600.0);

注意 60 -> 60.0 以及在秒字段中将 60 更正为 3600。

关于c++ - 如何提高代码的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com