User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,795 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 542 | Replies: 4 | Solved
Reply
Join Date: Oct 2006
Posts: 130
Reputation: Barefootsanders is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
Barefootsanders Barefootsanders is offline Offline
Junior Poster

!= opperator in a mathematical expression?

  #1  
Dec 2nd, 2007
Hey everyone,

I was wondering if someone could explain to me the following bit of code. I understand != in logic but i dont know how to read it like this:

float y = 0.01, x = 0.005, z;

z = 2 * x + (y != 0.001);

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: != opperator in a mathematical expression?

  #2  
Dec 2nd, 2007
Boolean operators evaluate to 0 (false) or 1 (true).

So depending on the outcome of that, you have
z = 2 * x + (0);
z = 2 * x + (1);
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: != opperator in a mathematical expression?

  #3  
Dec 2nd, 2007
Actually, in C 0 always stands for false, but any other number stands for true (not only 1). I'm not sure about the other way, it is possible that for this direction C automatically returns 1 (instead of any number different from 0).

However, knowing C it might be that the expression (y != 0.001) returns 0 when false, but 0.001 when true?

You can just test the code by simply outputting what (y != 0.001) returns and work from there.

Black Box
Reply With Quote  
Join Date: Oct 2006
Posts: 130
Reputation: Barefootsanders is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: != opperator in a mathematical expression?

  #4  
Dec 2nd, 2007
Oo got ya. Thanks
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: != opperator in a mathematical expression?

  #5  
Dec 2nd, 2007
Originally Posted by Barefootsanders View Post
Oo got ya. Thanks

Careful, checking for inequality using float is not an exact science due to the way a real number is stored in memory.
Most like it ( y != 0.01 ) will return a 1.000000 even when it should have return a 0.000000 if you use a float type and not a double.
Last edited by Aia : Dec 2nd, 2007 at 3:10 pm.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 9:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC