Getting different output from seemingly identical calculations

Can someone tell me why the calculations on lines 9 and 11, which seem to be the same, produce two different outputs. I know that the difference is not that big, but I use these values ​​to draw lines with OpenGL, and the difference is noticeable.

#include <iostream>
#include <cmath>

int main()
{
    int ypos=400;

    /// Output: 410.
    std::cout <<  400+(sin((90*3.14159)/180)*10) << std::endl;

    ypos=ypos+(sin((90*3.14159)/180)*10);
    /// Output: 409.
    std::cout << ypos << std::endl;

    return 0;
}
+3
source share
4 answers

The real answer is somewhere around 409.9999999.

This also outputs doublerounding to 410, because the math is all built in:

std::cout <<  400+(sin((90*3.14159)/180)*10) << std::endl;

as yposdeclared as int, a double value is truncated to 409(this is a specific behavior when casting from doubleto int):

ypos=ypos+(sin((90*3.14159)/180)*10);

/// Output: 409.
std::cout << ypos << std::endl;

, , PI:

const double PI = 3.141592653589793238463;

std::cout <<  400+(sin((90*PI)/180)*10) << std::endl;

double int, . , :

ypos += round(sin((90*PI)/180)*10);
+5

 std::cout <<  400+(sin((90*3.14159)/180)*10) << std::endl;

,

std::cout << ypos << std::endl;
+6

GCC

float. - . 410, 409.99999923. , float, ++ 6 , , 410. . ++ , . 409.

+3

In order for your code to be stable, so your OpenGL result behaves well, you probably should do all (or as much as possible) of your calculations with doubling so that the effects of truncation do not accumulate at the end (where I assume you need an integer number of pixels) either always round to generate an integer value or truncate but not mix two.

+1
source

All Articles