float variables in C [duplicate]

偶尔善良 提交于 2019-12-31 04:15:08

问题


May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior.

#include<stdio.h>

int main ()
{
    int a = 9/5;
    printf("%f\n", a);
    return 0;
}

Output:

0.000000

I have looked at some information on how float variables are stored in memory, it has stuff about mantissa, exponent and sign. But I am not getting how to relate that here.


回答1:


int a = 9/5;

performs integer division and ignores the remainder, so a is set to 1. Attempting to print that using %f gives undefined behavior, but by chance you got 0.000000 out of it.

Do

double a = 9./5.;

instead, or print with %d if integer division was the desired behavior. (float would also work, but a will be promoted to double when passed to printf, so there's no reason not to use double.)




回答2:


It is an Undefined Behaviour.

You are using float format specifier (%f) to print an int (a). You should use %d to see correct output.




回答3:


It is an undefined behaviour in C. Use %d format specifier instead of %f.

Does printf() depend on order of format specifiers? gives you detailed answer.




回答4:


Here's a brief analysis of your code:

int a = 9/5; // 9/5 = 1.8, but since you are doing integer division and storing the value in an integer it will store 1.
printf("%f\n", a);//Using incorrect format specifiers with respect to datatypes, will cause undefined behavior

printf("%d\n",a);//This should print 1. And correct.

Or if you want the float:

instead of int use float :

   float a=9.0f/5;//This will store 1.800000f
   //float a=9/5 will store 1.000000 not, 1.8 because of integer divison 
   printf("%f\n",a); //This will print 1.800000

Also do read this: http://en.wikipedia.org/wiki/IEEE_754-2008 on how floating points work.

Clarification about integer division:

C99: 6.5.5 Multiplicative operators

6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a

88) This is often called ‘‘truncation toward zero’’.




回答5:


Just assuming that your division should result in a fraction in normal math won't magically make it a float.

int a = 9/5;

You need to use

float a = 9.0/5;

First you need the right data type i.e. float (or better yet double for higher precision) to store a float.

Secondly, if you are dividing two integers i.e. 9 and 5, it will simply follow integer division i.e. only store the integer part of division and discard the rest. To avoid that i have added .0 after 9. This would force compiler to implicitly covert into float and do the division.

Regarding your mentioning of why it is printing 0, it is already mentioned that trying %f on integer is undefined behavior. Technically, a float is 4 bytes containing 3 byte number and 1 byte exponent and these are combined to generate the resultant value.



来源:https://stackoverflow.com/questions/13066075/float-variables-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!