开发者

How to do fixed-point math instead of floating-point?

I'm not the math uncle开发者_StackOverflow中文版, but a real one told me this:

"use fixed point instead of floating point to do multiplication, division, summation, trigonometry and integration"

So instead of using double or float data types, what shall I do? Does anyone have a handy snippet or link that shows the difference for non-math-uncles?


Check the below for link for fixed point, I'm sure will be useful for you ...

http://cnx.org/content/m11054/latest/

http://www.digitalsignallabs.com/fp.pdf


The language itself does not support anything except integers and floating-point values. However, you can easily simulate fix-point values by integers. If you, for example, decide that you want a fix-point value with three decimals, you can use, for example 12345 to represent 12.345. Addition is straight forward, and you can surely work out how to express the other basic operations.

However, there is no support for doing trigonometry etc.


Whoever the "math uncle" is, he's wrong.

Unless you really understand what you're doing, whatever fixed-point implementation you come up with will not be more accurate from using native double-precision and the system math library. If you did really understand what you were doing, you wouldn't be asking the question.

So don't worry about it. Use double. If you run into precision problems, first understand them, and only worry about how to avoid them after you understand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