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

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...).

Related

How to divide two integers without getting 0? [duplicate]

This question already has answers here:
Division of integers returns 0
(2 answers)
Closed 10 months ago.
My goal is two divide two integers in Presto 0.212, e. g. 1/2. The naive approach SELECT 1/2 returns 0. Next, I tried SELECT CAST(1/2 AS DOUBLE), but this also returns 0. How to divide 1/2 such that 0.5 is returned?
I'm not familiar with Presto, but my guess is that in the example you've provided 1/2 is being evaluated as an integer then is being cast as a double. Maybe something along the lines of SELECT CAST(1 AS DOUBLE)/CAST(2 AS DOUBLE) or you maybe you could just add .0 to the end of your numbers like SELECT 1.0/2.0. Just a few shots in the dark from me.

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.

Objective c division of two ints

I'm trying to produce a a float by dividing two ints in my program. Here is what I'd expect:
1 / 120 = 0.00833
Here is the code I'm using:
float a = 1 / 120;
However it doesn't give me the result I'd expect. When I print it out I get the following:
inf
Do the following
float a = 1./120.
You need to specify that you want to use floating point math.
There's a few ways to do this:
If you really are interested in dividing two constants, you can specify that you want floating point math by making the first constant a float (or double). All it takes is a decimal point.
float a = 1./120;
You don't need to make the second constant a float, though it doesn't hurt anything.
Frankly, this is pretty easy to miss so I'd suggest adding a trailing zero and some spacing.
float a = 1.0 / 120;
If you really want to do the math with an integer variable, you can type cast it:
float a = (float)i/120;
float a = 1/120;
float b = 1.0/120;
float c = 1.0/120.0;
float d = 1.0f/120.0f;
NSLog(#"Value of A:%f B:%f C:%f D:%f",a,b,c,d);
Output: Value of A:0.000000 B:0.008333 C:0.008333 D:0.008333
For float variable a : int / int yields integer which you are assigning to float and printing it so 0.0000000
For float variable b: float / int yields float, assigning to float and printing it 0.008333
For float variable c: float / float yields float, so 0.008333
Last one is more precise float. Previous ones are of type double: all floating point values are stored as double data types unless the value is followed by an 'f' to specifically specify a float rather than as a double.
In C (and therefore also in Objective-C), expressions are almost always evaluated without regard to the context in which they appear.
The expression 1 / 120 is a division of two int operands, so it yields an int result. Integer division truncates, so 1 / 120 yields 0. The fact that the result is used to initialize a float object doesn't change the way 1 / 120 is evaluated.
This can be counterintuitive at times, especially if you're accustomed to the way calculators generally work (they usually store all results in floating-point).
As the other answers have said, to get a result close to 0.00833 (which can't be represented exactly, BTW), you need to do a floating-point division rather than an integer division, by making one or both of the operands floating-point. If one operand is floating-point and the other is an integer, the integer operand is converted to floating-point first; there is no direct floating-point by integer division operation.
Note that, as #0x8badf00d's comment says, the result should be 0. Something else must be going wrong for the printed result to be inf. If you can show us more code, preferably a small complete program, we can help figure that out.
(There are languages in which integer division yields a floating-point result. Even in those languages, the evaluation isn't necessarily affected by its context. Python version 3 is one such language; C, Objective-C, and Python version 2 are not.)

How to generate a random float number in Objective-C with min and max? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Generating Random Numbers in Objective-C
What's the most optimal way to get a random floatingpoint number between floatA and floatB?
Hi,
I want to generate a random float number like
float scale = randFloat(0.5f, 2.0f);
How do I do this in objective-c?
Here is how you generate them :
// Get random value between 0 and 99
int x = arc4random() % 100;
// Get random number between 500 and 1000
int y = (arc4random() % 501) + 500);
You can easily extend this for any range you need.
Hope that helps.
Generate two random integers and divide one by the other. With some manipulation you can introduce a range.
Also, depending on the required resolution you could simply generate a single random integer and divide it by a fixed integer to get your float. For example, generate a random number between 0 and 100,000 and divide it by 100,000. You can offset and shift the result around to get the range you need.