Objective-C. Math functions - objective-c

I have a variable which contain the textfield value. I want to multiply that x value with a decimal number like 0.013. But after multiplication I got as answer 0.
It takes the decimal value as 0. What is the reason?

Once you get the text from the textfield, convert the string using floatValue :
CGFloat val = [myValue floatValue];
CGFloat res = val * 0.013;

In a comment, the OP notes
If i use 4/3 then it only takes answer as 1
This suggests that the problem is in how the value is initialized: 4/3 is integer division, returning the int value 1. The solution is to be sure that the calculations are actually dealing with floats, start to finish, by using float literals, e.g., replacing 4/3 with 4.0/3.0

you need to convert the text to the required format:
float floatValue = [yourTextField floatValue];
int intValue = [yourTextField intValuew];

Related

how to set precision in Objective-C

In C++, there is a std::setprecision function can set float/double precision.
how can i set precision in Objective-C? and this print below:
(lldb) p 10/200
(int) $0 = 0
(lldb) p (float)10/200
(float) $1 = 0.0500000007
line 3 result is 0.0500000007, why is '7' in the result? how can i get the result is 0.05?
Floating-point numbers are binary floating-point numbers. 0.05 cannot be represented exactly by a binary floating point number. The result cannot ever be exactly 0.05.
In addition, you are quite pointlessly using float instead of double. float has only six or seven digits precision. Unless you have a very good reason that you can explain, use double, which gives you about 15 digits of precision. You still won't be able to get 0.05 exactly, but the error will be much less.
You may use NSNumberFormatter to format objects in a wide variety of ways --- too numerous to list here, see the documentation available from Xcode. Also see the Data Formatting Guide.
You must make change between % modulo operator and its identifier f in order to get desired result.
NSString* formattedNumber = [NSString stringWithFormat:#"%.02f", myFloat];
%.02f tells the formatter that you will be formatting a float (%f) and, that should be rounded to two places, and should be padded with 0s.
Example:
%f = 25.000000 // results 25.000000
%.f = 25 // results 25
%.02f = 25.00 // results 25.00
plz use
double A = 0.0500000007;
NSString *b =[NSString stringWithFormat:#"$%.02f",A] ;
double B = [b doubleValue];

Will dividing an NSUInteger by 2 result in a whole number?

I am trying to do this in Objective-C:
self.nsarray.count/2
If the count is equal to 5, will the result be 5/2 = 2.5 or 5/2 = 2?
I am NSLogging the answer and it only shows me 2. I'm not sure if that's the actual answer or if's 2, because I am forced to use the %u format to log the answer. Please also explain the 'why' of this result.
The division with two whole numbers in Objective-C always produces a whole number as a result, in your case it would be NSUInteger, and 2 is a valid result in this case. To get a result with floating point at least one of your operands should be float typed, or at least one of them should be casted to float, so here's some options:
// Second part of division is float, so result is float as well
float result = self.array.count/2.
// First part of division is float, so result is float as well
float result2 = (float)self.array.count/2 // or you can type ((float)self.array.count)/2 for more clearance
Note that casting result to float isn't valid on your case, for instance in (float) (5/2) the result would be a whole number of type float (2.0) as you only cast a NSIntger to float
Floats are usually formatted in NSLog format as %f or %g

Objective-C: floorf( ) returns wrong value

Here is the code:
float passedPrice = 2.953;
float placed = 1000.0; //3 decimals
NSLog("%f", placed); // Gives 2953;
float withNoFractions = floorf(passedPrice * placed);
The value stored in withNoFractions is 2952! It shall be 2953. What is really strange is that it works some time.
Many decimal floating point fractions cannot be represented as exact fractions in binary, so they have to be approximated. 2.953 is being approximated as something like 2.95299999. When you multiply by 1000, the result is 2952.99999, and when you get the floor of this, it's 2952.
To solve this, you can either use round() instead of ffloorf(), or you can add 0.5 before calling ffloorf():
float withNoFractions = floorf(passedPrice * placed + 0.5);

Correct use casting float to int for percentage?

I'm using this code to attempt to convert a float to an int, then use it for UILabel text:
[progLabel setText:[NSString stringWithFormat:#"Deck Progress %i%%",progBar.progress*100]];
and I've tried this:
[progLabel setText:[NSString stringWithFormat:#"Deck Progress %i%%",(int)progBar.progress*100]];
The displayed text is always "0%".
I also tried this which works:
int pval=progBar.progress*100;
[progLabel setText:[NSString stringWithFormat:#"Deck Progress %i%%",pval]];
I've tried %f%% but that gives me too many digits - I'm trying to get only 0-100, no decimal places.
What am I doing wrong?
The (int) is applied only to progBar.progress because casting has higher precedence than multiplication, and (int)progBar.progress is always 0 if 0 ≤ progBar.progress < 1. You need to cast the whole expression like
(int)(progBar.progress * 100)
Alternatively, you could avoid casting at all, by using a format of floating point which does not show the fractional part.
[NSString stringWithFormat:#"Deck progress %.0f%%", progBar.progress * 100]
The displayed text is always "0%".
This is because you are not casting the float to integer: you are passing a float to an unsuspecting function expecting an integer. You should add a cast:
[progLabel setText:[NSString stringWithFormat:#"Deck Progress %i%%",(int)(progBar.progress*100)]];

NSTimeInterval to readable NSNumber

NSTimeInterval == double; (e.g. 169.12345666663)
How can I round up this double so that there are only 2 digits left after the "dot"?
It would be very good if the result is a NSNumber.
If this is for display purposes, take a look at NSNumberFormatter.
If you really want to round the double in your calculations for some reason, you can use the standard C round() function.
A NSDecimal can be rounded to a specified number of digits with NSDecimalRound().
double d = [[NSDate date] timeIntervalSince1970];
NSDecimal in = [[NSNumber numberWithDouble:d] decimalValue];
NSDecimal out;
NSDecimalRound( &out, &in, 2, NSRoundUp );
NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithDecimal:out];
If you really want two digits left after the dot, multiply by 100, round it using round() function, divide it by 100 again. However, this will not guarantee that it really has only two digits after the dot, since by dividing it again, you may get a number that cannot really be expressed with floating point notation and when you expect 0.1 you may in fact get 0.09999..., that's because you cannot really express 0.1 using floating point notation.
If you just want to round it to two digits after the dot for display purposes, you can use NSNumberFormatter as has been suggested or just use:
printf("%.2f\n", yourTimeInterval);
NSLog(#"%.2f\n", yourTimeInterval);
or to get an NSString, you can also use the following, which is probably even faster than using a NumberFormatter (however, it won't be localized according to the user prefs):
NSString * intervalStr = nil;
char * intervalStrTmp = NULL;
asprintf(&intervalStrTmp, "%.2f", yourTimeInteval);
if (intervalStrTmp) {
intervalStr = [[NSString alloc] initWithUTF8String:intervalStrTmp];
free(intervalStrTmp);
}
In the vast majority of cases rounding a number is something you should only do at display time. The properties of floating-point numbers (double or not) make it impossible to store certain numbers at a fixed-precision.
For information about formatting a number so it displays to two decimal places, see this other post.
Does this HumanReadableTimeInterval help? It returns a NSString, though.
Alternatively, you can round yourself by multiplying with 100, converting to an integer and dividing through 100 again.
I would just use the ANSI C round() function.
You can always round the number using:
double round2dec(double a) { return round(a * 100) / 100; }
But chances are that the representation of the result as a double will not have only 2 decimals.
Now, if by using the == sign, you meant that the comparison of your two double numbers is made only to the second decimal. Here is what you can do:
fabs(round2dec(NSTimeInterval) - round2dec(double)) < std::numeric_limits<double>::epsilon()