Skip to content

Instantly share code, notes, and snippets.

@taylorza
Last active November 1, 2021 03:12
Show Gist options
  • Save taylorza/7a0905e4a83ed9dae1c88ff464e30968 to your computer and use it in GitHub Desktop.
Save taylorza/7a0905e4a83ed9dae1c88ff464e30968 to your computer and use it in GitHub Desktop.
Example of fixed point
type fixed32 int32
func fromFloat32(f float32) fixed32 {
return fixed32((f * float32(1<<16)) + 0.5)
}
func (f fixed32) Float32() float32 {
return float32(f) / float32(1<<16)
}
func (f fixed32) Add(o fixed32) fixed32 {
return f + o
}
func (f fixed32) Sub(o fixed32) fixed32 {
return f - o
}
func (f fixed32) Mul(o fixed32) fixed32 {
return fixed32((int64(f) * int64(o)) >> 16)
}
func (f fixed32) Div(o fixed32) fixed32 {
return fixed32((int64(f) << 16) / int64(o))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment