Home:ALL Converter>double overloaded sin() and cos() do not maintain 15-digit decimal precision

double overloaded sin() and cos() do not maintain 15-digit decimal precision

Ask Time:2021-05-16T13:15:36         Author:asjhdbashjdbasjhdbhjb

Json Formatter

Using this link as a guide, https://www.geeksforgeeks.org/difference-float-double-c-cpp/#:~:text=double%20is%20a%2064%20bit,15%20decimal%20digits%20of%20precision. double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and 52 bits for the value), i.e. double has 15 decimal digits of precision , the below code does not maintain 15 decimal digits of precision. Rather, 14.

It is for simple projectile motion calculator, where, the range of a projectile launched at 30 degrees should match that of the same projectile launched at 60 degrees.

#include <iostream>
#include <iomanip>

int main()
{
    const double g = 9.80665;
    const double pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;

    double a1 = 30.0;
    double a2 = 60.0;
    double v = 25.0;
    double vx1 = v * cos(a1 * pi/180.0);
    double vy1 = v * sin(a1 * pi/180.0);
    double vx2 = v * cos(a2 * pi/180.0);
    double vy2 = v * sin(a2 * pi/180.0);

    double t_max1 = 2 * vy1 / g;
    double t_max2 = 2 * vy2 / g;

    double range1 = t_max1 * vx1;
    double range2 = t_max2 * vx2;
    
    std::cout << std::setprecision(16) << range1 << ", " << range2 << std::endl;

    return 0;
}

Output: 55.19375906810931, 55.19375906810933

Author:asjhdbashjdbasjhdbhjb,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67553399/double-overloaded-sin-and-cos-do-not-maintain-15-digit-decimal-precision
yy