Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Illogical Logic what am I doing wrong?(illogical logic -- help!)
Illogical Logic what am I doing wrong? [message #1700026] Mon, 29 June 2015 18:15 Go to next message
John Gray is currently offline John GrayFriend
Messages: 13
Registered: June 2015
Junior Member
Its a simple problem to describe: when I set diff_1== 0; sometimes it executes the code for diff_1 ==1 huh-? Right? If I isolate the code in another "scratch pad" app to test, it does fine.


diff_1 = 0;
	if (diff_1 == 0.00) {cout<<"flag_1";}
	if (diff_1==1.00) {pennies = pennies + 1;
	cout<<"increment by a penny to "<<pennies<<endl;}

-------------------------------------------------------------
works fine here -- "scratchpad"

#include <iostream>
#include<math.h>

using namespace std;

int main()
{
double difference = 0; // or 1
	if (difference == 0) {cout<<"no adjustment necessary"<<endl;}
		else if ((difference)!=0) {cout<<"adjustment necessary"<<endl;}

}



if I set difference to 1 or 0 it goes where it supposed to.

[Updated on: Mon, 29 June 2015 18:27]

Report message to a moderator

Re: Illogical Logic what am I doing wrong? [message #1700092 is a reply to message #1700026] Tue, 30 June 2015 06:27 Go to previous message
Ruslan Parkhomenko is currently offline Ruslan ParkhomenkoFriend
Messages: 6
Registered: June 2015
Junior Member
When you use floats and doubles, because of the way they work, the variable does not store the exact value. So 0 (NULL) may not be exactly 0, it might be 0.0000001 or something. This is why inequality and equality comparisons are unreliable with them.

So try to do it this way:

if (diff_1 < .001 ) {
//blah-blah
} else if (diff_1 > 0.99) {
//blah-blah
}

or evet this:

if (diff_1 < 1.0 ) {
//blah-blah
} else {
//blah-blah
}
Previous Topic:Odd compile errors using a struct in a vector
Next Topic:Eclipse does not save settings
Goto Forum:
  


Current Time: Fri Apr 26 05:21:37 GMT 2024

Powered by FUDForum. Page generated in 0.02695 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top