Swift: "float is not convertible to int" - swift

How can I convert this to Int? I've tried using the initializer: Int(), using the round method.. All of them generates an error. In this case the one I get is: "float is not convertible to int"
let CirclePoints = 84
let PI = 3.14159
let radius: Double!
let xBase: Int!
let yBase: Int!
var xPos = Int()
var yPos = Int()
xPos = round(xBase + radius * cos((PI / 10) * circlePoint))

I'd recommend converting all of your values to double since that is what the round function takes. The round function also returns a double, so you'll have to convert the result of round() into an Int to store it in xPos.
xPos = Int(round(Double(xBase) + Double(radius) * cos((PI / 10) * Double(circlePoint))))
Note that the conversion process here is actually creating new double values from the variables, not changing those variables.

Related

How to get the power of a Double with another Double in Swift 5.5?

So long story short I need to get x to the power of y which are both doubles
Problem is I keep getting 0
import Darwin
x: Double = 3.86
y: Double = 4.86
var Answer = Int(pow(Double(x),Double(y)))
// Answer = 0?
// Answer Should = 709.2744...
So why is this the case and how do I make this actually work?
and yes this calculation works fine with Integers just the second Doubles get added to the mix it just all falls apart
var x: Double = 3.86
var y: Double = 4.86
var Answer = Double(pow(x, y))
print(Answer)

Odd division result in swift [duplicate]

I'm trying to make a math app with different equations and formulas but I'm trying to circle sector but i just wanted to try to divide the input value by 360 but when I do that it only says 0 unless the value is over 360. I have tried using String, Double and Float with no luck I don't know what I'm doing is wrong but down here is the code. I'm thankful for help but I have been sitting a while and searched online for an answer with no result I might have been searching with the wrong search.
if graderna.text == ""{
}
else{
var myInt: Int? = Int(graderna.text!) // conversion of string to Int
var myInt2: Int? = Int(radien.text!)
let pi = 3.1415926
let lutning = 360
let result = (Double(myInt! / lutning) * Double(pi))
svar2.text = "\(result)"
}
Your code is performing integer division, taking the integer result and converting it to a double. Instead, you want to convert these individual integers to doubles and then do the division. So, instead of
let result = (Double(myInt! / lutning) * Double(pi))
You should
let result = Double(myInt!) / Double(lutning) * Double(pi)
Note, Double already has a .pi constant, so you can remove your pi constant, and simplify the above to:
let result = Double(myInt!) / Double(lutning) * .pi
Personally, I’d define myInt and lutning to be Double from the get go (and, while we’re at it, remove all of the forced unwrapping (with the !) of the optionals):
guard
let text = graderna.text,
let text2 = radien.text,
let value = Double(text),
let value2 = Double(text2)
else {
return
}
let lutning: Double = 360
let result = value / lutning * .pi
Or, you can use flatMap to safely unwrap those optional strings:
guard
let value = graderna.text.flatMap({ Double($0) }),
let value2 = radien.text.flatMap({ Double($0) })
else {
return
}
let lutning: Double = 360
let result = value / lutning * .pi
(By the way, if you’re converting between radians and degrees, it should be 2π/360, not π/360.)
You are dividing an Int by an Int.
Integer division rounds to the nearest integer towards zero. Therefore for example 359 / 360 is not a number close to 1, it is 0. 360 / 360 up to 719 / 360 equals 1. 720 / 360 to 1079 / 360 equals 2, and so on.
But your use of optionals is atrocious. I'd write
let myInt = Int(graderna.text!)
let myInt2 = Int(radien.text!)
if let realInt = myInt, realInt2 = myInt2 {
let pi = 3.1415926
let lutning = 360.0
let result = Double (realInt) * (pi / lutning)
svar2.text = "\(result)"
}
In the line let result = (Double(myInt! / lutning) * Double(pi)) you cast your type to double after dividing two integers so your result will always be zero. You have to make them doubles before division.
let result = (Double(myInt!) / Double(lutning)) * Double(pi))
If you want the value should be correct, then try as
let division = ((Float(V1) / Float(V2)) * Float(pi))

Rounding INT to make it 0 decimal places

I am running the following code with a round function.
let percentage = round((progress / maxValue) * 100)
However it keeps returning numbers like: 15.0, 25.0, 35.0 etc.
I want it to return: 15, 25, 35, basically 0 decimal places.
How can I do this?
Cheers! :D
That's because round() returns a floating point number, not an integer:
If you want an integer, you have to convert it:
let percentage = Int(round((progress / maxValue) * 100))
Cast it to an Int:
let percentage = round((progress / maxValue) * 100)
let percentageInt = Int(percentage)
round() returns a floating point number. You can convert the result
to an Int, or call lrint instead:
let percentage = lrint((progress / maxValue) * 100)
The functions
public func lrintf(_: Float) -> Int
public func lrint(_: Double) -> Int
return the integral value nearest to their argument as an integer.

Binary operator '..<' cannot be applied to two 'Int' operands [duplicate]

I am having the same problem as earlier with a different line of code; but this time, I wasn't able to fix it with the same approach as last time:
var Y : Int = 0
var X : Int = 0
#IBOutlet var ball : UIImageView!
ball.center = CGPointMake(ball.center.x + X, ball.center.y + Y)
This is the error I am getting:
binary operator + cannot be applied to operands of type CGfloat int
Declare them, instead, as the following:
let X : CGFloat = 0.0
let Y : CGFloat = 0.0
Replying to your comment:
The error has nothing to do with them being declared as var or let.
You could declare them as var and if you so insist on declaring them as Int, you would still need to do the following:
var X : Int = 0
var Y : Int = 0
ball.center = CGPointMake(view.center.x + CGFloat(X), view.center.y + CGFloat(Y))
The problem is that you are not having the same variable types, for example you can't add bool and string.
Change it to CGFloat instead of int:
let X : CGFloat = 0.0
let Y : CGFloat = 0.0
ball.center.x is a CGFloat and X is an Int. That's where the compiler is complaining.
Swift likes you to type cast numeric types (as if there wasn't a hierarchy in numeric domains) but you can avoid that by declaring X and Y as CGFloat instead of Int.
You could also get rid of the issue for good by defining the operator (that Swift should already have imho):
infix operator + {}
func +(left:CGFloat, right:Int) -> CGFloat
{ return left + CGFloat(right) }
func +(left:Int, right:CGFloat) -> CGFloat
{ return CGFloat(left) + right }

iOS Swift: Create Float value from two Ints

What's the best way to create a single Float value from two Ints? I have two vars:
let int1 = 165
let int2 = 5
I'm trying to combine them into a Float with the value 165.5.
Float to Int
Two approaches.
You can concatenate them into a String and pass that to a Float initializer:
let float1 = Float("\(int1).\(int2)")
Or you can divide int2 by 10 and add int1:
let float2 = Float(int1) + Float(int2)/10
Int to Float
If you want to go back, you can again use strings:
let float : Float = 165.5
let intArray = String(float)
.characters
.split(".")
.map { Int(String($0))! }
intArray[0] // 165
intArray[1] // 5
But it's simpler to use math:
let (int1, int2) = (Int(float), (float - floor(float)) * 10)