Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double Boolean Operations

 
Rick Tasche
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rick Tasche
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either that or use .doubleValue() to convert to double and use the primitive comparison operators.
 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’ve got more idea what a double boolean is than I have, then
 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They had the advantage when they wrote C# of being able to see the mistakes made in Java.
 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic