How to use decimal variable - objective-c

I am using a For loop to cycle through variables in an array that contain percentages. For each value I run through an equation to give me an average of all the percentages. The only problem is that I am unsure how to change the variable types to accommodate decimals whilst also work in with my equation.
// Calculate average of all percentages for specific exercise
long totalNumber = 0;
int temp;
long numberOfObjects = [iterationArray count];
for (NSArray *object in iterationArray) {
temp = [(NSNumber *)[object objectAtIndex:4] intValue]/100;
totalNumber = totalNumber + temp;
}
exercisePercentage = (totalNumber/numberOfObjects)*100;
return exercisePercentage;
I know that temp is the variable that needs to be a decimal. I was considering using an NSDecimalNumber but didn't know how to fit in with the rest of the code to return a long value.
I've tried with making temp a double or float but both return 0 on the line temp = [(NSNumber *)[object objectAtIndex:4] intValue]/100; despite the fact that [(NSNumber *)[object objectAtIndex:4] intValue] is 80.

Division of int with int always returns int, so you need to convert one value into float or double, something like this:
// Calculate average of all percentages for specific exercise
double totalNumber = 0.0;
double temp;
long numberOfObjects = [iterationArray count];
for (NSArray *object in iterationArray) {
temp = [(NSNumber *)[object objectAtIndex:4] intValue]/100.0;
totalNumber = totalNumber + temp;
}
exercisePercentage = (totalNumber/numberOfObjects)*100.0;
return exercisePercentage;
See it's 100.0, now it should work.

Please change the type of temp form int to float or double.
double temp;
float temp;

Related

Obtaining float from division between NSIntegers

