[sdiy] Best book /resource for fixed point math??

John Ames commodorejohn at gmail.com
Thu Sep 12 19:40:59 CEST 2013


On 9/12/13, Dan Snazelle <subjectivity at hotmail.com> wrote:
> In a few different wavetable synth programs (for avr) that I have been
> studying, I keep finding references to  fixed point math
>
> Variables like
>
> Q16n16
> Q0n31
> Q1n15 etc
> And functions for seperating the "fractional part" of a number, etc.
>
> Im wondering which book , video, etc
> Might be best for really coming to terms with understanding fixed
> point/floating point so that I can understand programs and so my own
> programs can improve.
Fixed-point math is really pretty simple conceptually. You just use
integers of specific sizes to hold the whole and fractional parts of a
number - as Charlie says, it's like separating "12.34" into separate
variables with the values "12" and "34." Q16n16, for example, would
use 16-bit integers for both, so you'd have a whole number of -32768
to 32767 (if it's signed, 0 to 65535 otherwise) and then a fractional
component measured in increments of 1/65536. Q1n15 would treat the
value as a 1-bit whole plus a 15-bit fractional. Basically the bits in
a convenient-sized machine word get divvied up to derive whatever
balance of numeric range and fractional precision suits the purpose
best.

Actually working with fixed-point math can be a bit more complex at
points - adding and subtracting variables of the same type is
completely identical to normal integer math, but mixing types (adding
a Q16n16 to a Q0n31, say) involves shifting both into the correct
position within a variable type large enough to contain both, doing
the operation, and then deciding what to do with any extra bits to fit
the result into the return type. And multiplication/division is a bit
trickier. Still, once you wrap your head around it it's surprisingly
neat, and much faster than floating-point math.

A good place to start would be with this article:
http://x86asm.net/articles/fixed-point-arithmetic-and-tricks/
but Wikipedia's article is pretty solid too:
http://en.wikipedia.org/wiki/Fixed-point_arithmetic



More information about the Synth-diy mailing list