c++ incorrect floating point arithmetic

前端 未结 3 1049
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 13:07

For the following program:

#include 
#include 
using namespace std;
int main()
{
    for (float a = 1.0; a < 10; a++)
              


        
3条回答
  •  猫巷女王i
    2020-12-06 13:34

    Floats only have so much precision (23 bits worth to be precise). If you REALLY want to see "0.333333333333333333333333333333" output, you could create a custom "Fraction" class which stores the numerator and denominator separately. Then you could calculate the digit at any given point with complete accuracy.

提交回复
热议问题