Calculate simple math equation [duplicate] - swift

This question already has answers here:
How to round a Double to the nearest Int in swift?
(9 answers)
Closed 4 years ago.
I try to use swift code to calculate 10 * 75% = 7.5
var b : Int = 10
var a : Int = 75
// result x is 0
var x = b * (a / 100)
However the result is zero. How to get 7.5 result without changing the type and value of a and b?
UPDATE:
I got it right by:
var x: Double = (Double(b) * (Double(a) / 100)) // x is 7.5
Now, how can I round it to 8 as a Int type?

You're using integer (i.e. whole number) arithmetic, when what you want is floating-point arithmetic. Change one of the types to Float, and Swift will figure out that x is also a Float.
var b : Int = 10
var a : Int = 75
var x = Float(b) * (Float(a) / 100.0) // now x is a Float (.75)

For doing arithmetic in Swift, both sides of the equation must be using the same type. Try using this code:
let b = 10.0
let a = 75.0
let x = b * (a / 100.0
print(x)
7.5
To make it round up, use the built in ceil function
print(ceil(x))
8.0

Ok then you just need to do:
var b : Int = 10
let c = (Double(b) * 0.75)
let restult = ceil(c)

Related

Generate a random number between negative and positive in swift [duplicate]

This question already has an answer here:
How to generate a random number in a range (10...20) using Swift [duplicate]
(1 answer)
Closed 4 years ago.
So I'm trying to generate a random number between negative and positive range because I need to give an object a position on the x axis. I've tried with arc4random and arc4random_uniform but it doesn't work. I need a way to generate a random position on the x axis, negative and positive.
I don't think you can generate negative random number using arc4random directly
let lValue = -10 // (your negative number)
let uValue = 10 //(your positive number)
let result = Int(arc4random_uniform(UInt32(uValue - lValue + 1))) + lValue
print(result)
hope this will work
let min : UInt32 = 10
let max : UInt32 = 50
let randomNumber = arc4random_uniform(max - min) + min
if arc4random_uniform(2) == 0 {
randomNumber = randomNumber * -1
}

Computing the result of an integral in Swift

I need to create a script that will calculate the overlap orbital of two 1s orbitals. The integral is given by
I tried calculating this using code but my answer is nowhere near the analytic result of S=(1+R+R^2/3)exp(-R). Could someone help me figure where I went wrong?
The code:
import Foundation
var sum: Double = 0.0 //The integral result
var step_size: Double = 0.0000025
var a: Double = 0.0
var R: Double = 5.0
var next_point: Double = 0.0
var midpoint: Double = 0.0
var height: Double = 0.0
var r_val: Double = 0.0
func psi_func(r_val: Double) -> Double {
return exp(-r_val)
}
//Integration
while next_point < R {
next_point = a + step_size
midpoint = a + step_size/2
height = psi_func(r_val: midpoint)
sum += psi_func(r_val: midpoint)*step_size
a = a + step_size
}
print("S = ", 2*3.14159*3.14159*sum) // This is a 3-D orbital, so I multiply by 2*pi*pi
For R = 5.0
My answer: 19.61
Analytic answer: 0.097
Two problems I can see:
I see a single wavefunction and not the product of two in your code
It is incorrect to just do a 1d integral and multiply by 2pi^2 in the end
Try doing a proper 3d integral with the correct integrand.

Function doesn't work on iPhone 5 with arc4random [duplicate]

This question already has answers here:
Crash when casting the result of arc4random() to Int
(7 answers)
Closed 7 years ago.
I have a problem with one of my function: my application run perfectly on iPhone 5s, 6, etc, but on a iPhone 5 I have a problem with my code. When my function is called, I always have this problem:
switch choixDeCote {
case 1 : //Haut
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 200
SpawnX = UInt32(MaxValue - MinValue)
SpawnX = arc4random_uniform(SpawnX)
SpawnY = UInt32(self.size.height)
directionX = Int(arc4random()) % Int(self.frame.size.width)
print(directionX)
directionY = 0
action = SKAction.moveTo(CGPoint(x: CGFloat(directionX),y: CGFloat(directionY)),duration: 4)
break
And Xcode says that directionX = Int(arc4random()) % Int(self.frame.size.width) has a problem, but I don't know which one.
arc4random() returns a UInt32. On a 32-bit machine like the iPhone 5, this can overflow an Int.
I would suggest instead:
directionX = Int(Int32(bitPattern: arc4random_uniform(UInt32(self.frame.size.width))))
the Int32(bitPattern:) is only a precaution and isn't really necessary since the self.frame.size.width is much smaller than MAXINT32 so the random number generated won't overflow an Int. You can just do:
directionX = Int(arc4random_uniform(UInt32(self.frame.size.width)))

Round down in swift

I would like to round a Double to a certain number of decimals always rounding down.
Example
rounding to .00 => 1.5679999 to 1.56
rounding to .000 => 1.5679999 to 1.567
round(1.5679999 * 100) / 100 //returns 1.57
round(1.5679999 * 1000) / 1000 //returns 1.568
I want a Double not a concatenated String
Just replace round with floor!
As of swift 3.0:
myDouble.rounded(.down)
SWIFT4+
ways to round on different cases
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0

Display Float In UILabel

I am having an issue with using a float in a UILabel.
float doubleNum;
floatNum = 10 / 20;
cashLabel.text = [[NSString alloc] initWithFormat:#"%f", floatNum];
If I use "floatNum = 10 / 10;" it correctly returns "1.000000000", however, if I put in "floatNum = 10 / 20" it returns "0.0000000". I have tried about everything I know and it does not work. I know it's a dumb mistake, but I can't figure it out.
Happy Holidays. :)
You need to cast one of the integer's to a float.
Try replacing the divisional line with:
float floatNum = (float) 10 / 20;
and you should get the correct answer.
Or if possible just use floats in your division:
float floatNum = 10.0f / 20.0f;
should also work
The issue here is that you are assigning floatNum the result of dividing one INTEGER by another. The result of 10 / 20 is indeed 0 and as a float, it appears as 0.0000000. In order to obtain a proper result, you need to either use a cast type to turn it into a float or add a .0 to one of the numbers. In division, if one of the numbers is a float (which is easily done by just adding a .0 to one of them), the result will be a float as well.
Normally, C performs "integer division" (basically, division without the remainder -- 10/3 is 3R1, so it yields 3).
When you type floatNum = 10/20, it does 10/20 = 0 (remainder 10).
To fix this, you have to tell the program that you're giving it floating point numbers. Try using:
floatNum = 10.0 / 20,
floatNum = 10 / 20.0, or
floatNum = float (10 / 20).
All of those should work.
try floatNum = 10.0f/20.0f (i.e. make sure the calculation is being done with floats rather than ints)
Consider following example to understand how floats work:
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.
Change your code to:
float floatNum;
floatNum = 10.0f / 20.0f;
cashLabel.text = [[NSString alloc] initWithFormat:#"%f", floatNum];