Why does this code return two different values ​​by doing the same?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    double a;
    double b;
    double q0 = 0.5 * M_PI + 0.5 * -2.1500000405000002;
    double q1 = 0.5 * M_PI + 0.5 * 0.0000000000000000;
    double w0 = 0.5 * M_PI + 0.5 * -43000.0008100000050000;
    double w1 = 0.5 * M_PI + 0.5 * -0.0000000000000000;
    double m = 1;
    double g = 43000000.81;
    double l1 = 0.1;
    double l2 = 0.1;
    double h = 0.0001;

    a = ((-g / l1) * sin(q0) + (sin(q1 - q0) * (cos(q1 - q0) * (w0 * w0 + (g / l1) * cos(q0)) + l2 * (w1 * w1 / l1))) / (m + pow(sin(q1 - q0), 2)));
    a = h * a;

    b = h * ((-g / l1) * sin(q0) + (sin(q1 - q0) * (cos(q1 - q0) * (w0 * w0 + (g / l1) * cos(q0)) + l2 * (w1 * w1 / l1))) / (m + pow(sin(q1 - q0), 2)));

    printf("%.20lf ", a);
    printf("%.20lf", b);
    return 0;
}

I do the same calculations with a and b, just with the difference that I get the value of a in two steps, and b in one.

My code returns: -629.47620126173774000000 -629.4762012617376363000000

What is the reason for the difference between the last two decimal places?

+4
source share
2 answers

Standard C (99 and 11) says:

Values ​​of operations with floating operands and values ​​subject to ordinary arithmetic transformations and floating constants are evaluated in a format whose range and accuracy may be greater than what is required by the type.

, , h*(X+Y), b, X+Y, double, - double. a=X+Y; a=h*a; , double, .

, " ". C-,

, , , , .

, , , , , , .

, , b, , (, , double).

cppreference FLT_EVAL_METHOD . FLT_EVAL_METHOD #pragma STDC FP_CONTRACT OFF.

+2

, a = bcde x = bc y = de a. = xy.

, .

0

Source: https://habr.com/ru/post/1687267/


All Articles