Float Fractional Precision

后端 未结 2 1275
独厮守ぢ
独厮守ぢ 2021-01-20 00:43

How many places of precision does a float have between 1.0f and 0.0f such that every value could be uniquely represented?

For

2条回答
  •  我在风中等你
    2021-01-20 01:21

    If I understood your question correctly, the answer is 6.

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    int main() {
        int i = 10;  /* Number of distinct number after 1 place of decimal */
        int k = 1;   /* Number of decimal places */
        for(;/* ever */;)
        {
            float prev = 0.0f;
            for(int j = 1; j <= i; ++j)
            {
                stringstream ss;
                ss << "0.";  /* Prepare stream with 0. followed by k digit number with leading zeroes */
                ss << setfill('0') << setw(k) << j;
                float next; /* Read the number */
                ss >> next;
                if(prev == next) return 0;  /* If previous number and current number can not be distinguished */
                prev = next;
            }
            cout << "Works for " << k << " places" << endl;
            i *= 10; /* 10 times more tests to be conducted for 1 more decimal places */
            k++;     /* Try for more decimal places */
        }
        return 0;
    }
    

    What does the code do

    1. Set precision to 1 place after decimal
    2. Compare 0.0 with 0.1, 0.1 with 0.2 .... 0.8 with 0.9 and 0.9
       with 1.0, if any of these are equal (not distinguish), exit.
       Otherwise print Works for 1 place.
    3. Set precision to 2 places after decimal
    4. Compare 0.00 with 0.01, 0.01 with 0.02 .... 0.98 with 0.99 and 0.99
       with 1.00, if any of these are equal (not distinguish), exit. Otherwise
       print Works for 2 places.
    5. Repeat similar steps for 3 and more digits unless you exit
    

提交回复
热议问题