By Kevin


2016-02-13 17:35:53 8 Comments

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)"
        }

3 comments

@Rob 2016-02-13 17:43:00

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

@gnasher729 2016-02-13 17:42:58

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)"
}

@Kevin 2016-02-13 18:43:53

Thanks for your timme and help aswell @gnasher729

@Cole 2016-02-13 17:42:27

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

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] How do I call Objective-C code from Swift?

  • 2014-06-02 20:05:42
  • David Mulder
  • 289293 View
  • 980 Score
  • 17 Answer
  • Tags:   objective-c swift

23 Answered Questions

[SOLVED] @selector() in Swift?

29 Answered Questions

[SOLVED] Is floating point math broken?

28 Answered Questions

[SOLVED] How do I make an attributed string using Swift?

20 Answered Questions

[SOLVED] #pragma mark in Swift?

  • 2014-06-03 14:05:56
  • Arbitur
  • 223668 View
  • 940 Score
  • 20 Answer
  • Tags:   swift documentation

15 Answered Questions

[SOLVED] How to iterate a loop with index and element in Swift

9 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

16 Answered Questions

[SOLVED] #ifdef replacement in the Swift language

1 Answered Questions

[SOLVED] Correcting floating math broken by multiplying, is it ok?

  • 2014-05-07 16:16:43
  • Prosto Trader
  • 113 View
  • 3 Score
  • 1 Answer
  • Tags:   javascript math

Sponsored Content