473,387 Members | 1,528 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,387 software developers and data experts.

Problem with the while statemnet

105 100+
I have some problem in execution of the program :

the code is for example:

void a (float value)
{

float size=0;
size=value;
while(size>=value)
{
some statements
}

when I print the values of size and value, they are the same.but it is
not entering the while loop even though the condition is true.If I just write some
cout statement it enters into the loop.

when I run the debugger , value =1e-10 and when I assign the same value to
size, size=1.074e-038#DEN in the watch window.

When I display using the cout I get both the same values.

I am not able to understand what the problem is.I tried to run in debug and release versions , it does'nt work.

Could anyone give any suggestion ?Thanks
Nov 27 '07 #1
4 1218
gpraghuram
1,275 Expert 1GB
I have some problem in execution of the program :

the code is for example:

void a (float value)
{

float size=0;
size=value;
while(size>=value)
{
some statements
}

when I print the values of size and value, they are the same.but it is
not entering the while loop even though the condition is true.If I just write some
cout statement it enters into the loop.

when I run the debugger , value =1e-10 and when I assign the same value to
size, size=1.074e-038#DEN in the watch window.

When I display using the cout I get both the same values.

I am not able to understand what the problem is.I tried to run in debug and release versions , it does'nt work.

Could anyone give any suggestion ?Thanks

HI,
Floating numerals are not suited for comparion in the condition statements.
Sometimes the condition may be successful and sometimes not.
Raghuram
Nov 28 '07 #2
Hi this code working fine for me check code & result

Code:--->

1 #include<iostream.h>
2 void a (float value)
3 {
4
5 float size=0;
6 size=value;
7 while(size>=value)
8 {cout<<"Inside of while"<<endl;
9 cout<<"value of size in Function A = \t "<<size<<endl;
10 cout<<"value of value in function A = \t"<<value<<endl;
11 value++;
12 }
13 }
14 int main ()
15 {
16 float v=1e-10;
17 cout<<"value of V in Main = \t"<<v<<endl;
18 a(v);
19 cout<<"out of main"<<endl;
20 return 0;
21 }
22

output:--->

value of V in Main = 1e-10
Inside of while
value of size in Function A = 1e-10
value of value in function A = 1e-10
out of main
Nov 28 '07 #3
mickey22
105 100+
I just tried in the same way writing separately..It works fine.When I include some other statements it does'nt work.Its really wierd.

Thanks for all the suggestions.
Nov 28 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
Due to the automatic rounding, these operators may report a condition as true when the numbers
are only close in value.

Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

To compare two floating point numbers, you have to establish a sigma error tolerance.

Expand|Select|Wrap|Line Numbers
  1. if (fabs(i - j) < 0.000001) { ... // almost equal }
  2.  
Nov 28 '07 #5

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

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
8
sammyboy78
by: sammyboy78 | last post by:
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
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: 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
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...
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,...

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.