Math not coming up right in NSInteger Objective C

274 views Asked by At

So I've got a basic math equation I'm trying to perform in programming.

NSInteger testValue = (self.waveform.zoomStartSamples/self.waveform.totalSamples)*100;

Self.waveform.zoomStartSamples is @property (nonatomic, assign) unsigned long int zoomStartSamples;

self.waveform.totalSamples is @property (nonatomic, assign, readonly) unsigned long int totalSamples;

Here are the NSLog's I'm running

    NSLog(@"Value of testValue is %ld", (long)testValue);
    NSLog(@"zoomStartSamples are %lu", self.waveform.zoomStartSamples);
    NSLog(@"totalSamples are %lu", self.waveform.totalSamples);

And here is the result I get:

Value of testValue is 0 zoomStartSamples are 1033554 totalSamples are 4447232

I don't think the value of testValue should be coming up 0. Any ideas?

Ultimately I would like to float the value to another variable to use it elsewhere. Thanks.

2

There are 2 answers

0
gabbler On BEST ANSWER

This is because zoomStartSamples/totalSamples results in a int 0, so 0*100 = 0, try to use zoomStartSamples * 100/totalSamples instead.

0
Ken Thomases On

This is the nature of integer math. 1033554 / 4447232 = 0.23240388628252. However, that value can't be represented as an integer. It's between 0 and 1. In C, the result is truncated to 0. Then, you multiply that by 100, which still yields 0.

If you want fractional values, you need to use floating-point math. You can convert the result back to an integer at the end. In many cases, the compiler will do that final conversion automatically. For example:

NSInteger testValue = (self.waveform.zoomStartSamples/(double)self.waveform.totalSamples)*100;

By casting one subexpression to double, the division is performed with doubles. The numerator is automatically promoted to double. The result is a double. Similarly, the multiplication is performed with doubles; the 100 is promoted to a double. When the double result is assigned to an integer variable, it is truncated.