Why would you use float over double, or double over long double?

后端 未结 5 1185
我寻月下人不归
我寻月下人不归 2021-01-01 13:35

I\'m still a beginner at programming and I always have more questions than our book or internet searches can answer (unless I missed something). So I apologize in advance if

5条回答
  •  孤城傲影
    2021-01-01 14:02

    The basic principle here would be don't use more than you need.

    The first consideration is memory use, you probably realized that already, if you are making only one double no big deal, but what if you create a billion than you just used twice as much memory space as you had too.

    Next is processor utilization, I believe on many processors if you use smaller data types it can do a form of threading where it does multiple operations at once.

    So an extension to this part of the answer is SSE instructions basically this allows you to used packed data to do multiple floating point operations at once, which in an idealized case can double the speed of your program.

    Lastly is readability, when someone is reading your code if you use a float they will immediately realize that you are not going over a certain number. IMO sometimes the right precision number will just flow better in the code.

提交回复
热议问题