Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

If/else loop

0 views
Skip to first unread message

John Devine

unread,
Jan 21, 2002, 1:50:34 AM1/21/02
to
I'm kind of new to java and I'd like to know how to write and if/else loop
on the basis of a double being positve or negative....

Can someone help me?


Dave Ortman

unread,
Jan 21, 2002, 1:25:00 AM1/21/02
to
"John Devine" <jde...@purdue.edu> wrote in message
news:a2ga82$d2h$1...@mozo.cc.purdue.edu...

> I'm kind of new to java and I'd like to know how to write and if/else loop
> on the basis of a double being positve or negative....
>
> Can someone help me?

if (double < 0) {
-- Do stuff for negative number
} else {
-- Do stuff for positive number (0 included)
}

-Dave Ortman


John Devine

unread,
Jan 21, 2002, 2:41:58 AM1/21/02
to

"Dave Ortman" <dor...@yahoo.com> wrote in message
news:u4nckos...@corp.supernews.com...
so would double =< make 0 do stuff for negative and zero?


Alex Potter

unread,
Jan 21, 2002, 6:23:35 AM1/21/02
to

"John Devine" <jde...@purdue.edu> wrote in message
news:a2gd8b$e14$1...@mozo.cc.purdue.edu...

Think "less than or equal to"

Alex


Lee Ryman

unread,
Jan 21, 2002, 9:04:40 AM1/21/02
to
> > >
> > so would double =< make 0 do stuff for negative and zero?
> >
>
> Think "less than or equal to"
>
> Alex

should be <= should it not?


Chris Smith

unread,
Jan 21, 2002, 9:59:39 AM1/21/02
to
Dave Ortman wrote ...

> > I'm kind of new to java and I'd like to know how to write and if/else loop
> > on the basis of a double being positve or negative....
> >
> > Can someone help me?
>
> if (double < 0) {
> -- Do stuff for negative number
> } else {
> -- Do stuff for positive number (0 included)
> }

Since IEEE floating point has both positive and negative zero values, it
may be more correct to write:

if ((d < 0) || (d == -0.0))
{
// stuff for negative numbers
}
else
{
// stuff for positive numbers
}

Of course, that depends on the original application. Just pointing out
that unlike real numbers, IEEE floats are *always* either positive or
negative.

Chris Smith

Sam Baker

unread,
Jan 21, 2002, 11:45:03 AM1/21/02
to
If a double variable d equals -0.0, then does (d == 0.0) return a boolean value of
false?
Can -0.0 occur as the result of normal calculations in a Java application?


Patricia Shanahan

unread,
Jan 21, 2002, 12:47:29 PM1/21/02
to

Sam Baker wrote:
>
> If a double variable d equals -0.0, then does (d == 0.0) return a boolean value of
> false?

0.0 == -0.0 is true.

> Can -0.0 occur as the result of normal calculations in a Java application?

Depends on what you mean by "normal". It is used to represent a result
is negative but has an absolute magnitude too small to represent. For
example:

public class Test{
public static void main(String[] args){
System.out.println(1.0/Double.NEGATIVE_INFINITY);
System.out.println(-1e-300/1e100);
}
}

Patricia

Sam Baker

unread,
Jan 21, 2002, 3:04:46 PM1/21/02
to
> 0.0 == -0.0 is true.

Chris Smith, above, suggested:


if ((d < 0) || (d == -0.0))
{
// stuff for negative numbers
}
else
{
// stuff for positive numbers
}

but Chris's code does the "stuff for negative numbers" if d == 0.0.

I just tested this. I'm using Java 1.3.
I dropped this code into an applet that has a text area called Answer:

double d = 0.0;


if ((d < 0) || (d == -0.0))
{

Answer.setText("Negative number.");
}
else
{
Answer.setText("Positive number.");
}
}
When this is compiled and viewed, the text area reads "Negative number."


Patricia Shanahan

