Extra precision values seen for float values

后端 未结 4 1726
旧巷少年郎
旧巷少年郎 2021-01-26 13:38

Below is the solution code for an excercise from KN King\'s Modern Programming approach.

#include

int main(void)
{
    int i;
    float j,x;
             


        
4条回答
  •  无人共我
    2021-01-26 14:21

    Why are there extra decimal values seen for floats?

    That's why floats are floats.By default %f lets you display 6 digits after decimal. That's nothing do do with the precision of the number stored.Numbers are stored according to the IEEE 754 FLOATING POINT representation in most systems.And numbers are not stored with decimal points. So, since you do not control how you store numbers, you can't change the precision.Precision is fixed.

    Is there any default precision set for float and how can I change it.

    Again , precision is not default its not how it is stored. Check its representation for 32 bit or 64 bit.Youwould like to print more/less digits after decimal.You can do this :

     float i=2.89674534423;
     printf("%.10f",i); Prints 10 digits after decimal.
    

提交回复
热议问题