wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Double Boolean Operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double Boolean Operations" Watch "Double Boolean Operations" New topic
Author

Double Boolean Operations

Rick Tasche
Greenhorn

Joined: Feb 20, 2012
Posts: 10
Please note I have absolutely no control over the fact that the numbers I am working on are Doubles, and can not change them.

How do I preform boolean ( > < == != ) operations on doubles ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Rick Tasche wrote:Please note I have absolutely no control over the fact that the numbers I am working on are Doubles, and can not change them.
How do I preform boolean ( > < == != ) operations on doubles ?

aDouble.compareTo(anotherDouble) and aDouble.equals(anotherDouble)

I suggest you look up java.lang.Comparable in the API docs.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rick Tasche
Greenhorn

Joined: Feb 20, 2012
Posts: 10
I'm sorry, but anything from that site is unreadable ( to me ).

What you are saying is there is no such thing as a simple boolean when using decimals ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Either that or use .doubleValue() to convert to double and use the primitive comparison operators.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Rick Tasche wrote:I'm sorry, but anything from that site is unreadable ( to me ).

Then you're in for a long and unhappy life as a programmer.

If it's just the font size (which I have to admit bugs my old eyes) use the Zoom on your browser; but you really do need to get used to reading the API docs.

Otherwise you'll get a lot of RTFM answers to your questions.

Winston

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
If you are using Java5 or higher, you can simply use the > < operators using boxing; I don’t think that will work for != and == however; you would need to use d1.equals(d2) or !d3.equals(d4).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Campbell Ritchie wrote:you would need to use d1.equals(d2) or !d3.equals(d4).

Good point. It's also worth noting that there are subtle differences between Double and double when it comes to comparisons: eg, -0.0 is NOT equal to 0.0, but
@Rick: you'll have to read the API docs to find out.

Winston
Rick Tasche
Greenhorn

Joined: Feb 20, 2012
Posts: 10
10 years programming experience in - other languages - .
Java by far has the worse syntax of anything I have ever been forced to use, the documentation included.

How would one "box" a double boolean ?
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Winston Gutkowski wrote:
Rick Tasche wrote:I'm sorry, but anything from that site is unreadable ( to me ).

Then you're in for a long and unhappy life as a programmer.

If it's just the font size (which I have to admit bugs my old eyes) use the Zoom on your browser; but you really do need to get used to reading the API docs.

Otherwise you'll get a lot of RTFM answers to your questions.

Winston



I'm with Rick, I was progamming java about 2 years before the api documentation made much sense to me. (java 1.4) it did seem to improve recently with descriptive pages actually explaining how stuff works.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

I suppose each to their own. The Java libraries are huge, so it does take some time to find your way around them, but I've found Java to be the best and most consistently documented of any of the dozen or so languages I've used.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Wendy Gibbons wrote:I'm with Rick, I was progamming java about 2 years before the api documentation made much sense to me.

Maybe it's my background coming from languages that didn't have it then; but I find the API docs (and Javadoc in general) absolutely wonderful.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Rick Tasche wrote:10 years programming experience in - other languages - .
Java by far has the worse syntax of anything I have ever been forced to use, the documentation included.

How would one "box" a double boolean ?

Boolean[] doubleBoolean = new Boolean[] {true, false};
?

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
You’ve got more idea what a double boolean is than I have, then
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Rick Tasche wrote:Java by far has the worse syntax of anything I have ever been forced to use, the documentation included.

Can I ask what you've used before? As I said, none of the languages (COBOl, PL/1, C, C++, Pascal and a few others) that I learned had any sort of doc system at all, so I find Javadoc brilliant.

Winston
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Indeed, the reason I got frustrated with Pascal, C and C++ was because I always had to hunt all over the place to find documentation for functions from various libraries I had to include. And of course half of them don't work consistently for different environments. Especially bad with C because when I needed help I always stumbled on forums full of unreadable *nix techie lingo.

Java's huge standard library was a deep breath of fresh air.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Stephan van Hulst wrote:And of course half of them don't work consistently for different environments. Especially bad with C because when I needed help I always stumbled on forums full of unreadable *nix techie lingo.

Indeed. And as for the language syntax itself: again, I love it; but I grew up with C and C++, so it's very familiar. I can understand how it might not be for everybody though.

Java's huge standard library was a deep breath of fresh air.

Amen.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
There’s only one other language with anything like Java’s documentation: C#. But the Java documentation is better, even if only because you don’t have to use an IDE to find it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Campbell Ritchie wrote:But the Java documentation is better, even if only because you don’t have to use an IDE to find it...

