LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C newbie float ? (https://www.linuxquestions.org/questions/programming-9/c-newbie-float-88352/)

bluesky 09-02-2003 07:15 AM

C newbie float ?
 
Hi guys

I am a newbie in C programming. When I try the follwoing code, it does not seem right.

#include <stdio.h>

int main (void)
{
float num = 0.0;

num = 3 / 7;
printf("---> %f\n", num);

return 0;
}

Somehow it print num is equal to 0.000000. Could someone tell me what is wrong with it. Thank you for your help.

nephilim 09-02-2003 07:45 AM

I think the problem is the line where you assign num = 3 / 7. Since 3 and 7 are integers, your result will also be an integer (in this case 0) which is than cast to a float (0.0).

Try this:

num = 3.0 / 7.0

and see what the output is.

nephilim 09-02-2003 07:46 AM

In addition to my post above:

You can also cast the integers to float yourself, so this could also work:

num = (float)3 / (float)7

bluesky 09-02-2003 04:57 PM

Thanks alot for your help 3.0 / 7.0 work ^_^

Thanks for your help again

LinuxTiro 09-02-2003 09:19 PM

u can even give
num=(float)3/7;
or
num=3/(float)7;

note:- operation between an int and a real always yields a real.

SaTaN 09-02-2003 10:01 PM

Quote:

Originally posted by HiTleR
u can even give
num=(float)3/7;
or
num=3/(float)7;

note:- operation between an int and a real always yields a real.


Everyone has been repeating the same thing which most of us know ..

But, no one has explained why it is that way .

Maybe someonne would give me an answer

jinksys 09-02-2003 11:50 PM

consider the following code.

float num; <- defines num as a float.
num=1/2; <- in the C world, 1 is different than 1.0 . 1 is an integer, so 1/2 is zero, since 2 doesnt go into 1 at least one time.
Now 1.0/2.0 is 0.5 since 1.0 and 2.0 are floats and therefore the arithmatic 1.0/2.0 will return a float number.

Now consider this:
float num; <- defines float
num=(float)1/(float)2; <- (float) is called a type casting. If you dont know what that is, let me explain. Type casting converts one type into another. So this code:

int num;
float num2;
num=5; <- assigns 5 to num.
num2= (float)num / (float) 5; <- converts num to a float, 5 to 5.0, and then executes the division, which returns a float.

hope that helps

Dark_Helmet 09-03-2003 12:51 AM

First, the same effect as the typecasting above can be easily accomplished by making a minor change to the expression. Instead of "3 / 7" change it to one of the following:

3.0 / 7.0
3.0 / 7
3 / 7.0

or even

3. / 7
3 / 7.

=============================
Explanation

C considers integers (char, int... long or unsigned) to be less "precise" than real values (float, and double). However, integer math is simpler than floating point math. So, unless you specify otherwise, the computer will use integer math, which is what caused 3 / 7 to return 0.

In any expression, C will "upgrade" any data type to prevent data loss if it is necessary. Since both 3 and 7 can be represented as plain integers, that's what C interprets them as. However, if you specify 7.0, the decimal tells C the number is a floating point value, and cannot accurately be represented by an integer. At that point, C is forced to "upgrade" any other numerical values in the expression to have the same level of precision. So, if you had "3 / 7.0", C knows 7.0 is a floating point value, and upgrades 3 (originally considered an integer) to a floating point value as well, performs the division, and returns the floating point result.

Things get complicated when you have operator precedence. For example, suppose you had this:
1.0 + ( 3 / 7 )

That would evaluate to 1.0 because the expression inside the parentheses is evaluated first. An integer divided by an integer gives an integer result (0, in this case). Then, the 1.0 is looked at, causing the 0 to be upgraded to 0.0, then the addition is made, and the result of 1.0 is returned.

Flipping it around:
1 + ( 3 / 7.0 )

gives 1.428... because the expression in the parentheses is evaluated first, returning a floating point, which forces the 1 to upgrade to a 1.0, then the addition, and the floating point result is returned.

The moral of the story is: if you don't want integer math, add the decimal portion to all of your numerical values.


All times are GMT -5. The time now is 02:25 PM.