473,479 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

while with double

3 New Member
Hi,
I have problems with the following code:


double LOW,HIGH,VALUE,STEP;
cout<<"Lower limit?\n";
cin>>LOW;
cout<<"Higher limit?\n";
cin>>HIGH;
STEP=0.1;
//first loop:
VALUE=LOW;
while (VALUE<HIGH+STEP)
{
cout<<"\t"<<VALUE;
VALUE=VALUE+STEP;
}
cout<<"\n";
//second loop:
VALUE=LOW;
while (!(VALUE>HIGH))
{
cout<<"\t"<<VALUE;
VALUE=VALUE+STEP;
}
cout<<"\n";


For instance:
If "LOW" is set to 0.7 and "HIGH" to 1.1, the first loop gives 0.7 up to 1.2 while the second loop works (0.7 to 1.1).
If "LOW" is set to 0.8 and "HIGH" to 1.2, the second loop gives 0.8 up to 1.1 while the second loop works (0.8 to 1.2).
Why is this?
Sep 7 '06 #1
3 2047
perca
3 New Member
Oopps! Should read:


For instance:
If "LOW" is set to 0.7 and "HIGH" to 1.1, the first loop gives 0.7 up to 1.2 while the second loop works (0.7 to 1.1).
If "LOW" is set to 0.8 and "HIGH" to 1.2, the second loop gives 0.8 up to 1.1 while the FIRST loop works (0.8 to 1.2).
Why is this?
Sep 7 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
This is due to what is called rounding errors. Floating point numbers do not hold an exact value but an approximation. When you do arithmatic on them then what you get is an approximate result. For instance given your first example

If "LOW" is set to 0.7 and "HIGH" to 1.1, the first loop gives 0.7 up to 1.2 while the second loop works (0.7 to 1.1).

it appears in the first loop that the condition

VALUE < HIGH+STEP is going wrong since when value = 1.2 (and is output)

1.2 < 1.1 + 0.1

appears to have been true when human logic would make it false.

The problem is that value is the result of many fp calculations, it may not be 1.2 but

1.19999999999999

This displays as 1.2 but when compared with 1.2 is actually lower.

The answer is not to use floating point numbers as loop control variables. having got the input parameters calculate the number of steps you wish to take and use an integer variable to control your loopand take that many steps.

Of course within the calculation of the number of steps you will have to take account of the fact that there may be a rounding error.
Sep 7 '06 #3
perca
3 New Member
Ok, that's what I was afraid of. Thanks anyway!

This is due to what is called rounding errors. Floating point numbers do not hold an exact value but an approximation. When you do arithmatic on them then what you get is an approximate result. For instance given your first example

If "LOW" is set to 0.7 and "HIGH" to 1.1, the first loop gives 0.7 up to 1.2 while the second loop works (0.7 to 1.1).

it appears in the first loop that the condition

VALUE < HIGH+STEP is going wrong since when value = 1.2 (and is output)

1.2 < 1.1 + 0.1

appears to have been true when human logic would make it false.

The problem is that value is the result of many fp calculations, it may not be 1.2 but

1.19999999999999

This displays as 1.2 but when compared with 1.2 is actually lower.

The answer is not to use floating point numbers as loop control variables. having got the input parameters calculate the number of steps you wish to take and use an integer variable to control your loopand take that many steps.

Of course within the calculation of the number of steps you will have to take account of the fact that there may be a rounding error.
Sep 7 '06 #4

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

Similar topics

1
2310
by: Brittany | last post by:
can someone explain to me what if else staments do and what while statements do.
34
4124
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
1
2295
by: AJ Smith | last post by:
To Whom It May Concern: I have the following code: #include <cctype> //Provides isdigit #include <cstdlib> //Provides EXIT_SUCCESS #include <cstring> //Provides strchr #include <iostream>...
3
1736
by: rmijares | last post by:
I am sure you guys have seen this before. I am working on the famous Employee Payroll program for a class assignment. In any case, the while loop at the end of the Employee class just keeps looping;...
1
1944
by: powerej | last post by:
program is suppose to ask for an input from 1-8, if the user enters a number outside of this range than it will continue to ask until it does, something is wrong and i cant seem to figure it out, i...
4
1987
by: Villanmac | last post by:
First up thanks to the guys who pointed out my error last week. Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
7
3992
by: ramana | last post by:
I'm wondering if someone could point me to the flaw in the following code that uses the while(!FP.eof()) condition to read the input data. This condition is reading the last data point of the file...
3
11923
by: spittinfire | last post by:
-------------------------------------------------------------------------------- I am trying to correct the errors on an assignment that has already been graded because I have to now modify it and...
1
1346
by: sonyb | last post by:
In this program , I informed the user to enter 3 random numbers .. Each one of these numbers were taken and entered into an function (2x + 5) and after the 3rd number was inserted into this...
2
2880
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the...
0
7019
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
7067
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...
1
6719
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
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5312
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,...
1
4757
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...
0
4463
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...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
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...

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.