Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@porglezomp
Created August 8, 2019 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porglezomp/e73bc3e1911c45f3792fa655ec42a3b7 to your computer and use it in GitHub Desktop.
Save porglezomp/e73bc3e1911c45f3792fa655ec42a3b7 to your computer and use it in GitHub Desktop.
What does Int << Float mean? :3
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]
import Foundation
// Erika's idea
func <<(lhs: Int, rhs: Double) -> Double {
return Double(lhs) * pow(2.0, rhs)
}
print(1 << 1.3)
// 2.4622888266898326
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