Created
August 8, 2019 20:23
-
-
Save porglezomp/e73bc3e1911c45f3792fa655ec42a3b7 to your computer and use it in GitHub Desktop.
What does Int << Float mean? :3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Int { | |
subscript(bit bit: Int) -> Int { | |
guard (0..<Int.bitWidth).contains(bit) else { return 0 } | |
return (self >> bit) & 1 | |
} | |
subscript(bit bit: Float) -> Float { | |
var whole = bit | |
whole.round(.down) | |
let fract = bit - whole | |
return Float(self[bit: Int(whole)]) * (1 - fract) | |
+ Float(self[bit: Int(whole) + 1]) * fract | |
} | |
} | |
// My TERRI8LE idea | |
func <<(lhs: Int, rhs: Float) -> [Float] { | |
return (0..<Int.bitWidth).map { lhs[bit: Float($0) - rhs] } | |
} | |
print(1 << 1.3) | |
// [0.0, 0.70000005, 0.29999995, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Erika's idea | |
func <<(lhs: Int, rhs: Double) -> Double { | |
return Double(lhs) * pow(2.0, rhs) | |
} | |
print(1 << 1.3) | |
// 2.4622888266898326 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Int { | |
subscript(bit bit: Int) -> Int { | |
guard (0..<Int.bitWidth).contains(bit) else { return 0 } | |
return (self >> bit) & 1 | |
} | |
subscript(bit bit: Float) -> Float { | |
var whole = bit | |
whole.round(.down) | |
let fract = bit - whole | |
return Float(self[bit: Int(whole)]) * (1 - fract) | |
+ Float(self[bit: Int(whole) + 1]) * fract | |
} | |
} | |
// Erika's good interpretation of what fractional bits means | |
func <<(lhs: Int, rhs: Float) -> Float { | |
return (0..<Int.bitWidth).map { Float(1 << $0) * lhs[bit: Float($0) - rhs] }.reduce(0.0, +) | |
} | |
print(1 << 1.3) | |
// 2.6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment