Why Float error is different in C? [closed] - objective-c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Please see the pictures:
I'm amazing the result is different.

A CGFloat is actually a double on 64 bit platforms. (It was a float on old 32 bit platforms.)
So here you're dividing a double by a double:
CGFloat price = 88888736 / a;
^^^^^ ^^^^^^^^ ^
double int -> double double
and here you're dividing a double by a float:
CGFloat price2 = 88888736 / 100.0f;
^^^^^^ ^^^^^^^^ ^^^^^^
double int -> double float
Change 100.0f to either 100.0 or (CGFloat)100 and you should be fine.
LIVE DEMO

CGFloat is a double on your machine, therefore what you are doing is this:
double a = 100.00f
double price = 88888736 / a
float a2 = 100.00f // `float` type enforced by the trailing `f`
double price2 = 88888736 / a2
The difference is that in the first case the division is a double division while in the second case this is a float division where the result is then assigned to a double. Since float division has less precision, you get the result you are seeing.

Related

Double Over Precision

Apologies if this has been asked already - not sure what to search for.
Simple bit of code:
double x = 4505;
double y = 1000;
double z = 1000000;
double result = (x * y) / z;
Answer should be 4.505; but I get:
result = 4.5049999999999999
The values of x, y and z could be anything, and sometimes I need that level of precision in the result but I can't see why this is happening.
The Question is how to I remove the rounding error so that I can re-run further calculations on the decimal value without getting erroneous results and at the same time maintain high level of precision for numbers that need it.
It's simply a Floating Point Rounding Error. Also there is this.
If you want result rounded to 3 decimal places, then use:
result = floor(result * 1000.0) / 1000.0;
or just during presentation:
NSLog(#"result = %.3f", result);

Why do these float calculations return different values? [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 9 years ago.
I'm working with an iOS project where I have to do a bit of math. Can anyone explain to me why these two implementations return different results?
float total = 31/30;
NSLog(#"%f", total); // returns 1.00000 in console
float total2 = 31/30.0;
NSLog(#"%f", total2); // returns 1.03333 in console
In the majority of computer languages, division involving two integers will have an integer result, the floor of the real result.
In C division, the type of the result is the type of the most precise number in the calculation. In your first example, both 31 and 30 are integers, and so the result is then the integer 1 which is cast to a float to result in 1.00. In your second example, while 31 is an integer, 30.0 is a literal float, and the calculation has a float result, which is than stored in your variable (1.033333...).

Calculate positive fractions in objective-c [duplicate]

This question already has answers here:
Objective c division of two ints
(4 answers)
Closed 9 years ago.
I tried to calculate 4/3 and store it into a float.
float answer = 4/3;
This only returns 1. Isn't objective-c able to calculate these kinds of fractions or do I have to do it any other way?
If numerator and denominator are both integers, then division will be integer. Use
float answer = 4/(float)3
4 and 3 are integers. So that division is an integer division, which evaluates to 1.
If you want a floating-point division, use (at least one) float literal.
float answer = 4f/3;
Your assignment contains an integer divide, which returns zero if the number you are dividing by is greater. You probably meant to do:
float p1 = (4.0f / 3.0f);
or
float p1 = ((float)4 / 3);

Calculation returns 0 objective-c [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 9 years ago.
I'm trying to calculate an average where i am getting the value from a textfield and multiplying it by the value of a label.
int grade1 = [[self.Cw1Grade text]intValue];
int grade1weight = self.weight1.text.intValue;
int a1grade = grade1 / 100;
int a1total = a1grade * grade1weight;
NSString *grade1total = [NSString stringWithFormat:#"%d", a1total];
[self.averageLabel setText:grade1total];
help appreciated thanks for your time
You can't divide integers like that. Or well, you can, but you won't get the result you expect, because integer divisions return integers (result will be rounded down to the next whole number)
Try this:
float a1grade = (float)grade1 / 100;
or
float a1grade = grade1 / 100.0;
If at least one of the operands is a float, you'll have a floating point division. But of course you have to store the result in a float variable in that case.
Also don't forget, that the string format specifier for floats is %f, not %d.

How to round of the value? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Objective-c: How to round off Float values?
I wanted to roundoff the float value from 42.56789 to 42.6. Can any one help me out in this.
Thanks in advance
There are two functions in c namely floor and ceil to roundoff the float values.
Refer Wikipedia reference
float value=42.56789;
NSString *roundedValue=[NSString stringWithFormat:#"%0.f",value];
NSLog(#"%#",roundedValue);
It prints 43 on console.
float f = 42.56789;
f *= 10.0f;
f = ceilf(f);
f /= 10.0f;
NSLog(#"%.02f",f);
I have done it manually .
float val=42.567890// float gives 6 decimal places
int x=(int) val;
float f=val-(int)val;
int o=(int) f*10;
float round=x+o/10+(f-(o/10))*1000)/50;