Sheesh. Chalk another "duh" up for GOGW (good old Gates-ware).

Winston
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Winston Gutkowski wrote:
Campbell Ritchie wrote:But the Java documentation is better, even if only because you don’t have to use an IDE to find it...

Sheesh. Chalk another "duh" up for GOGW (good old Gates-ware).

I'm not sure that's fair. I use C# most of the time at the moment, and I get at the documentation via MSDN, not via Visual Studio. (And, it has to be said, Visual Studio is a damn good IDE).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
They had the advantage when they wrote C# of being able to see the mistakes made in Java.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Matthew Brown wrote:
Winston Gutkowski wrote:Sheesh. Chalk another "duh" up for GOGW (good old Gates-ware).

I'm not sure that's fair. I use C# most of the time at the moment, and I get at the documentation via MSDN, not via Visual Studio. (And, it has to be said, Visual Studio is a damn good IDE).

Yes, but Eclipse is free, and covers everything that VS does and more (as do a lot of freeware/shareware products). Hopefully, MS will get hoisted by their own petard, like IBM did, when people realize that there are more flexible (and better) products out there.

But that's an argument for a different time (and thread ).

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Wendy Gibbons wrote:I'm with Rick, I was progamming java about 2 years before the api documentation made much sense to me.

Maybe it's my background coming from languages that didn't have it then; but I find the API docs (and Javadoc in general) absolutely wonderful.

Winston


Ditto. After years in C, C++, Perl, TCL, ... One of my favorite things about Java has alwyas been the Javadocs. Although the color scheme and/or layout of the docs for 7 will take some getting used to.

As for the syntax, I don't find it any better or worse overall than any other language I've used. It has it's pluses and minuses, but I've always found it easy to write and to read (though my C experience probably has a lot to do with that).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rick Tasche wrote:
How would one "box" a double boolean ?


You don't. I have no idea what a double boolean even is.

You can autobox a double though, or a boolean. Autoboxing/unboxing is simply the compiler inserting appropriate code to translate between primitives and their wrappers so that we don't have to do it ourselves.



As for your question about using <, >, ==, != on Doubles, I second the advice to just unbox before doing the operations.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Winston Gutkowski wrote:
Matthew Brown wrote:
Winston Gutkowski wrote:Sheesh. Chalk another "duh" up for GOGW (good old Gates-ware).

I'm not sure that's fair. I use C# most of the time at the moment, and I get at the documentation via MSDN, not via Visual Studio. (And, it has to be said, Visual Studio is a damn good IDE).

Yes, but Eclipse is free, and covers everything that VS does and more (as do a lot of freeware/shareware products). Hopefully, MS will get hoisted by their own petard, like IBM did, when people realize that there are more flexible (and better) products out there.

But that's an argument for a different time (and thread ).

Winston



I have to agree with Matthew --- Visual Studio is definitely a damn good IDE !!

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
You will get problems with == and != because the values are probably not unboxed before those tests.
java DoubleDemo
d1 > d2: false
d1 >= d2: true
d1 < d2: false
d1 <= d2: true
d1 == d2: false
d1 != d2: true
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:You will get problems with == and != because the values are probably not unboxed before those tests.[code=java]Double d1 = 1.23;


Oh, there's no "probably" about it. The language rules for Reference Equality Operators == and != didn't change when autoboxing was introduced.

That's why I vote for the explicit unboxing to literals, even if it's not necessary because we're doing a <, <=, >, or >=, so that we'll be consistent and have less chance of confusion or of inadvertently using == or != when we shouldn't. Kind of like always using braces for ifs, even when not strictly necessary.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Henry Wong wrote:I have to agree with Matthew --- Visual Studio is definitely a damn good IDE !!

Does it still cover Java, after all those lawsuits?

Winston
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

10 years programming experience in - other languages - .
Java by far has the worse syntax of anything I have ever been forced to use, the documentation included.


I'm still intrigued to know what these other languages are, along with examples of them having better syntax than Java...oh yeah, and where this fantastic documentation is.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Jeff Verdegan wrote:That's why I vote for the explicit unboxing to literals, even if it's not necessary...

Agreed, as long as Rick remembers what I said above: Comparisons for Double and double are not the same.

Winston
 
Don't get me started about those stupid light bulbs.
 
subject: Double Boolean Operations
 
Similar Threads
Perfect conversion for bytes to kilobytes conversion and back forth
why use int over double?
One basic thread question
Tricky assignment i=i++;
Mock Question