473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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;
while(x==1.1)
{
printf("\n %f",x);
x=x-.1;
}
getch( );
}

why isnt the program giving any output?
Aug 14 '08 #1
9 17359
gpraghuram
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. }
  5.  
I think this is told by one of the experts here(Banfa/Josah)
Serach in the forum for this

Raghu
Aug 14 '08 #2
Banfa
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.
1.1
.1
1e-3
are all examples of double constants, if you want a float constant you have to
tack an f on the end
1.1f
.1f
1e-3f
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
Banfa
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
JosAH
11,448 Expert 8TB
Also read "What every computer scientist should know about floating-point arithmetic".

kind regards,

Jos
Aug 15 '08 #8
newb16
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
Banfa
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

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

Similar topics

4
by: JKop | last post by:
Does the Standard specify any minimum range or minimum precision for the float, double and long double. I haven't found anything in the Standard about it. -JKop
11
by: Christian Christmann | last post by:
Hi, just a general question. Which data type is more precise for large values: float or double? Thank you. Chris
14
by: ziller | last post by:
Why is it that FLT_DIG (from <float.h>) is 6 while DBL_DIB is 15? Doing the math, the mantissa for floats is 24 bits = 2^24-1 max value = 16,777,215.0f. Anything 8-digit odd # greater than that...
9
by: FalkoG | last post by:
Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is to convert the position after decimal point. I...
2
by: sriamar | last post by:
Hello, How does the type conversion work if the expression involves a float and long int? By K&R 2nd Ed i assume float & long -> float & float . But does the 'long' qualifier affect this...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.