473,568 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float/double/long double range?

13 New Member
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 17369
gpraghuram
1,275 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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
shweta khare
13 New Member
Thank u for clarifying my doubts.....
Aug 15 '08 #4
shweta khare
13 New Member
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 Recognized Expert Moderator Expert
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
shweta khare
13 New Member
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 interconversion s?
Aug 15 '08 #7
JosAH
11,448 Recognized Expert MVP
Also read "What every computer scientist should know about floating-point arithmetic".

kind regards,

Jos
Aug 15 '08 #8
newb16
687 Contributor
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 Recognized Expert Moderator Expert
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
11776
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
102447
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
15262
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 will be rounded off. For doubles, the mantissa is 53 bits = 2^53-1 max value = 9,007,199,254,740,991.0l (that's an L). So 16 digit odd numbers...
9
90122
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 searched numberless websites but nothing could answer my question. I would be pleased to get some help. Regards
2
2478
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 in any way?. Because I dont remember seeing something like 'long float ' anywhere and only 'long
6
7596
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, Karthi
8
5445
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, 6 = Scale 1) When I read this field into float datatype. I get a value 1.9000, which is correct. But when I read its value in a double datatype...
60
7174
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
35736
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going...
45
5246
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 multiple it by 1000 to get 11111 and the preform bitwise like <<2 to get 88888 and then divide by 1000 to go back to float 8.8888. but these seem...
0
7692
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2099
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.