By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

One question about FLOAT number as condition statement

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
G
Well , Thank you ! :-)

Dec 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.