Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1838602
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:30:01+00:00 2026-05-17T06:30:01+00:00

In Objective-C I have a timer fire every 0.1 seconds and increment a double value

  • 0

In Objective-C I have a timer fire every 0.1 seconds and increment a double value (seconds) by 0.1.

So it should basically keep time counting up by 1/10 of a second. When it fires it checks some if–else statements to see if time (seconds) is equal to 3, 9, 33, etc., but these are never triggered. I suppose it is because of the way doubles are represented in bits, that is the decimal is an approximation and never actually a whole number.

How can I fix this so my statements are triggered?

-(void)timeSeconds:(NSTimer*)theTimer {
seconds = seconds + 0.1;
NSLog(@"%f", seconds);
if (seconds == 3.0) {
    [player pause];
    [secondsTimer invalidate];
}
else if (seconds == 9){
    [player pause];
    [secondsTimer invalidate];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:30:01+00:00Added an answer on May 17, 2026 at 6:30 am

    The floating point types cannot represent some numbers exactly, so when these are added, the error is compounded and the floating point type becomes less and less precise.

    Use an integral type but represent the time difference using greater resolution, for example, use an NSUInteger to represent milliseconds instead of seconds, and increment by 100 instead of 0.1. Instead of comparing seconds == 3.0, you would use milliseconds == 3000, etc.

    Keep in mind that timers are not fired very precisely:

    A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed.

    You may find that when milliseconds==9000, more than 9 seconds has actually passed (but probably not much more). There are other tools available if more precise timing is required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table named Info of this schema: int objectId; int time; int
I have a somewhat messily-formatted Objective-C code base. Is there a way to have
Say I have the following Objective-C class: @interface Foo { int someNumber; NSString *someString;
I'm learning objective-C and Cocoa and have come across this statement: The Cocoa frameworks
We have a collection of commercial MFC/C++ applications which we sell using Stingray Objective
Do I really have to learn Objective-C to develop solid Mac Apps? As Mac
I'm just beginning to have a look at Objective-C and Cocoa with a view
Will a MacBook Air have enough horsepower for occasional development in Ruby? Objective C?
I have some code which runs in a timer, but I would like to
I am trying to do a little timer app in Objective-C/Cocoa. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.