• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double Boolean Operations

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either that or use .doubleValue() to convert to double and use the primitive comparison operators.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’ve got more idea what a double boolean is than I have, then
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic