Skip to main content



      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 14:15 Go to next message
Eclipse UserFriend
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 14:27] by Moderator

Re: Illogical Logic what am I doing wrong? [message #1700092 is a reply to message #1700026] Tue, 30 June 2015 02:27 Go to previous message
Eclipse UserFriend
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: Thu Jul 03 12:50:50 EDT 2025

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

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

Back to the top