When to use a Float

前端 未结 7 1013
南笙
南笙 2021-02-03 18:40

Years ago I learned the hard way about precision problems with floats so I quit using them. However, I still run into code using floats and it make me cringe because I know som

相关标签:
7条回答
  • 2021-02-03 19:23

    Use float for performance and size. If you can manage the precision loss.

    While it is true that a modern processor takes the same amount of time to process single and double precision opertions, you can sometimes get twice the throughput if you use floats with SIMD (MMX/SSE/etc. on x86) instructions.

    SSE registers are 128 bits wide, and can hold 4 floats or 2 doubles. Thus, if used correctly, you can do twice as many operations with floats as compared to doubles.

    The size reduction (4 bytes instead of 8) becomes important when dealing with very large data sets (and size reduction usually improves performance as well due to caching, etc.)

    0 讨论(0)
提交回复
热议问题