Skip to content

Instantly share code, notes, and snippets.

@tanner0101
Last active October 30, 2015 23:20
Show Gist options
  • Save tanner0101/24030969dacc6d9b4885 to your computer and use it in GitHub Desktop.
Save tanner0101/24030969dacc6d9b4885 to your computer and use it in GitHub Desktop.
Converts 16-bit 8:8 Fixed Point numbers to swift Doubles.
/**
Converts 16-bit 8:8 Fixed Point numbers to swift Doubles.
Fixed Point mathematical functions:
<https://courses.cit.cornell.edu/ee476/Math/>
Compare the results from these calls to the Examples from the "Fixed Point mathematical functions" paper.
Double.from88FixedPoint(0, 0)
Double.from88FixedPoint(1, 0)
Double.from88FixedPoint(1, 128)
Double.from88FixedPoint(1, 192)
Double.from88FixedPoint(1, 1)
Double.from88FixedPoint(255, 0)
Double.from88FixedPoint(254, 128)
Double.from88FixedPoint(254, 0)
Double.from88FixedPoint(129, 0)
Double.from88FixedPoint(255, 128)
Double.from88FixedPoint(255, 192)
Double.from88FixedPoint(0, 128)
Double.from88FixedPoint(128, 0)
Double.from88FixedPoint(127, 0)
Double.from88FixedPoint(2, 64)
Double.from88FixedPoint(253, 192)
<https://gist.github.com/tannernelson/24030969dacc6d9b4885>
*/
extension Double {
static func from88FixedPoint(byte1: UInt8, _ byte2: UInt8) -> Double {
var double: Double = 0
var integer = Double(byte1)
var fraction = Double(byte2)
if integer >= 256 / 2 {
integer -= 256
}
double += Double(integer)
double += (1 / 256) * Double(fraction)
return double
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment