Search code examples
iosaccelerometeraddition

Add together two UIAccelerationValue's?


I am not sure if this is a stupid question or not so please bear with me here. Anyway I have two UIAccelerationValue's. When I individually NSLog their results I get values like this respectively:

1. Value: -0.016186

2. Value2: -0.033460

However when I add or subtract the two UIAccelerationValue's together, I get values like this:

Value: -571.142049
Value: -1085.174017
Value: -2061.834754
Value: -3917.490156
Value: -7443.235420
Value: -14142.151420

As you can see the values just keep increasing exponentially.

Anyway, how can I just simply add or subtract the two UIAccelerationValues together without the results I am getting now?


Solution

  • I think you are storing the result of the addition/subtraction and are adding/subtracting over and over again, hence the increased, large values

    UIAccelerationValue is just a double (see UIAccelerometer.h):

    typedef double UIAccelerationValue;
    

    With the following example you get expected output:

    UIAccelerationValue xOne = -0.016186;
    UIAccelerationValue xTwo = -0.033460;
    
    NSLog(@"%f", xOne + xTwo); // prints out -0.049646
    NSLog(@"%f", xOne - xTwo); // prints out 0.017274