• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

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

## Recommended Posts

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 ?

##### Share on other sites
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.

##### Share on other sites
((( 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

##### Share on other sites
Doubles handle decimals, handle 4 decimal more than float

##### Share on other sites
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 int main(){ float Value; Value = 2 * 1960; Value = Value / 2048; Value = Value - 1; std::cout<< Value; return 0;}

##### Share on other sites
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

##### Share on other sites
ahh ok so its just to add .0 behind.. should have thought about that, thanks !

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627653
• Total Posts
2978433

• 10
• 12
• 22
• 13
• 33