0

I am looking for a way to convert a fractional decimal to binary fraction in Swift 3. But unfortunately Xcode shows error message

"Cannot assign value of type 'Double' to type 'Int'" // line 8

"Binary operator '*' cannot be applied to operands of type 'Double' // line 9

and

'Int'" and "Cannot assign value of type 'Int' to type 'Double'" // line 11

Code itself:

var fraDecimal: Double, fraBinary: Double, bFractional: Double = 0.0, dFractional: Double = 0.6817, fraFactor: Double = 0.1
var dIntegral: Int, bIntegral: Int = 0
var intFactor: Int = 1, remainder: Int, temp: Int, i: Int
var signs: Int = 6

while (signs > 0 && dFractional > 0){
    dFractional = dFractional * 2
    temp =  dFractional
    bFractional = bFractional + fraFactor * temp
    if(temp == 1) {
        dFractional = Int(dFractional) - temp
    }
    fraFactor=fraFactor/10
    signs-=1
}

Maybe there's some kind of workaround?

Thank you for your attention to my request

rmaddy
  • 314,917
  • 42
  • 532
  • 579
DanielD
  • 87
  • 1
  • 5
  • Hint: Swift numeric types are **not** interchangeable (the error in line 9 is misleading though) – vadian Oct 20 '17 at 19:29
  • Not a direct answer to your question, but here is a (more efficient) method to approximate a floating point number by a fraction: [Decimal to Fraction conversion in Swift](https://stackoverflow.com/questions/35895154/decimal-to-fraction-conversion-in-swift). – Martin R Oct 20 '17 at 19:31

0 Answers0