Swift

let number = *-10*
print(number)
*// Use abs to make number positive.*
let result = **abs**(number)
print(result)
*// An absolute value is always positive.*
let result2 = **abs**(result)
print(result2)-10
10
10

let small = *10*
let big = *2000*
*// Compute max.*
let resultMax = **max**(small, big)
print(resultMax)
*// Compute min.*
let resultMin = **min**(small, big)
print(resultMin)2000
10

import Foundation
*// Compute square root with Foundation.*
let result = **sqrt**(Double(*25*))
print(result)5.0

import Foundation
*// Compute floor for a Double.*
let number1 = *1.1*
let floor1 = **floor**(number1)
print(*"floor \(number1) = \(floor1)"*)
*// Floor removes the fractional part.*
let number2 = *1.9*
let floor2 = **floor**(number2)
print(*"floor \(number2) = \(floor2)"*)floor 1.1 = 1.0
floor 1.9 = 1.0

import Foundation
let number = *0.1*
print(number)
*// Use ceil to remove the fractional part and round up.*
let result = **ceil**(number)
print(result)0.1
1.0

import Foundation
*// Use power of 2 on 3 (square it).*
let number = *3.0*
let result = **pow**(number, 2)
*// Write results.*
print(*"\(number) squared = \(result)"*)3.0 squared = 9.0

import Foundation
*// A negative number has a sign of minus.*
let number = *-1.0*
let result = number.**sign**
print(*"\(number), \(result)"*)
*// Convert Int to Double to use sign.*
*// ... Positive numbers have sign bits of plus.*
let number2 = *200*
let result2 = Double(number2).**sign**
print(*"\(number2), \(result2)"*)-1.0, minus
200, plus

Odd, Even

Fibonacci

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

No updates found for this page.