469,623 Members | 1,111 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

One question about FLOAT number as condition statement

G
Hi ,you guys:

look at the code below
////////////////////////////////////////////////////////////////////////////////////////////////

#include<iostream>
using namespace std;

int main()
{
double min,max,add;
while(1)
{
cout<<"Please Input The Range of C (min max): ";
cin>>min>>max;
cin.ignore(20,'\n');

cout<<"Please Input The Increment : ";
cin>>add;

for(double C=min;C <= max;C+=add)
cout<<C<<endl;
}
return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
Compiler : Bloodshed Dev C++ 4.9.9.2

if I input 3.2 3.6 0.1 ,it will NOT output 3.6
but if 3.5 3.6 0.1 ,it will.
if 2.2 2.4 0.1, it will NOT output 2.4,
but if 0.2 0.4 0.1, it will output 0.4.
some other numbers would occurence like this ...

if I want to use FLOAT numbers as the condition statement, how can I
implemet it?

Thanx in advance !

Dec 21 '06 #1
3 2209
This is not a bug, but limitation of 32 or 64 bit system.

When we convert some number from decimal to binary format. Some time
they don't fit exactly in 32 bits. some number may take more then 32
bits to express exact decimal number or some numbers even can't be
converted in to binary system ( called recursive Number). In these
scenario, we take the nearest binary number which can be expressed in
32 bits.Because of this we create a difference of delta.

In some cases this delta creates problem, although it looks if we add
3.2 +0.1 it should ocme 3.3 but it comes 3.3 +delta. And thats what is
happeening those cases, you mentioned here.

If you really want to see then convert them in binary format and
perfome addition of binary numbers.

G wrote:
Hi ,you guys:

look at the code below
////////////////////////////////////////////////////////////////////////////////////////////////

#include<iostream>
using namespace std;

int main()
{
double min,max,add;
while(1)
{
cout<<"Please Input The Range of C (min max): ";
cin>>min>>max;
cin.ignore(20,'\n');

cout<<"Please Input The Increment : ";
cin>>add;

for(double C=min;C <= max;C+=add)
cout<<C<<endl;
}
return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
Compiler : Bloodshed Dev C++ 4.9.9.2

if I input 3.2 3.6 0.1 ,it will NOT output 3.6
but if 3.5 3.6 0.1 ,it will.
if 2.2 2.4 0.1, it will NOT output 2.4,
but if 0.2 0.4 0.1, it will output 0.4.
some other numbers would occurence like this ...

if I want to use FLOAT numbers as the condition statement, how can I
implemet it?

Thanx in advance !
Dec 21 '06 #2
G a écrit :
Hi ,you guys:

look at the code below
[snip]
for(double C=min;C <= max;C+=add)
[snip]

if I input 3.2 3.6 0.1 ,it will NOT output 3.6
but if 3.5 3.6 0.1 ,it will.
if 2.2 2.4 0.1, it will NOT output 2.4,
but if 0.2 0.4 0.1, it will output 0.4.
some other numbers would occurence like this ...

if I want to use FLOAT numbers as the condition statement, how can I
implemet it?
Float numbers are not garantied to represent all numbers.
That means that the equality operator may not be used accurately:
min = 2.2;
max = 2.4;
incr = 0.1;

in the for loop:
1. C = min -C= 2.2
2. C += incr -C= 2.2 + 0.1 = 2.200000000000...0047
3. C += incr -C=2.40000000000..0047 2.4

Your design is faulty:
input 1 1000000000 1 may loop indefinetly because at some point x+1=x;

Rather use:
int nbstep = std::ceil(( max - min ) / incr );
//assuming incr != 0 and bound are respected
//use std::numeric_limits<double>
for(int i=0;i<nbstep ; ++i)
{
double C=min+i*inc;
//....
}
//make some decision about last value
Michael
Dec 21 '06 #3
G
Well , Thank you ! :-)

Dec 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Gurikar | last post: by
22 posts views Thread by James H. | last post: by
11 posts views Thread by Capstar | last post: by
9 posts views Thread by Amy Lee | last post: by
18 posts views Thread by Lie | last post: by
135 posts views Thread by robinsiebler | last post: by
26 posts views Thread by neha_chhatre | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.