   Do you have to cast literal constants to avoid numbers from being truncated?
  Hah, I hate having to condense my question into 1 subject line. It always ends up being confusing.In my program, I have a ball that moves around the screen. The ball has an x velocity and a y velocity. The velocity is in pixels per second. To find out how much a ball should move, I measure the time in milliseconds since the last frame, and I divide that by 1000 to get the number of seconds:seconds = milliseconds / 1000;Both "seconds" and "milliseconds" are floats. Then I do something like this:x_position += x_velocity * seconds;The "x_position" and "x_velocity" variables are also floats. When I blit the ball to the screen, I simple cast x_position as an int, which I'm sure just truncates it. So, if x_position is 100.345 then the ball ends up getting blitted to 100.I figure that, even if (x_velocity * seconds) only results in a fraction of a pixel each frame, it should eventually add up to a whole pixel, which will result in a movement of 1 pixel after several frames. However, my ball never moves. I ran a million tests and I couldn't figure it out. Then, just for shits, I decided to cast the literal constant "1000" as a float. Lo and behold, it worked!seconds = milliseconds / (float)1000;I never, ever read that such a thing is necessary. It totally floored me that this worked. In fact, I'm still skeptical. Is it really necessary to cast the literal constant, or is it just a coincidence that this worked?P.S. This reminds me. I need to take a course on const correctness sometime.   
  If miliseconds is not a floating point data type, well, then miliseconds / 1000 is integer division, not floating point division. No need to cast the literal, 1000.0f is of type float (and 1000.0 is double)   ;  You could probably do:seconds = milliseconds / 1000.0f;and it will work.   ;  For a/b you get a floating point division if either a or b or both are floats. If one is an integer then it will be promoted to a float as long as the other is a float. If milliseconds is a float, you get a floating point result regardless of whether you cast 1000 to float or not, so that can't be your error. But incidentally, if you want floating point literals, you can do 1000.0f, if you want doubles you can do 1000.0, but like I said, that shouldn't make a difference if the situation you describe is accurate.   ;Quote:Original post by RDragon1If miliseconds is not a floating point data type, well, then miliseconds / 1000 is integer division, not floating point division. No need to cast the literal, 1000.0f is of type float (and 1000.0 is double)milliseconds is a float. I do it something like this:float milliseconds = (float)SDL_GetTicks() - last_frame_time;So, given that, is there any reason I would have to cast or use 1000.0f or 1000.0?I actually don't mind doing those. I just find this interesting.   ;Quote:Original post by outRiderFor a/b you get a floating point division if either a or b or both are floats. If one is an integer then it will be promoted to a float as long as the other is a float. If milliseconds is a float, you get a floating point result regardless of whether you cast 1000 to float or not, so that can't be your error. But incidentally, if you want floating point literals, you can do 1000.0f, if you want doubles you can do 1000.0, but like I said, that shouldn't make a difference if the situation you describe is accurate.I'm wondering if perhaps I made some other change to my program, along with the (float)1000, that I'm not remembering, that fixed the program, making it appear as though the (float)1000 did it when it in fact did not.