I am writing a program in Qt, in C ++, for translating the coordinate values ​​from the geodetic coordinate system into the coordinates of the rectangular coordinate system, in all calculations I need accuracy to at least the 8th digit.

For example, there is such a function for converting degrees to radians:

#include "convertorad.h" #include <QDebug> #include <QString> double convertToRad(double grad){ double rad; rad = (grad * 3.1415926535897932) / 180; QString str; str.setNum(rad); qDebug() << str; return rad; } 

The output of qDebug() with grad = 49.8344 and grad = 36.7203 as follows: enter image description here

I need a minimum of 0.86977439, and 0.8697743902. Ideally this is up to the 10th mark.

Here is the function itself for converting from a geodetic coordinate system to a rectangular one:

 #include "to_rectangular_cs.h" #include <QDebug> QString to_rectangular_cs(double BGrad, double BMin, double BSec, double LGrad, double LMin, double LSec, double h){ BGrad = BGrad + (BMin / 60); BGrad = BGrad + (BSec / 3600); double BRad; BRad = convertToRad(BGrad); LGrad = LGrad + (LMin / 60); LGrad = LGrad + (LSec / 3600); double LRad; LRad = convertToRad(LGrad); double alpha, E, N; int a = 6378137; alpha = 1 / 298.257223563; E = 2 * alpha - pow(alpha, 2); N = a / sqrt((1 - E * pow(sin(BRad), 2))); double X, Y, Z; X = (N + h) * (cos(BRad)) * (cos(LRad)); Y = (N + h) * (cos(BRad)) * (sin(LRad)); Z = (floor(((1 - E) * N + h) + 0.5)) * (sin(BRad)); QString res, str; res = str.setNum(X) + " " + str.setNum(Y) + " " + str.setNum(Z); return res; } 

The string that is returned by this function with .split ("") is divided and written to the QStringList class variable, and then set to using line.edText () in lineEdit. It is displayed with the exponent up to the 5th digit, but you need a minimum of 8th: enter image description here

  • one
    The answer is replete with variations of possibilities. Let the title about int not confuse. - alexis031182

2 answers 2

Just use the extra arguments to the setNum function:

 str.setNum(X, f, 10) 

Etc.

    The first. For pi, there is a constant M_PI, it lives in <cmath> .

    The second. All your numeric constants are interpreted by the C ++ standard as an int . That they were double , they must be written with a decimal zero at the end - 60.0

    Third. The double type is a number with a floating point. Its accuracy on the numerical axis is uneven. If you need uniform precision everywhere, use fixed-point arithmetic — for example, the GMP library.

    • one
      Regarding the second: the remark is fair and appropriate, but here it does not matter at all. - ixSci
    • Well, here while the author is lucky. And then he rolls 2/5 and will be very surprised. - gbg
    • one
      To solve this problem, the double type is suitable; I do not need a fixed-point number. I mistakenly thought that the values ​​I received using qDebug () are the true values ​​of the variables. In fact, when I convert a double number to a string, it is rounded off, and is not the true value stored in the machine’s memory. - Vitaliy Chervatyuk