import Foundation
func frexp(_ value: Double) -> (Double, Int) {
guard value != 0 else { return (0.0, 0) }
let exponent = Int(floor(log2(abs(value)))) + 1
let fraction = value / pow(2.0, Double(exponent))
return (fraction, exponent)
}
let d = 3.14
let (fraction, exponent) = frexp(d)
print(String(format: "fraction = %.3f exponent = %d", fraction, exponent))
/*
run:
fraction = 0.785 exponent = 2
*/