unread,
Jan 21, 2002, 5:06:09 PM1/21/02
to
There are ways to test for -0.0, but I don't think it can be done by
simple comparison. If you really need to treat -0.0 as negative then you
could look at 1/d:

(d < 0 || 1/d < 0)

1/-0.0 is Double.NEGATIVE_INFINITY, and is unambiguously less than 0.

However, I generally find it more useful to treat both -0.0 and 0.0 as
zero, neither positive nor negative.

Java arithmetic is set up to support that view. It only makes a
distinction in how they are printed, and in the sign of the result of a
few operations such as division of a non-zero by a zero.

Patricia

Phil Eschallier

unread,
Jan 21, 2002, 5:10:16 PM1/21/02
to

and this is what you'd expect in any 2's Complement machine ... 0 ==
-0 because there is no binary representation for -0 in 2's Complement
so this value is stored as 0.

--
Phil Eschallier
Bux Technical Services
70 Irish Meetinghouse Rd
Perkasie, PA 18944
215.249.TECH (215.249.8324)
215.249.8325 (fax)
http://www.BuxTech.Com

Carl Howells

unread,
Jan 21, 2002, 5:15:54 PM1/21/02
to
"Phil Eschallier" <ph...@buxtech.com> wrote...

> and this is what you'd expect in any 2's Complement machine ... 0 ==
> -0 because there is no binary representation for -0 in 2's Complement
> so this value is stored as 0.

For integer types, yes. But this conversation is on floating-point types.
They are most definately NOT stored as 2's compliment numbers.


Phil Eschallier

unread,
Jan 21, 2002, 5:31:22 PM1/21/02
to

Carl;

Most interesting, I was not aware that floating point on a 2's
Complement machine could represent -0. Could you provide any
references to such mechanics? I'd very much like to learn more about
this.

My understanding of this is that floating point is the combination of
a base (mantissa) and an exponent ... each of which would be 2's
Complement numbers on a 2's Complement machine ... thus my confusion
because I can't figure out what combination of mantissa and exponent
would result in -0.0 in such an environment.

Thanx in advance for any references you can provide.

... Phil

Sam Baker

unread,
Jan 21, 2002, 5:51:52 PM1/21/02
to
> There are ways to test for -0.0, but I don't think it can be done by
> simple comparison. If you really need to treat -0.0 as negative then you
> could look at 1/d:
>
> (d < 0 || 1/d < 0)
>
> 1/-0.0 is Double.NEGATIVE_INFINITY, and is unambiguously less than 0.

I like it!

> However, I generally find it more useful to treat both -0.0 and 0.0 as
> zero, neither positive nor negative.

