Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Autonomous Problems / I'm dumb (http://www.chiefdelphi.com/forums/showthread.php?t=146376)

HatterTingamaji 03-28-2016 07:41 PM

Autonomous Problems / I'm dumb
 
Ok so I'm trying to get Autonomous up and running and forgot what I would use to convert from Int to Boolean,
if you can even do that.
I'm not even sure anymore.
:yikes: :o

GeeTwo 03-28-2016 07:50 PM

Re: Autonomous Problems / I'm dumb
 
While there are casts, the best (most readable and consistent across languages) way to convert a number to Boolean is to us a comparison operator. For example, if you want false when x is zero and true otherwise, use (x!=0).

Edit: that's ok with integers. With floats, you have to watch out for NaN cases. My favorite way to test around all of the cases where x is not a number is to do something like (x==x && x!=0).

HatterTingamaji 03-28-2016 07:55 PM

Re: Autonomous Problems / I'm dumb
 
Ok so I'm not crazy (well no more so than usual). Thank you!
:D

euhlmann 03-28-2016 10:05 PM

Re: Autonomous Problems / I'm dumb
 
Quote:

Originally Posted by GeeTwo (Post 1564410)
My favorite way to test around all of the cases where x is not a number is to do something like (x==x && x!=0).

:ahh:

These exist for a reason!
Code:

Double.isNaN(double)
Double.isInfinite(double)


GeeTwo 03-29-2016 06:35 AM

Re: Autonomous Problems / I'm dumb
 
Calling two negatively phrased language-dependent methods is better than one comparison? If there were an isFinite() method, I would consider using it.

euhlmann 03-29-2016 09:31 AM

Re: Autonomous Problems / I'm dumb
 
Quote:

Originally Posted by GeeTwo (Post 1564534)
Calling two negatively phrased language-dependent methods is better than one comparison? If there were an isFinite() method, I would consider using it.

Better than using x==x, which seems hacky and is surely platform/implementation dependent. It's better not to introduce hard-to-diagnose bugs in your code.


All times are GMT -5. The time now is 07:24 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi