473,396 Members | 2,023 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,396 software developers and data experts.

Why the control is not going into the loop?

Hi all,

I executed the following program in compiler,the control didn't go into the loop What is the reason?Your help is high li appreciable.

#include <stdio.h>

void main()
{
float x=1.1;
while(x == 1.1)
{
printf("%f\n",x);
x=x-0.1;
}
}

Pls help me.....
Apr 28 '09 #1
5 1992
donbock
2,426 Expert 2GB
@mohanphani2003
The first rule is NEVER compare floating point numbers (float or double) for equality (==) or inequality (!=). Floating point representations are approximations; you cannot predict which cases will compare precisely equal and which won't.

Even so, you might expect the equality comparison to work in your case. Notice that floating point literals are of type double unless you provide the "f" type suffix: for instance, "1.1f". Therefore, the comparison within the while statement is between float variable x and double constant 1.1; causing x to be promoted to a double. The promoted value of x has less precision that the constant, so the values compare not equal. If you are determined to proceed with this experiment, I suggest you either declare x as a double or add an "f" suffix to all of your floating pointer literal constants.

There's a link to a floating point tutorial that is often provided in cases like this. I can't find the site. Hopefully one of the other experts will give it to us.
Apr 28 '09 #2
donbock
2,426 Expert 2GB
This is the Insights bulletin board. You should't post questions here. Questions should go in the answers bulletin board.
Apr 28 '09 #3
JosAH
11,448 Expert 8TB
@mohanphani2003
Check out the very first article in this section; it contains a link to a canonical article that explains all of the gory details of floating point operations in great detail.

kind regards,

Jos
Apr 28 '09 #4
donbock
2,426 Expert 2GB
@donbock
@JosAH
I guess you can tell that I didn't really look that hard. I suggest you read What Every Computer Scientist Should Know About Floating-Point Arithmetic.
Apr 28 '09 #5
JosAH
11,448 Expert 8TB
@donbock
I must admit that I only read your remark about this thread not belonging in the 'insights' section, but yes I've read that FP article; I agree that it is a must read if one ever wants to do something sensible with floating point numbers.

kind regards,

Jos
Apr 28 '09 #6

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

Similar topics

26
by: Wolfgang Keller | last post by:
Hello, is it that my know-how to use Google is insufficient or... ....does really noone use Python for industrial control applications? At least I didn't manage to find any publicly...
3
by: robert | last post by:
Mr. Kyte's article doesn't use cursors, while Mr. Feuerstein's book examples do. my recollection of conventional wisdom is to avoid using cursors. is this difference merely a question of style,...
9
by: Alvin Bruney [MVP] | last post by:
Exceptions must not be used to control program flow. I intend to show that this statement is flawed. In some instances, exceptions may be used to control program flow in ways that can lead to...
3
by: anon | last post by:
I am having a hard time with makeing what I am trying to do work. I am making a questionaire web app. I have a custom control that has a label and 5 radio buttons. My problem is that each...
3
by: I am Sam | last post by:
I keep getting the following error message when I try to iterate through a CheckBoxList control: Object reference not set to an instance of an object. Description: An unhandled exception...
6
by: Nick Stansbury | last post by:
Hi, I have a loop running on Page_PreRender that sets a number of controls to invisible based on a set of criteria. Before I do this however, I set all of the drop down lists to be visible with...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
1
by: adstheman | last post by:
Hi all, I'm testing new versions of our suite of software products on different Windows platforms and found this strange problem the other day. When running the test release on a clean Windows...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.