Right. For many applications, you don't want to use == with doubles or floats
anyway. Instead, you allow for round-off error by testing
( (Math.abs(d1 - d2) < epsilon )
where d1 and d2 are doubles and epsilon is how much difference you'll
tolerate.

Dale King

unread,
Jan 21, 2002, 6:37:49 PM1/21/02
to
"Phil Eschallier" <ph...@buxtech.com> wrote in message
news:86g04zz...@buxtech.com...

Floating point types have a sign bit. The manitssa is unsigned. The exponent
is not exactly two's complement either. It is an unsigned from which you
subtract a certain number to get the real number. Then you also have to take
int account that the manitssa usually has an implied leading 1 that is not
stored.

> Thanx in advance for any references you can provide.

See any reference on IEEE754 or floating point types in the JLS.

--
Dale King


Patricia Shanahan

unread,
Jan 21, 2002, 7:11:38 PM1/21/02
to

Phil Eschallier wrote:
>
> >>>>> "Carl" == Carl Howells <chow...@cs.uoregon.edu> writes:
>
> > "Phil Eschallier" <ph...@buxtech.com> wrote...
> >> and this is what you'd expect in any 2's Complement machine
> >> ... 0 == -0 because there is no binary representation for -0 in
> >> 2's Complement so this value is stored as 0.
>
> > For integer types, yes. But this conversation is on
> > floating-point types. They are most definately NOT stored as
> > 2's compliment numbers.
>
> Carl;
>
> Most interesting, I was not aware that floating point on a 2's
> Complement machine could represent -0. Could you provide any
> references to such mechanics? I'd very much like to learn more about
> this.

The JVM is a machine that uses 2's complement to represent signed
integers, but a major subset of IEEE 754 to represent floating point
numbers. Do you consider that to be a "2's complement machine"?

Many real processors also use 2's complement for signed integers and
IEEE 754 formats for floating point.

>
> My understanding of this is that floating point is the combination of
> a base (mantissa) and an exponent ... each of which would be 2's
> Complement numbers on a 2's Complement machine ... thus my confusion
> because I can't figure out what combination of mantissa and exponent
> would result in -0.0 in such an environment.

The structure of a normalized IEEE 754 64-bit float has a sign bit for
the overall number, and represents the exponent in excess-1023 notation
(subtract 1023 from the stored binary value to get the actual exponent).
No 2's complement at all.

0.0 is represented by the all bits zero pattern. -0.0 is represented by
the sign bit being one and the remaining 64 bits zero.

>
> Thanx in advance for any references you can provide.

The obvious one is the Java Language Specification, "4.2.3
Floating-Point Types, Formats, and Values" at
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208

If you have access to IEEE standards, it may also be worth reading IEEE
754.

You can examine the innards of Java floating point numbers by using
Double.doubleToLongBits.

Patricia

Chris Smith

unread,
Jan 21, 2002, 7:10:28 PM1/21/02
to
Patricia Shanahan wrote ...

> There are ways to test for -0.0, but I don't think it can be done by
> simple comparison.

Yep. My mistake. I keep posting code without testing it... someone slap
me next time I do that.

Chris

Phil Eschallier

unread,
Jan 22, 2002, 11:29:02 AM1/22/02
to

> Patricia

All;

Thanx to you or posted the references that I'd requested.

I can see that there is certainly a representation for -0.0, but I
haven't yet played with code to produce such a value as the result of
an arithmetic operation.

My original motivation for posting was that I used to develop software
for the US Navy, for which several of the target platforms were older
avionic computers that were 1's Complement machines. Integer and
fixed point math (we didn't have floating point) definitety had
potential for results being -0 or -0.0. And in such an environment,
-0 != 0. In working with more modern machines, I'd believed that -0.0
was not a possibility. Even with formal education and experience,
it's obvious that I needed to read the IEEE docs a bit more closely
before getting involved here.

As soon as I get a break, I'll play with some tests for my own
edification. But just FYI (as most of you would probably expect)
... despite the different binary representation of 0.0 and -0.0, it
would seem that equality tests don't care. For example, my 2 minute
test code:

$ cat Neg.java

public class Neg {
public static void main (String[] args) {
double v1 = 0.0;
double v2 = -0.0;

System.out.println("v1 -->" + v1);
System.out.println("v2 -->" + v2);

// test 1
if (v1 > 0.0) {
System.out.println("v1 greater than 0.0");
} else {
System.out.println("v1 less than or equal to 0.0");
}

// test 2
if (v1 > -0.0) {
System.out.println("v1 greater than -0.0");
} else {
System.out.println("v1 less than or equal to -0.0");
}

// test 3
if (v2 > 0.0) {
System.out.println("v2 greater than 0.0");
} else {
System.out.println("v2 less than or equal to 0.0");
}

// test 4
if (v2 > -0.0) {
System.out.println("v2 greater than -0.0");
} else {
System.out.println("v2 less than or equal to -0.0");
}

// test 5
if (v1 == v2) {
System.out.println("v1 == v2");
} else if (v1 > v2) {
System.out.println("v1 > v2");
} else if (v1 < v2) {
System.out.println("v1 < v2");
} else {
System.out.println("Who knows??");
}
}
}

Yields (on both Solaris SPARC and Win2k):

$ java Neg
v1 -->0.0
v2 -->-0.0
v1 less than or equal to 0.0
v1 less than or equal to -0.0
v2 less than or equal to 0.0
v2 less than or equal to -0.0
v1 == v2

Again ... just FYI ... I'm sure that a better set of tests / examples
will come, I just wanted to hack together something to prove to myself
that conceptually, 0.0 and -0.0 are equivalent.

Again, thanx to those who'd followed up.

... Phil

Patricia Shanahan

unread,
Jan 23, 2002, 1:17:24 AM1/23/02
to

Phil Eschallier wrote:
...


> As soon as I get a break, I'll play with some tests for my own
> edification. But just FYI (as most of you would probably expect)
> ... despite the different binary representation of 0.0 and -0.0, it
> would seem that equality tests don't care. For example, my 2 minute
> test code:

...

It is correct that (0.0 == -0.0), and documented in the description of
floating point == in the JLS.

Even wierder, if you are used to earlier floating point systems, is the
fact that some pairs of IEEE floats are not comparable. In effect there
are four comparison results: less, equal, greater, and none of the
above. Double.NaN does not compare equal to anything, not even itself.

Patricia

Roedy Green

unread,
Jan 27, 2002, 9:02:07 PM1/27/02
to
On Mon, 21 Jan 2002 00:50:34 -0600, "John Devine" <jde...@purdue.edu>
wrote or quoted :

>I'm kind of new to java and I'd like to know how to write and if/else loop
>on the basis of a double being positve or negative....
>
>Can someone help me?

if (d < 0) doNegative();
if (d > 0) doPositive();

See the java cheat sheet in the Java glossary for how to do loops.

--
eagerly seeking telecommuting programming work.
canadian mind products, roedy green
the java glossary is at
http://www.mindprod.com/gloss.html
or http://209.139.205.39

Roedy Green

unread,
Jan 27, 2002, 9:09:47 PM1/27/02
to
On Mon, 21 Jan 2002 07:59:39 -0700, Chris Smith <cds...@twu.net>
wrote or quoted :

>Of course, that depends on the original application. Just pointing out
>that unlike real numbers, IEEE floats are *always* either positive or
>negative.

But surely +0. == -0. ??

scampbell

unread,
Jan 27, 2002, 9:21:40 PM1/27/02
to

"Roedy Green" <ro...@mindprod.com> wrote in message
news:8lc95u0bho76djrpv...@4ax.com...


See the java glossary ...under luser or troll!
quit wasting bandwidth with your babbling!
people here are newbs and want real answers not your retarded gay ass snide
remarks...
no wonder you are "eagerly seeking" employment...

Jon Skeet

unread,
Jan 28, 2002, 3:31:25 AM1/28/02
to
Roedy Green <ro...@mindprod.com> wrote:
> On Mon, 21 Jan 2002 07:59:39 -0700, Chris Smith <cds...@twu.net>
> wrote or quoted :
>
> >Of course, that depends on the original application. Just pointing out
> >that unlike real numbers, IEEE floats are *always* either positive or
> >negative.
>
> But surely +0. == -0. ??

In value, yes, but not in bit pattern. That's why comparing two floating
point numbers isn't quite as simple as comparing their bit patterns.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Joel Rees

unread,
Jan 28, 2002, 10:13:42 PM1/28/02
to
"John Devine" <jde...@purdue.edu> wrote in message news:<a2ga82$d2h$1...@mozo.cc.purdue.edu>...

> I'm kind of new to java and I'd like to know how to write and if/else loop
> on the basis of a double being positve or negative....
>
> Can someone help me?

I assume that John got his answer, I hope he got it right. I'm
surprised that no one mentioned that if/else is not a loop. (I. e.,
If/else branches once. Loops go round and round.)

Reminders:
http://www.sun.com/documentation/
http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5198

Gotta love them docs!

Sample source:
(Note the three conditions used in comparing the doubles in the
loops.)

public class TestDoubles
{ public static void main( String[] argish )
{ double zero = 0.0;
double thing = -1.0;
double twisted = zero / thing; // Try to dodge the optimizer.
System.out.println( "Did we get minus zero? " + Double.toString(
twisted ) );
boolean fact = ( twisted == 0.0 );
System.out.println( "Is zero equal to minus zero? " + fact );
System.out.println( "Bit pattern, zero: " + Long.toHexString(
Double.doubleToLongBits( zero ) ) );
System.out.println( "Raw bit pattern, zero: " + Long.toHexString(
Double.doubleToRawLongBits( zero ) ) );
System.out.println( "Bit pattern, minus zero: " +
Long.toHexString( Double.doubleToLongBits( twisted ) ) );
System.out.println( "Raw bit pattern, minus zero: " +
Long.toHexString( Double.doubleToRawLongBits( twisted ) ) );
System.out.println( "" );

double big = 1 / zero;
double minusBig = 1 / twisted;
System.out.println( "Infinity: " + Double.toString( big ) );
System.out.println( "Bit pattern, infinity: " + Long.toHexString(
Double.doubleToLongBits( big ) ) );
System.out.println( "Raw bit pattern, infinity: " +
Long.toHexString( Double.doubleToRawLongBits( big ) ) );
System.out.println( "Minus infinity: " + Double.toString( minusBig
) );
System.out.println( "Bit pattern, minus infinity: " +
Long.toHexString( Double.doubleToLongBits( minusBig ) ) );
System.out.println( "Raw bit pattern, minus infinity: " +
Long.toHexString( Double.doubleToRawLongBits( minusBig ) ) );
fact = ( big == minusBig );
System.out.println( "Is infinity equal to minus infinity? " + fact
);
System.out.println( "" );

double zero2 = 1 / big;
double minusZero2 = 1 / minusBig;
System.out.println( "Another way to calculate zero: " +
Double.toString( zero2 ) );
System.out.println( "Another way to calculate minus zero: " +
Double.toString( minusZero2 ) );
System.out.println( "" );

System.out.println( "Counting down:" );
for ( double di = 1.0; di >= -1.0; di -= .1 )
{ System.out.print( "value: " + Double.toString( di ) );
if ( di < 0.0 )
System.out.println( " is less than zero." );
else if ( di == 0.0 )
System.out.println( " is zero." );
else
System.out.println( " is greater than zero." );
}
System.out.println( "\nCounting up:" );
for ( double di = -1.0; di <= 1.0; di += .1 )
{ System.out.print( "value: " + Double.toString( di ) );
if ( di < 0.0 )
System.out.println( " is less than zero." );
else if ( di == 0.0 )
System.out.println( " is zero." );
else
System.out.println( " is greater than zero." );
}
}
}

A good way to synthesize positive/negative NaN might be to store di
from the loops where the round-off error becomes significant, and
divide that very small number by 2.0 or 128.0 or 1024.0 or so.

Enjoy!

Joel

Roedy Green

unread,
Jan 27, 2002, 9:09:47 PM1/27/02
to
On Mon, 21 Jan 2002 07:59:39 -0700, Chris Smith <cds...@twu.net>
wrote or quoted :

>Of course, that depends on the original application. Just pointing out

>that unlike real numbers, IEEE floats are *always* either positive or
>negative.

But surely +0. == -0. ??

--


eagerly seeking telecommuting programming work.
canadian mind products, roedy green
the java glossary is at
http://www.mindprod.com/gloss.html
or http://209.139.205.39

========= WAS CANCELLED BY =======:
From: Roedy Green <ro...@mindprod.com>
Control: cancel <8lc95u0bho76djrpv...@4ax.com>
Subject: cmsg cancel <8lc95u0bho76djrpv...@4ax.com>
Date: Mon, 28 Jan 2002 00:04:05 GMT
Message-ID: <cancel.8lc95u0bho76d...@4ax.com>
X-No-Archive: yes
Newsgroups: microsoft.test,alt.flame.niggers,comp.lang.java.help
NNTP-Posting-Host: w088.z064003087.lax-ca.dsl.cnc.net 64.3.87.88
Lines: 1
Path: news.uni-stuttgart.de!news.fh-hannover.de!news-han1.dfn.de!news-nue1.dfn.de!newsfeed.r-kom.de!newsfeed.freenet.de!lon1-news.nildram.net!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.stealth.net!msrtrans1!msrnewsc1!cppssbbsa01.microsoft.com!tkmsftngp01!tkmsftngp04!u&n&a&c&anceller
Xref: news.uni-stuttgart.de control:40719336

This message was cancelled from within The Unacanceller's glorious new software, Lotus 1-2-3 For Rogue Cancellers.

scampbell

unread,
Jan 27, 2002, 9:21:40 PM1/27/02
to

"Roedy Green" <ro...@mindprod.com> wrote in message
news:8lc95u0bho76djrpv...@4ax.com...

See the java glossary ...under luser or troll!
quit wasting bandwidth with your babbling!
people here are newbs and want real answers not your retarded gay ass snide
remarks...
no wonder you are "eagerly seeking" employment...

========= WAS CANCELLED BY =======:
From: "scampbell" <mscam...@adelphia.net>
Control: cancel <UA258.12399$Xw4.3...@news1.news.adelphia.net>
Subject: cmsg cancel <UA258.12399$Xw4.3...@news1.news.adelphia.net>
Date: Mon, 28 Jan 2002 02:00:49 GMT
Message-ID: <cancel.UA258.12399$Xw4.3...@news1.news.adelphia.net>


X-No-Archive: yes
Newsgroups: microsoft.test,alt.flame.niggers,comp.lang.java.help
NNTP-Posting-Host: w088.z064003087.lax-ca.dsl.cnc.net 64.3.87.88
Lines: 1

Path: news.uni-stuttgart.de!news.fh-hannover.de!fu-berlin.de!news.stealth.net!msrtrans1!msrnewsc1!cppssbbsa01.microsoft.com!tkmsftngp01!tkmsftngp04!u&n&a&c&anceller
Xref: news.uni-stuttgart.de control:40719311

Roedy Green

unread,
Jan 27, 2002, 9:02:07 PM1/27/02
to
On Mon, 21 Jan 2002 00:50:34 -0600, "John Devine" <jde...@purdue.edu>
wrote or quoted :

>I'm kind of new to java and I'd like to know how to write and if/else loop


>on the basis of a double being positve or negative....
>
>Can someone help me?

if (d < 0) doNegative();
if (d > 0) doPositive();

See the java cheat sheet in the Java glossary for how to do loops.

--


eagerly seeking telecommuting programming work.
canadian mind products, roedy green
the java glossary is at
http://www.mindprod.com/gloss.html
or http://209.139.205.39

========= WAS CANCELLED BY =======:
From: Roedy Green <ro...@mindprod.com>
Control: cancel <j5c95u8ennakq5jdf...@4ax.com>
Subject: cmsg cancel <j5c95u8ennakq5jdf...@4ax.com>
Date: Mon, 28 Jan 2002 01:32:28 GMT
Message-ID: <cancel.j5c95u8ennakq...@4ax.com>


X-No-Archive: yes
Newsgroups: microsoft.test,alt.flame.niggers,comp.lang.java.help
NNTP-Posting-Host: w088.z064003087.lax-ca.dsl.cnc.net 64.3.87.88
Lines: 1

Path: news.uni-stuttgart.de!dns.phoenix-ag.de!news.csl-gmbh.net!news.stealth.net!msrtrans1!msrnewsc1!cppssbbsa01.microsoft.com!tkmsftngp01!tkmsftngp04!u&n&a&c&anceller
Xref: news.uni-stuttgart.de control:40719637

0 new messages