Why the math 1/2 dont return 0.5 ;
-
wrote on 24 Jan 2015, 18:45 last edited by
Hi, i have a strange problem, i have the code float test = 1/2;
but returns test= 0 ;i cannot understand this, and my code don't work !
See the screen shot from debugger!The problem is that the code on line 299 if(((dt%temp.period)==0), also dont work and i discover this inconvenience!
-
wrote on 24 Jan 2015, 19:00 last edited by
Try
@float test = 1.0/2.0; @ -
wrote on 24 Jan 2015, 19:20 last edited by
good that means in if state need to make a casting ?
-
wrote on 24 Jan 2015, 19:55 last edited by
Yes, you need to cast one of them if both of then integers:
@
int a = 1;
int b = 2;
float test = float(a) / b;
@ -
Not that any reasonable compiler wouldn't see this but why not just do this?
@
float test = 0.5f;
@
@andreyc - again, not that any compiler wouldn't see this but if you define a float avoid downcasting from a double:
@
float test = 1.0f/2.0f;
@
3/5