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

  • Home
  • SEARCH
  • 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 3401634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:02:23+00:00 2026-05-18T05:02:23+00:00

I am writing a method which calculates the average time interval between a series

  • 0

I am writing a method which calculates the average time interval between a series of events (specifically, button tapping in iPhone). Since the number of time intervals I want to average might change during the program’s life-cycle, I am storing the time readings in an object named tapTimes of type NSMutableArray so I don’t have to worry about managing the array size.

When I store the time readings as doubles, everything works ok (for simplicity, in the following example I am interested only in one time interval):

CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();
[tapTimes addObject:[NSNumber numberWithDouble:(double)time]];
[tapTimes removeObjectAtIndex:0];
double deltaT = [[tapTimes objectAtIndex:1] doubleValue] - [[tapTimes objectAtIndex:0] doubleValue];

Which, for tapping every second or so, gives (the first value is just the first time reading since the array is initialized with zeros):

deltaT 311721948.947153
deltaT 1.023200
deltaT 1.080004
deltaT 1.055961
deltaT 1.087942
deltaT 1.080074

However, if I store floats:

CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();
[tapTimes addObject:[NSNumber numberWithFloat:(float)time]];
[tapTimes removeObjectAtIndex:0];
float deltaT = [[tapTimes objectAtIndex:1] floatValue] - [[tapTimes objectAtIndex:0] floatValue];

Then I get unexpected results for deltaT:

deltaT 311721760.000000
deltaT 0.000000
deltaT 0.000000
deltaT 0.000000
deltaT 0.000000
deltaT 32.000000
deltaT 0.000000
deltaT 0.000000
deltaT 0.000000

Any idea what goes wrong? (I am doing mt first steps in Objective-c/cocoa, so I hope the answer is not too trivial :))

  • 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-18T05:02:24+00:00Added an answer on May 18, 2026 at 5:02 am

    The problem is that float is not precise enough to tell one timestamp from a timestamp a second later. Stick to double; there’s a reason that CFAbsoluteTime uses that type.

    Here’s a demonstration of the problem under 64-bit 10.6.4:

    val: 311721760.000000 - val2: 311721761.000000 - (val2 - val): 1.000000
    v: 311721760.000000 - v2: 311721760.000000 - (v2 - v): 0.000000
    

    The first line used double values; val2 was generated by adding 1 to val. The second line used float values. The source code follows:

    //clang so_float.m -o so_float
    #import <stdio.h>
    
    int
    main(void) {
      double val = 311721760.000000;
      double val2 = val + 1.0;
      fprintf(stderr, "val: %f - val2: %f - (val2 - val): %f\n", val, val2, val2 - val);
      float v = val;
      float v2 = val + 1.0;
      fprintf(stderr, "v: %f - v2: %f - (v2 - v): %f\n", v, v2, v2 - v);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a method which calculates a user's phone recharge credit expires within n
When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something
I am writing a Clone method using reflection. How do I detect that a
Is there a good method for writing C / C++ function headers with default
I'm writing tests for a business method that invokes some DAO classes to perform
I'm writing a unit test for a method that packs boolean values into a
I have a method that I'm writing that is calling another overloaded method inside
I'm writing a JavaSCript class that has a method that recursively calls itself. Scheduler.prototype.updateTimer
I'm having some trouble with a generic method I'm writing. It has the following
I'm writing a test for a file parser class. The parse method receives a

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.