Calculate positive fractions in objective-c [duplicate] - objective-c

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

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.

Short Rounds Up? [duplicate]

Does anyone know why integer division in C# returns an integer and not a float?
What is the idea behind it? (Is it only a legacy of C/C++?)
In C#:
float x = 13 / 4;
//== operator is overridden here to use epsilon compare
if (x == 3.0)
print 'Hello world';
Result of this code would be:
'Hello world'
Strictly speaking, there is no such thing as integer division (division by definition is an operation which produces a rational number, integers are a very small subset of which.)
While it is common for new programmer to make this mistake of performing integer division when they actually meant to use floating point division, in actual practice integer division is a very common operation. If you are assuming that people rarely use it, and that every time you do division you'll always need to remember to cast to floating points, you are mistaken.
First off, integer division is quite a bit faster, so if you only need a whole number result, one would want to use the more efficient algorithm.
Secondly, there are a number of algorithms that use integer division, and if the result of division was always a floating point number you would be forced to round the result every time. One example off of the top of my head is changing the base of a number. Calculating each digit involves the integer division of a number along with the remainder, rather than the floating point division of the number.
Because of these (and other related) reasons, integer division results in an integer. If you want to get the floating point division of two integers you'll just need to remember to cast one to a double/float/decimal.
See C# specification. There are three types of division operators
Integer division
Floating-point division
Decimal division
In your case we have Integer division, with following rules applied:
The division rounds the result towards zero, and the absolute value of
the result is the largest possible integer that is less than the
absolute value of the quotient of the two operands. The result is zero
or positive when the two operands have the same sign and zero or
negative when the two operands have opposite signs.
I think the reason why C# use this type of division for integers (some languages return floating result) is hardware - integers division is faster and simpler.
Each data type is capable of overloading each operator. If both the numerator and the denominator are integers, the integer type will perform the division operation and it will return an integer type. If you want floating point division, you must cast one or more of the number to floating point types before dividing them. For instance:
int x = 13;
int y = 4;
float x = (float)y / (float)z;
or, if you are using literals:
float x = 13f / 4f;
Keep in mind, floating points are not precise. If you care about precision, use something like the decimal type, instead.
Since you don't use any suffix, the literals 13 and 4 are interpreted as integer:
Manual:
If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.
Thus, since you declare 13 as integer, integer division will be performed:
Manual:
For an operation of the form x / y, binary operator overload resolution is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.
The predefined division operators are listed below. The operators all compute the quotient of x and y.
Integer division:
int operator /(int x, int y);
uint operator /(uint x, uint y);
long operator /(long x, long y);
ulong operator /(ulong x, ulong y);
And so rounding down occurs:
The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands. The result is zero or positive when the two operands have the same sign and zero or negative when the two operands have opposite signs.
If you do the following:
int x = 13f / 4f;
You'll receive a compiler error, since a floating-point division (the / operator of 13f) results in a float, which cannot be cast to int implicitly.
If you want the division to be a floating-point division, you'll have to make the result a float:
float x = 13 / 4;
Notice that you'll still divide integers, which will implicitly be cast to float: the result will be 3.0. To explicitly declare the operands as float, using the f suffix (13f, 4f).
Might be useful:
double a = 5.0/2.0;
Console.WriteLine (a); // 2.5
double b = 5/2;
Console.WriteLine (b); // 2
int c = 5/2;
Console.WriteLine (c); // 2
double d = 5f/2f;
Console.WriteLine (d); // 2.5
It's just a basic operation.
Remember when you learned to divide. In the beginning we solved 9/6 = 1 with remainder 3.
9 / 6 == 1 //true
9 % 6 == 3 // true
The /-operator in combination with the %-operator are used to retrieve those values.
The result will always be of type that has the greater range of the numerator and the denominator. The exceptions are byte and short, which produce int (Int32).
var a = (byte)5 / (byte)2; // 2 (Int32)
var b = (short)5 / (byte)2; // 2 (Int32)
var c = 5 / 2; // 2 (Int32)
var d = 5 / 2U; // 2 (UInt32)
var e = 5L / 2U; // 2 (Int64)
var f = 5L / 2UL; // 2 (UInt64)
var g = 5F / 2UL; // 2.5 (Single/float)
var h = 5F / 2D; // 2.5 (Double)
var i = 5.0 / 2F; // 2.5 (Double)
var j = 5M / 2; // 2.5 (Decimal)
var k = 5M / 2F; // Not allowed
There is no implicit conversion between floating-point types and the decimal type, so division between them is not allowed. You have to explicitly cast and decide which one you want (Decimal has more precision and a smaller range compared to floating-point types).
As a little trick to know what you are obtaining you can use var, so the compiler will tell you the type to expect:
int a = 1;
int b = 2;
var result = a/b;
your compiler will tell you that result would be of type int here.

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

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;