473,390 Members | 1,051 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,390 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 2383
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...

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.