Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Math.min and Math.max for byte / short

Reply
Thread Tools

Math.min and Math.max for byte / short

 
 
Philipp
Guest
Posts: n/a
 
      07-21-2008
Hello,
Why is there no Math.min(byte,byte) and Math.max(byte,byte) (and
equivalent for short) ?

This results in casting all the time... beuh.
Phil
 
Reply With Quote
 
 
 
 
conrad@lewscanon.com
Guest
Posts: n/a
 
      07-21-2008
Philipp wrote:
> >Why is there no Math.min(byte,byte) and Math.max(byte,byte) (and
> >equivalent for short) ?

>
> >This results in casting all the time... beuh.


rossum wrote:
> The casting is inevitable. *The '<' operator is not defined for
> anything smaller than int, so the compiler will widen any byte or
> short operands to int anyway.


Also, OP, why does the casting bother you? It takes no additional
source code and doesn't have a speed penalty.

For further insight, check out the JLS on numeric promotions and
binary numeric conversions.

--
Lew
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-21-2008
On Mon, 21 Jul 2008 08:06:45 -0700 (PDT), http://www.velocityreviews.com/forums/(E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>Also, OP, why does the casting bother you? It takes no additional
>source code and doesn't have a speed penalty.


Would you not write

byte c = (byte) Math.min ( a, b );

How do you pull it off without additional source code, namely the
(byte)?

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-21-2008
On Jul 21, 4:42*pm, Roedy Green <(E-Mail Removed)>
wrote:
> On Mon, 21 Jul 2008 08:06:45 -0700 (PDT), (E-Mail Removed) wrote,
> quoted or indirectly quoted someone who said :
>
> >Also, OP, why does the casting bother you? *It takes no additional
> >source code and doesn't have a speed penalty.

>
> Would you not write
>
> byte c *= (byte) Math.min ( a, b );
>
> How do you pull it off without additional source code, namely the
> (byte)?


Very good point, if you are assigning the result back to a byte
variable.

However, for other uses you might not need the cast back to byte. For
example, if the result is used in another step then it likely would be
widened to int again anyway, so you'd leave it there.

E.g.,
if ( Math.min( a, b ) > 0 ) ...

In this case, you would not bother casting the 'min()' result back to
byte.

--
Lew

 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      07-21-2008
On Jul 21, 11:10*pm, Eric Sosman <(E-Mail Removed)> wrote:
> Roedy Green wrote:
> > On Mon, 21 Jul 2008 08:06:45 -0700 (PDT), (E-Mail Removed) wrote,
> > quoted or indirectly quoted someone who said :

>
> > byte c * * = (byte) Math.min ( a, b );


> * * *The inventors of java.lang.Math *could* have provided
> byte-valued overloads of min, max, etc:
> * * *I guess they just didn't feel it was important enough.


Hmm. Oh well. They provided it for double float int and long. I guess
they could have made the effort to type the three missing ones eh?

> If they were wrong in the O.P.'s view, he can always write
> his own min/max methods for byte and short.


That's what I obviously did.

Thanks for your asnwers anyway.
(I thought there was some more elaborate logic below this than "they
didn't feel like it")
Phil
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-21-2008
Philipp wrote:
> On Jul 21, 11:10 pm, Eric Sosman <(E-Mail Removed)> wrote:
>> Roedy Green wrote:
>>> On Mon, 21 Jul 2008 08:06:45 -0700 (PDT), (E-Mail Removed) wrote,
>>> quoted or indirectly quoted someone who said :
>>> byte c = (byte) Math.min ( a, b );

>
>> The inventors of java.lang.Math *could* have provided
>> byte-valued overloads of min, max, etc:
>> I guess they just didn't feel it was important enough.

>
> Hmm. Oh well. They provided it for double float int and long. I guess
> they could have made the effort to type the three missing ones eh?


I think it may be a matter of consistency with arithmetic promotion. The
four types that are supported are the four types in which a Java
implementation does arithmetic.

Patricia
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-21-2008
Eric Sosman wrote:
> I guess they just didn't feel it was important enough.
> If they were wrong in the O.P.'s view, he can always write
> his own min/max methods for byte and short. For char and
> boolean, too, if it suits his fancy (can't use `<' and `>'
> on booleans, but Java has other operators to fill the bill).
>


public static boolean min(boolean a, boolean b) { return a & b; }
public static boolean max(boolean a, boolean b) { return a | b; }

The usefulness of this? I don't know. Actually, technically, boolean
values don't have an intrinsic ordering.
B={tautology, contradiction} isn't an ordered set.



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-22-2008
Roedy Green wrote:
> On Mon, 21 Jul 2008 08:06:45 -0700 (PDT), (E-Mail Removed) wrote,
> quoted or indirectly quoted someone who said :
>
>> Also, OP, why does the casting bother you? It takes no additional
>> source code and doesn't have a speed penalty.

>
> Would you not write
>
> byte c = (byte) Math.min ( a, b );
>
> How do you pull it off without additional source code, namely the
> (byte)?
>


I think it's been mentioned here on this list that the JVM and the
language spec require bytes to be promoted to ints for any kind of
arithmetic or logical operation. The Java language merely implements
what is possible on current JVMs in this respec. There is no operation
for comparing bytes in JVMs, so you might as well promote then cast it,
because that's what the JVM is going to do.

Whether it's truly correct for Java to follow the JVM capabilities so
closely in this regard is another debate. I think that's why there are
no byte or char versions of min or max.
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-22-2008
Eric Sosman wrote:

>
> Unnh, what? The result of the comparison is not an int,
> but a boolean, and you almost certainly don't want to cast the
> boolean to something else.


Oops, yeah I totally missed that. Thanks.
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-23-2008
rossum wrote:

>>
>>
>> </off-topic>

> "Alligator sandwich, and make it sn..." (courtesy of Terry Pratchett)
>
> rossum
>


I could really murder a curry right now.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
converting int and short to byte array and vice versa carmen Java 4 01-12-2010 05:00 PM
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM



Advertisments