472,124 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,124 software developers and data experts.

float/double/long double range?

the following program does not give any output supposedly becoz a double const is being assigned to a float variable....i dont quiet understand by what is meant by this...the program goes like this....
void main( )
float x=1.1;
printf("\n %f",x);
getch( );

why isnt the program giving any output?
Aug 14 '08 #1
9 17257
1,275 Expert 1GB
You cant compare float variable like this.
You can check this way
Expand|Select|Wrap|Line Numbers
  1. if(x-1.11 < 0.01)
  2. {
  3. //matched
  4. }
I think this is told by one of the experts here(Banfa/Josah)
Serach in the forum for this

Aug 14 '08 #2
9,065 Expert Mod 8TB
Are you sure that the the reason that there is no output isn't in fact because you invoked undefined behaviour by declaring main as returning void?

main returns int, always, without exception. To do otherwise invokes undefined behaviour.

Undefined behaviour is exactly what it says, once invoked in anyway the behaviour of the program is completely undefined. The program may do anything including formatting your hard disk, although results are normally less drastic but often still a disaster for the program. You should always avoid invoking undefined behaviour.

OK onto your observed behaviour, the statement
the following program does not give any output supposedly becoz a double const is being assigned to a float variable
is wrong. This is not the reason that the program does not give any output.

You are getting warnings about assigning a double constant to a float variable because that is what you are doing on lines 3 and 7 of your program.

C has 2 floating point types float and double these have different precisions, often 7 and 15 significant digits, and are held in a different number of bytes, often 4 and 8 bytes respectively.

That means that when you assign a double (variable or constant) to a float variable the compiler has to truncate the value, that is convert it to the new format and you loose precision.
are all examples of double constants, if you want a float constant you have to
tack an f on the end
If you do this for all double constants in your program then it will apparently fix the behaviour and you will get output (probably).

However there is another problem. You are using a float variable (x) as a control variable, that is you are using it in an if, while or for condition.

This is very poor practice because as i mentioned earlier a float holds 7 significant digits (and a double 15). But they can hold a vast range of values from very large to very small. The reason they can do this is that they actually only hold approximations to the number in question.

Because the value held is an approximation trying to test it against a specific value is almost bound to fail at sometime.

You should absolutely never use the comparisons == and != with float and double variables and you should only use <, <=, > and >= with very great care and in the knowledge that it is possible to get cases that when worked out on paper produce one result but that the computer will come up with the inverse.
Aug 14 '08 #3
Thank u for clarifying my doubts.....
Aug 15 '08 #4
I had the misconception that all decimal point numbers are floating point numbers...but thereafter i came across double constants....
by d ways i looked up the range of both ie
float- 3.4e-38 to 3.4e38
double- 1.7e-308 to 1.7e308
altough these are in the exponential form....
can u please cite some examples of floating point numbers in the fractional form and bring abt the pts of diff btw floats and double in the fractional form.............what will be the range of both in the fractional form?why is 1.1,3.14 double constants and not floats?
and yes if i use 1.1f then i do get an output....
Aug 15 '08 #5
9,065 Expert Mod 8TB
1.1 and 3.14 are double constants and not floatconstants because that is what the standard says.

Floating point literals are of type double unless you specifically give them type float by appending an f e.g. 1.1f, 3.14f

Like I said float holds 7 significant digits and double 15 so

double dv = 1.0000000001;

would work as expected but

float fv = 1.0000000001f;

would not, you would most likely get a compiler warning or error but if you didn't then fv would end up with a value of 1.
Aug 15 '08 #6
are all decimal point numbers by default stored as double constants unless u append f wid dem?...can u cite some examples of floats.....
am sorry am jus a beginner in c programming language,would u please suggest a site where i can read more into this....ie float-double interconversions?
Aug 15 '08 #7
11,448 Expert 8TB
Also read "What every computer scientist should know about floating-point arithmetic".

kind regards,

Aug 15 '08 #8
687 512MB
are all decimal point numbers by default stored as double constants unless u append f wid dem?...can u cite some examples of floats.....
You can't append u to float number, compiler will not allow unsigned float. I'd suggest reading ieee 754.
Aug 15 '08 #9
9,065 Expert Mod 8TB
As I already said floating point literals are of type double unless you append an f in which case they are of type float.
Aug 15 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by JKop | last post: by
11 posts views Thread by Christian Christmann | last post: by
14 posts views Thread by ziller | last post: by
9 posts views Thread by FalkoG | last post: by
2 posts views Thread by sriamar | last post: by
8 posts views Thread by abdul_n_khan | last post: by
60 posts views Thread by Erick-> | last post: by
116 posts views Thread by Dilip | last post: by
45 posts views Thread by Carramba | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.