472,371 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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 2320
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
5
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from...
11
by: Gurikar | last post by:
Hello, Can any one tell me is the code below correct. #include<iostream.h> int main() { int i = 1; float f = i / 2; if(f) cout<<"HI";
22
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
9
by: Amy Lee | last post by:
Hello, I major maths in mu university, and I wanna build an app to do Steffensen's Acceleration Method. Here's my code: /* Equation: x^3-x-1=0 */ #include <stdio.h> #include <math.h>
18
by: Lie | last post by:
I'm very surprised actually, to see that Python rejected the use of fractional/rational numbers. However, when I read the PEP, I know exactly why the proposal was rejected: People compared...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
26
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.