This question already has an answer here:
Modulo operator in Objective C
(1 answer)
Closed 8 years ago.
I want to check if a number is divisible by 6 and if not, I need to increase it until it becomes divisible.
Use the modulus operator. This operator returns the remainder of the division operation. Check if this is 0 to check if it divides perfectly.
if (i % 6 == 0) {
// ...
}
Related
This question already has answers here:
how to swap numbers in kotlin using function?
(2 answers)
Val cannot be reassigned a compile time error for a local variable in fun in Kotlin
(3 answers)
Closed 5 months ago.
I am a beginner, learning the basics of Kotlin. We were asked to swap the values of two variables (see the photo below). My question is, why is simply swapping the values of the variable not the expected solution as written in the image? It had to use a third variable.
Example:
Var x = 1
Var y = 2
println(x) // prints 1
println(y) // prints 2
x = 2
y = 1
println(x)
println(y)
Also, in the photo, was the Val tmp reassigned?
This will swap two integer:
x=x+y
y=x-y
x=x-y
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.
This question already has answers here:
bit-shifting by an integer value
(2 answers)
Closed 7 years ago.
if I declare a variable of type uint64_t and assign it the value 1 << 34 i.e. shift the value left by 34 bits the result variable is set to 0. Any shift > 31 gives 0 and shifts < 31 are work just fine.
My assignment statement is:
uint64_t test34B = (uint64_t)1 << 34;
Is there an Xcode compiler setting I'm missing or something?
On a 32-bit platform, the literal 1 will be 32-bit, and hence the compiler error.
Instead use a literal explicitly sized to 64-bits using the ull suffix:
uint64_t n = 1ull << 34;
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...).
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);