如何提高代码的精度



如何提高以下的准确性(精度)?

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.898556时,以38.9134结束。

由于意外整数部门,您正在遇到圆形/截断错误。要获得正确的双重精度,您需要将degrees.minutesdegrees.seconds隐式转换为double,如以下:

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

注意60-> 60.0和秒数中60至3600的校正。

有两个问题。

首先,Degree_Minutes结构的minutes成员被声明为整数类型,因此degrees.minutes/60将整数划分为整数,该整数会产生截断的整数结果。将其更改为degrees.minutes/60.会产生浮点结果。

第二,degrees.seconds/60不正确。要么应该是degrees.seconds/3600degrees.seconds/60添加到学位。

#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
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.0;
    remainder = fmod(temp,1);
    degrees_minutes.minutes = temp-remainder;
    degrees_minutes.seconds = remainder*60.0;
    return degrees_minutes;
}
double geo_deg_to_dec (Degree_Minutes degrees)
{
    double decimal = degrees.degrees + (degrees.minutes/60.0) + (degrees.seconds/60.0/60.0);
    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";
    cout << "This should be: 38deg 53' 54.801"" << endl;
    cout << endl;
    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";
    cout << "This should be: 38.898556" << endl;
    return EXIT_SUCCESS;
}

你差点有。您需要通过明确指定十进制(如其他答案中指定)来为武力双分裂,但是您还需要将秒数除以60两次。在实践中,我可能会将其更改为degrees.seconds/3600,但我将其按照说明。

相关内容

  • 没有找到相关文章

最新更新