2+2 is... umm.. 5 ?

Started by
5 comments, last by Jesper T 22 years, 9 months ago
I''m using msvc++ to do this: value = ((( 2 * 1960 ) / 2048 ) - 1 ); the value is previously declared as type double.. what i cant understand is that when i check the value''s value its 0 !!! how can this be possible ?? ive done more accurate calculations before, might it have something to do with how I''ve set up msvc++ ? any ideas ?
Advertisement
If you do the math it comes out to .91 . Now I can''t remember exactly as I''m still learning but a double doesn''t hold decimal values. Try using float and see if that helps.

I know only that which I know, but I do not know what I know.
((( 2 * 1960 ) / 2048 ) = 1
1-1 = 0

doesn''t matter if value is a double, the conversation is done after the integer expression is evalutated.

however

((( 2.0 * 1960 ) / 2048 )-1 ~ 0.91




Doubles handle decimals, handle 4 decimal more than float
I correct myself. Sorry about that. I tryed the code out and got the same thing you did. I split the math functions into multiple lines and that solved the problem.

  #include <iostream>int main(){	float Value;	Value =  2 * 1960;	Value = Value / 2048;	Value = Value - 1;	std::cout<< Value;	return 0;}  

I know only that which I know, but I do not know what I know.
try value = ((( 2.0 * 1960.0 ) / 2048.0 ) - 1.0 );
or value = ((( 2f * 1960f ) / 2048f ) - 1f );

i think, unless otherwise explicitly stated, the compiler will treat constant numbers as integers
Paul
ahh ok so its just to add .0 behind.. should have thought about that, thanks !

This topic is closed to new replies.

Advertisement