-
Re: Var declarations
QuinceyMorris May 22, 2017 8:16 PM (in response to jdavidg9)The problem is a practical one, that there's no widespread agreement about what the % operator should do. It calculates a remainer, or a modulus, and it produces a positive result, or a negative result, or a result with the same sign as … whatever, it rounds, it truncates, towards the origin, towards positive infinity, towards negative infinity.
For those reasons, the designers of Swift decided to disallow the use of operator % with floating point numbers. Instead, you use one of the "remainder" functions:
developer.apple.com/reference/swift/floatingpoint
to say what you mean.
-
Re: Var declarations
jorisvm May 23, 2017 6:13 AM (in response to jdavidg9)Hi,
this might help:
var nNum3:Float = 7.0 var nNum4:Float = 4.0 var nREnt = nNum3.truncatingRemainder(dividingBy: nNum4) print("Float - Reaminder after division:\(nREnt), type: \(type(of:nREnt))") let nNum1:Int = 7 let nNum2:Int = 4 let nRest = 7 % 4 print("Int - Reaminder after division:\(nRest), type: \(type(of:nRest))")
Console output:
Float - Reaminder after division:3.0, type: Float
Int - Reaminder after division:3, type: Int
Have fun,
Joris
-
Re: Var declarations
mamun May 24, 2017 8:53 AM (in response to jorisvm)Solved. Working Fine...
var nNum3:Float = 5.2
var nNum4:Float = 1.0
var nREnt : Int = Int(nNum3.divided(by: nNum4))
var nRes1 : Float = nNum3.remainder(dividingBy: nNum4)
-
-
Re: Var declarations
John368 May 28, 2017 1:24 AM (in response to jdavidg9)Remainder Operator % is used only for values of type Int
If you write
var nRes1 : Int = nNum3 % nNum4
then there will be no error