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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-02-2003, 07:15 AM
|
#1
|
|
LQ Newbie
Registered: Feb 2003
Posts: 4
Rep:
|
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.
|
|
|
|
09-02-2003, 07:45 AM
|
#2
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
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.
|
|
|
|
09-02-2003, 07:46 AM
|
#3
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
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
|
|
|
|
09-02-2003, 04:57 PM
|
#4
|
|
LQ Newbie
Registered: Feb 2003
Posts: 4
Original Poster
Rep:
|
Thanks alot for your help 3.0 / 7.0 work ^_^
Thanks for your help again
|
|
|
|
09-02-2003, 09:19 PM
|
#5
|
|
Member
Registered: Aug 2003
Posts: 59
Rep:
|
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.
|
|
|
|
09-02-2003, 10:01 PM
|
#6
|
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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
|
|
|
|
09-02-2003, 11:50 PM
|
#7
|
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417
Rep:
|
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
|
|
|
|
09-03-2003, 12:51 AM
|
#8
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:07 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|