Last active
November 1, 2021 03:12
-
-
Save taylorza/7a0905e4a83ed9dae1c88ff464e30968 to your computer and use it in GitHub Desktop.
Example of fixed point
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
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