2
I am creating a micro engine, for study purposes, and wanted to simulate a movement vector of a body/object through time.
I started doing an infinite loop to do the object vector updates over time, but soon I had a problem with time: I’m using the ctime library, with the clock() function, saving the initial clock out of the loop, in a clock_t type. Then take new clocks, subtract, and divide by the CLOCKS_PER_SEC macro.
The problem that this division always shows me whole result, not real (float). In fact, when I have it printed, and force one of the split elements as float, the result comes out as float, but in the application of the program this does not happen.
The code below prints: 0 0 0 0 ... 1 1 1 ... 2 2 ...
clock_t t1 = clock();
clock_t t2;
while (1) {
t2 = ( clock() - t1 ) / (float)CLOCKS_PER_SEC;
cout << t2 << endl;
}
This one prints: 0.001 0.002 ... 0.542 0.543 ... 1.147... the way I wanted to use the values, but this only happens with printing. In assignment it saves the integer value.
clock_t t1 = clock();
clock_t t2;
while (1) {
cout << ( clock() - t1 ) / (float)CLOCKS_PER_SEC << endl;
}
How do I care about this problem? There’s been some mistake on my part?
I am using the Dev C++ IDE with the GCC compiler
You may declare
t2
asfloat
:float t2;
– suriyel
Wow, obg! It worked! I had been trying to do this with t1, n know pq, just so it worked!
– Gustavo Campos
Can you tell me why this happens?
– Gustavo Campos
clock_t
may be bothfloat
how muchint
, then need to define the type you want. Have more detailed answers in the OS if you want to know in depth.– suriyel