I'm trying to update a UILabel with the amount of time left on an audio track in minutes and seconds. I'm getting an Invalid operand to binary % error . Here's the code:
- (void)updateTimeLeft
{
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;
int min = timeLeft / 60;
int sec = timeLeft % 60;
self.timeDisplay.text = [NSString stringWithFormat:#"%d : %d", min,sec];
}
I changed the code to the following:
int sec = lroundf(timeLeft) % 60;
The error goes away, but I suspect that there's a problem here because the timer counts down correctly from 5:00 to 4:10, but then displays 4:9 instead of 4:09
thanks for the help
Alterations below,
self.timeDisplay.text = [NSString stringWithFormat:#"%02d:%02d", min,sec];
Related
in my app i need to submit the time to the game center and i need to show that in Elapsed Time - To the hundredth of a second format.
00:00:00.00
this is the format i want to show in leader board.
In my app im getting the time in following format
ss.SS
ss = seconds
SS = hundredth of a second
i converted the value to double before send it to the game center
double newScoreDouble = [newScore doubleValue];
But when i sending the double score to the game center it asking me to convert it to int64_t format. But when i convert it to that format it loses some part of the double value.
double intPart = 0;
double fractPart = modf(newScoreDouble, &intPart);
int isecs = (int)intPart;
int min = isecs / 60;
int sec = isecs % 60;
int hund = (int) (fractPart * 100);
int64_t time_to_send_through_game_center = min*6000 + (sec*100 + hund);
this is the way i convert double to int64_t
Can any one say how to send whole double value to the game center and display it in Elapsed Time - To the hundredth of a second format.
Thanks
I've done this before. When you're recording a score in the to the hundredth of a second format. You would multiply your seconds with a hundred before submitting.
So let's say the user scored 1minute 44 seconds 300 milliseconds : 1:44:30 = 104.3 seconds. Then you would set your value property of GKScore object equal to 104.3 * 100 = 10430 ,and submit it like that.
Give it a try :)
I have a double value as 47. I divide it by 60, but when I try to print it it shows 0.0000.
It should display 0.7858
How can I do that? Any help?
Here is my code:
int minutes = decimal * 60;
float min = (minutes)/60;
NSLog(#"%.4f",min);
Try this :
double decimal = 47.0 / 60.0; // Just so minutes is 47
double minutes = decimal * 60.0;
double min = minutes / 60.0;
NSLog(#"%.4f",min);
In Objective C (as well as C and C++), integer division is applied when both left and right operands are integers, so minutes / 60 = 0. If you want a floating point operation, use floating points literals.
try this:
int minutes = 40;
float min = (minutes * 1.0f)/60.0f;
NSLog(#"%.4f",min);
or
int minutes = 40;
float min = (float)minutes/60.0f;
NSLog(#"%.4f",min);
I am successfully making a CMTime with following code:
endPoint = CMTimeMake([mp currentPlaybackTime], 1);
The current position, which originally was a float value, of the movie in my MPMoviePlayerController is given back as a CMTime in seconds, which is not bad.
But how I can get this position in e.g. in milli seconds?
I played with the 'timescale' and set it to 10 and 100, but it didn't have an effect to the result.
Thank you in advance!
I’m not really sure what you want. Do you understand the logic behind CMTime?
1.0s = 1/1s = CMTimeMake(1, 1)
0.1s = 1/10s = CMTimeMake(1, 10)
0.2s = 1/5s = CMTimeMake(1, 5)
0.2s = 2/10s = CMTimeMake(2, 10)
…
In other words, CMTimeMake(a, b) is the time value a/b. Thus when you have a floating-point time value:
double time1 = 0.2;
// in ms, (0.2*1000)/1000 == 200/1000 == 0.2
CMTime time2 = CMTimeMake(time1*1000, 1000);
Maybe this is what you want?
I have the following code, and wanted to get other set of eyes to make sure I have written the right code to calculate the frame rate of a scene. Could you please chime in?
This is written for the iPad using SDK 3.2.
thanks!
- (void)drawView:(id)sender
{
mach_timebase_info_data_t timer;
mach_timebase_info(&timer);
uint64_t t1 = mach_absolute_time();
[renderer render];
uint64_t delta = mach_absolute_time() - t1;
delta *= timer.numer;
delta /= timer.denom;
NSLog(#"%lld ms: %.2f FPS", delta, 1000000000.0f/delta);
}
In case you want to measure the time spent rendering OpenGL, this won't work. OpenGL operations are processed in parallel and will not affect timing on the CPU. You can profile the time it takes to issue the OpenGL calls, but you won't be able to see how long it took them to finish.
This is unfortunate, but it makes sense. This is probably the reason why everyone's just eying their framerate: if the GPU can't finish processing in time, your CPU gets blocked and your timer (most likely CADisplayLink) will not fire "in time".
You may want to look into (expensive) profiling tools like gDEBugger, but I'm not sure they work on iOS.
I use CFAbsoluteTime to compute the frame duration in an openGL app. I stopped using mach_time because the results were not reliable.
- (void)update {
// Compute Frame Duration
static CFAbsoluteTime sPreviousTime = 0;
const CFAbsoluteTime newTime = CFAbsoluteTimeGetCurrent();
const CFAbsoluteTime deltaTime = newTime - sPreviousTime;
sPreviousTime = newTime;
float frameDuration = deltaTime;
// keep frameDuration in [0.01 ; 0.5] seconds
if (frameDuration > 0.5f) {
frameDuration = 0.5f;
} else if (frameDuration < 0.01f) {
frameDuration = 0.01f;
}
[self tick:frameDuration]; // use frameDuration to do something every frame
}
Short answer : yes what you are doing is correct.
Longer answer : to get the time in seconds for a delta between two mach_absolute_time calls, you need to do the following:
// I do this once at launch.
mach_timebase_info_data_t timer;
mach_timebase_info( &timer );
// Start time.
uint64_t t1 = mach_absolute_time( );
// Do activity.
// End time.
uint64_t t2 = mach_absolute_time( );
// Calculate delta.
uint64_t delta = t2 - t1;
// Use denom/numer from timer.
delta *= timer.numer;
delta /= timer.denom;
// Convert nanoseconds to seconds.
float secondsElapsed = ( float )( delta / 1000000000.0 );
Of course, if you want a FPS, from you need the inverse of the seconds:
1.0f / secondsElapsed;
In your case, instead of doing:
float secondsElapsed = ( float )( delta / 1000000000.0 );
You are doing:
float inverseSecondsElapsed = ( float )( 1000000000.0 / delta );
So you do indeed get the FPS as intended, so all should work as intended.
I am trying to display minutes and seconds based on a number of seconds.
I have:
float seconds = 200;
float mins = seconds / 60.0;
float sec = mins % 60.0;
[timeIndexLabel setText:[NSString stringWithFormat:#"%.2f , %.2f", mins,seconds]];
But I get an error: invalid operands of types 'float' and 'double' to binary 'operator%'
And I don't understand why... Can someone throw me a bone!?
A lot of languages only define the % operator to work on integer operands. Try casting seconds and mins to int before you use % (or just declare them int in the first place). The constant values you use will also need to be int (use 60 instead of 60.0).
As others have pointed out, you should be using integers. However, noone seems to have spotted that the result will be incorrect. Go back and have another look at modulo arithmetic, and you'll realize you should be doing
int seconds = 200;
int mins = seconds / 60;
int sec = seconds % 60;
Note the last line, seconds % 60 rather than mins % 60 (which will return the remainder of the minutes divided by 60, which is the number of minutes to the hour, and completely unrelated to this calculation).
EDIT
doh, forgot the ints... :)
The 60.0 forces a conversion to double
try:
float seconds = 200;
float mins = seconds / 60;
float sec = mins % 60;
Use ints instead. At least in your example, seems like they're enough (it will also be faster and clearer).
Also, in this case you would get 3.3333... mins, and not 3 minutes as expected. You could use Math.ceil(x) if you need to work with floats.
Do like this:
float seconds = 200.5;
float mins = floor(seconds / 60.0);
float sec = seconds - mins * 60.0;