I have two NSInteger variables called "domande" and "corrette". I have to execute this operation with them: corrette*10/domande. I want the result to be a float variable, so I declared a "voto" variable as so: "float voto = corrette*10/domande;" . When I output the value of "voto" with NSLog I get an approximated value of the result followed by ".000000".
Here's the code:
NSInteger domande = [numDomande integerValue];
NSInteger corrette = [numRisposteCorrette integerValue];
float voto = corrette*10/domande;
NSLog(#"float value is: %f", voto);
When I assign to "domande" a value of 7, and to "corrette" a value of 4: voto=5.000000
Instead it should be voto=5.71...
How can I have the division return not an integer type converted to float, but directly a float type?
Simplest way is to do:
float voto = 10.0f * corrette / domande;
By making the first argument a float, you guarantee that the others will be promoted as well and that intermediate and final results will not suffer truncation.
You could achieve a similar result by casting corrette to a float but I tend to prefer simplicity where possible.
Rather than converting integers to floats, you could just get floats in the first place:
CGFloat domandeFloat = [numDomande floatValue];
CGFloat corretteFloat = [numRisposteCorrette floatValue];
CGFloat voto = (corretteFloat / domandeFloat) * 10.0f
NSLog(#"float value is: %f", voto);
NSInteger does not have a method called floatValue. NSInteger is just an int. Instead, the solution would be:
CGFloat domandeFloat = [[NSNumber numberWithInt: numDomande] floatValue];
CGFloat domandeFloat = [[NSNumber numberWithInt: numRisposteCorrette] floatValue];
CGFloat voto = (corretteFloat / domandeFloat) * 10.0f;
Try to convert the NSIntegers to a float type first:
float voto = (float)corrette*10/(float)domande;
you can cast "10" from int to float by writing it as "10.0"
float voto = corrette*10.0/domande;
or
float voto = ((float)corrette*10) / (float)domande;
Operation "/" is returning type of it operands - 5/4 will return int result 1 because 5 and 4 are int, and 5.0/4.0 will return 1.25, because 5.0 and 4.0 are interpreted as float values. So you should manually cast type of input variables corrette and domande to float

speed up this Standard Deviation method

I have this method to calculate the standard deviation of an array of NSNumber integers, given a mean. The calculation uses NSDecimals to retain the highest resolution. This is currently demanding many cpu cycles, any help to speed it up while retaining the resolution required is appreciated! Thank you.
-(NSDecimal)standardDeviationOf:(NSMutableArray *)array withMean:(NSDecimal)mean {
if (![array count]) return CPTDecimalFromInt(0);
NSDecimal sumOfSquaredDifferences = CPTDecimalFromInt(0);
for (NSNumber *number in array) {
NSDecimal valueOfNumber = CPTDecimalFromInt([number intValue]);
NSDecimal difference = CPTDecimalSubtract(valueOfNumber, mean);
sumOfSquaredDifferences = CPTDecimalAdd(sumOfSquaredDifferences, CPTDecimalMultiply(difference, difference));
}
return CPTDecimalFromDouble(
sqrt(
CPTDecimalDoubleValue(sumOfSquaredDifferences) / [[NSNumber numberWithInt:[array count]] doubleValue]
)
);
}
An NSDecimal has 38 digits of precision, whereas double has roughly 16 digits of precision. But at the end of your loop, when you convert sumOfSquaredDifferences to double for the sqrt function, all the extra precision you had in the NSDecimal is "lost". You might as well perform the arithmetic of your inner loop using double, which should be much faster than NSDecimal:
double sumOfSquaredDifferences = 0;
double valueOfMean = [mean doubleValue];
for (NSNumber *number in array) {
double valueOfNumber = [number intValue];
double difference = valueOfNumber - valueOfMean;
sumOfSquaredDifferences += difference * difference;
}
return CPTDecimalFromDouble(sqrt(sumOfSquaredDifferences /
double([array count])));

NSNumber and decimal value

I have an NSNumber like this for example = 1978, i would like to convert this for : 1K9, seconde example : 35700 convert to : 35K7 ( where "k" is kilometers and "M" is meters, how i can do this
thanks
int temp;
NSNumber *yourNumber;//the number you enter from some where
NSString *newValue;
if([yourNumber intValue]>1000){
temp = [yourNumber intValue] % 1000 ;//your number module 1000
newValue= [[temp stringValue]stringByAppendingString:#"K"];
}
Note: I haven't my mac with me, if the [temp stringValue] gives any worning&error please inform me.
Here's how:
NSNumber *initialNumber = [NSNumber numberWithInt:35700];
NSString *resultString = [NSString stringWithFormat:#"%iK%i", floor(initialNumber / 1000), floor((initialNumber % 1000) / 100)];
Basically you can work with the internal number data.
Assuming you are working on a meter-based value, you might want something like this:
NSNumber *sourceValue = ... // your NSNumber value from any source
int meters = sourceValue.intValue;
int km = floor(meters / 1000); // only your kilometers
int sub_km = meters % 1000; // only the part behind the kilometers
int first_sub_km = floor(sum_km / 100); // the first digit of the subrange
NSString *readable = [NSString stringWithFormat:#"%iK%i", km, first_sub_km];
First, you split the meters into <= 1000 and > 1000.
Then you'll just have to put that out formatted, with a K in between.
Write your own subclass of NSNumberFormatter. In this subclass you can implement the calculation logic.
The logic might look like this.
Devide the value by thousend and add your "k"
if you want to have the first digit of hundreds get the thired last digit of your value
return the new string

Odd behavior with NSUInteger - can't convert to float properly

Here is my situation. Its driving me nuts:
I have an NSMutableArray with a count value of 517. I have a double value that is my multiplier.
double multiplier = 0.1223;
double result = [myArray count] * multiplier; // 63 even (wrong!)
In fact it should be 63.2291. If I go:
double result = [myArray count] * 0.1223; // 63.2291 (right!)
or..
double result = 517 * multiplier; // 63.2291 (right!)
Does this make any sense to anyone?
Addendum:
here is my actual function:
- (double) getValueForPercentage:(double)percVal
{
int adjustedCount = [originalData count] - 1;
double final = percVal * (double)adjustedCount;
return final;
}
I never get any digits beyond the decimal point when I do this. It does however work if I get rid of the "-1", a-la:
- (double) getValueForPercentage:(double)percVal
{
int adjustedCount = [originalData count];
double final = percVal * (double)adjustedCount;
return final;
}
Of course, I need to have the -1.
Second addendum:
Another interesting thing I noted was, if I pass a hard-coded number to this function it works fine, but if I pass the double value that I need to use, it fails:
int pointCount = [srcData getDayCount];
for (int i = 0; i < pointCount; i++) {
double progress = (double)i/(double)(pointCount - 1);
double satv = [srcData getValueForPercentage:progress];
// satv is always a number without any digits beyond the decimal
}
Well, when I started to have these issues i looked around a bit and found no reason or explanation.
What I do now is make everything become an NSNumber and then call doubleValue on it. This should yield the results you're looking for:
NSNumber * pointCount = [NSNumber numberWithUnsignedInt: [srcData getDayCount]];
for (NSInteger i = 0; i < [pointCount intValue]; i++) {
NSNumber * count = [ NSNumber numberWithInt: i ];
double progress = [count doubleValue]/[pointCount doubleValue] - 1.0;
double satv = [srcData getValueForPercentage:progress];
// satv is always a number without any digits beyond the decimal
}
Hope it helps.

initialization makes integer from pointer without a cast

here is the code, where listArray is NSMutableArray
......
listArray = [[NSMutableArray alloc] initWithCapacity:100];
for ( int i = 0 ; i < 100 ; i ++ )
[listArray addObject:[NSNumber numberWithInt:i]];
....
for(int max=99; max >= 0 ; max --)
{
int rN = (arc4random() % max) + 0;
//Warning 1: initialization makes integer from pointer without a cast
int temp = [listArray objectAtIndex:rN];
[listArray insertObject:[listArray objectAtIndex:max] atIndex:rN];
//passing argument 1 of 'insertObject:atIndex:' makes pointer from integer without a cast
[listArray insertObject:temp atIndex:max];
}
You need to "unbox" the int value:
int temp = [[listArray objectAtIndex:rN] intValue];
The array stores objects (here an NSNumber), while you want a plain int.
The same holds for insertion, that's why you call [NSNumber numberWithInt:...] while adding.
Edit: The indices in the array range from 0 ... (count-1), not sure if your +1 does what it should.
It should be:
NSNumber *temp = [listArray objectAtIndex:rN];
NSMutableArray holds Obj-C objects, which is why you have to use NSNumber numberWithInt:. That means when you pull it out with objectAtIndex, you get a NSNumber *.