LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-02-2003, 07:15 AM   #1
bluesky
LQ Newbie
 
Registered: Feb 2003
Posts: 4

Rep: Reputation: 0
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.
 
Old 09-02-2003, 07:45 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-02-2003, 07:46 AM   #3
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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
 
Old 09-02-2003, 04:57 PM   #4
bluesky
LQ Newbie
 
Registered: Feb 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks alot for your help 3.0 / 7.0 work ^_^

Thanks for your help again
 
Old 09-02-2003, 09:19 PM   #5
LinuxTiro
Member
 
Registered: Aug 2003
Posts: 59

Rep: Reputation: 15
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.
 
Old 09-02-2003, 10:01 PM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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
 
Old 09-02-2003, 11:50 PM   #7
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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
 
Old 09-03-2003, 12:51 AM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting int to float? pirate_pete Programming 5 09-15-2005 02:00 AM
[C++] char * to float Ossar Programming 2 08-03-2005 10:28 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
how big is a float in C? SciYro Programming 11 04-04-2005 09:24 AM
How to use a float in a script. philipina Programming 4 03-18-2004 08:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration