ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

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).

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.

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.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.