Types - Integers & Double

I have the following line of code:

x = y - (1/(z+1)) * B,

where x, y, z, and B are integers.

What is the proper way to make sure x is computed accurately?

Thanks.
double x=y - (1/double(z+1)) * B,
Floating point numbers aren't perfectly accurate, though.
but x has to be an integer. so do i need to recast it if doing as u suggest? also since it has to be an integer, the accuracy is only needed to an integer. i just got a warning on conversions so am trying to fix that.
Assigning a floating-point value to an integer saves only the integer part of the value.
If you want to do some rounding, you'll have to use a little arithmetic.
http://www.cplusplus.com/forum/articles/3638/
Hope this helps.
the syntax at least is giving me an error on the above suggestion.
Last edited on
x = static_cast<int>( y - ( 1 / ( z + 1.0 ) ) * B );
Topic archived. No new replies allowed.