๐ ฟ๏ธBinaryFloatingPoint
Last updated
Last updated
// ๐
ฟ๏ธ BinaryFloatingPoint
protocol BinaryFloatingPoint {
// instance properties
var exponentBitPattern: Self.RawExponent { get }
var significandBitPattern: Self.RawSignificand { get }
// type properties
static var exponentBitCount: Int { get }
static var significandBitCount: Int { get }
}
.binaryBitPattern - include .signBitPattern
.
custom operator: floating +โโจรท int
๐พ ็จๅผ๏ผ replit
// Some code
func test_FloatingPoint<T: BinaryFloatingPoint>(_ x: T) {
// --------------[ ็งๅญธ่จ่ ]------------------
// x = ยฑ (significand) * (radix)^(exponent)
// -------------------------------------------
let t = type(of: x)
log([
x,
// ๐
ฟ๏ธ FloatingPoint API // ex: let x = Double.pi
x.sign, // .plus (enum FloatingPointSign)
x.significand, // 1.57... (Double)
t.radix, // 2 (Int)
x.exponent, // 1 (Int)
// ๐
ฟ๏ธ BinaryFloatingPoint API
x.exponentBitPattern, // 1024 (UInt)
x.significandBitPattern, // 25...44 (UInt64)
t.exponentBitCount, // 11 (Int)
t.significandBitCount, // 52 (Int)
String(x.exponentBitPattern, radix: 2),
// "10000000000"
String(x.significandBitPattern, radix: 2)
// "1001001000011111101101010100010001000010110100011000"
] as! [HasMirrors])
}
// --------------[ ็งๅญธ่จ่ ]------------------
// x = ยฑ (significand) * (radix)^(exponent)
// -------------------------------------------
test_FloatingPoint(Double.pi)
// ๐ 3.141592653589793
// ๐ plus
// ๐ 1.5707963267948966
// ๐ 2
// ๐ 1
// ๐ 1024
// ๐ 2570638124657944
// ๐ 11
// ๐ 52
// ๐ 10000000000
// ๐ 1001001000011111101101010100010001000010110100011000
test_FloatingPoint(Float.pi)
// ๐ 3.1415925
// ๐ plus
// ๐ 1.5707963
// ๐ 2
// ๐ 1
// ๐ 128
// ๐ 4788186
// ๐ 8
// ๐ 23
// ๐ 10000000
// ๐ 10010010000111111011010
Swift โฉ Numbers and Basic Values โฉ Numeric Protocols โฉ BinaryFloatingPoint
inherits from FloatingPoint.
has a custom BinaryBitPatternView.