Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31451

Re: Simple if-exercises

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Simple if-exercises
Date 2015-04-03 21:45 +0000
Organization A noiseless patient Spider
Message-ID <mfn1l9$f11$1@dont-email.me> (permalink)
References (2 earlier) <mflc17$mdd$1@Gaia.teknon.de> <mflq2e$nsq$1@dont-email.me> <mfmkp4$3ga$1@Gaia.teknon.de> <mfmpq3$2tt$2@dont-email.me> <mfmrs9$srh$1@dont-email.me>

Show all headers | View raw


On Fri, 03 Apr 2015 16:07:23 -0400, Eric Sosman wrote:

> On 4/3/2015 3:31 PM, Martin Gregorie wrote:
>>[...]
>> But in any case using Math.signum() looks kind of messy by the time
>> you've called the static function and then coerced its result to
>> integer:
>>
>> switch((int)Math.signum(n))
>> {
>>     case -1:
>>     case 0:  s = "Negative-or-equal"; break;
>>     case +1: s = "Positive"; break; default: s = "Defaulted";
>> }
> 
>      Note that the "default:" case will never execute.
> Math.signum(n) can return Double.NaN (or Float.NaN, depending on the
> type of "n"), but converting a NaN to int yields zero.
> JLS 5.1.3.
>
Of course. In the usual situations where I use a switch statement my only 
use for default is to trap errors, so this is best regarded as a token 
error trap. I did write a test program to check the Math.signum() 
behaviour which looped round something like:

for each command-line argument
  switch((int)Math.signum(Integer.parseInt(arg[i])))
  {
    case list
    default statement
  }
  print the result

so in those circumstances I'd argue that NOT using default would be poor 
practice.
 

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Simple if-exercises Jeff Higgins <jeff@invalid.invalid> - 2015-04-02 09:58 -0400
  Re: Simple if-exercises Jeff Higgins <jeff@invalid.invalid> - 2015-04-02 10:53 -0400
    Re: Simple if-exercises Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-02 15:08 -0400
  Re: Simple if-exercises Gene Wirchenko <genew@telus.net> - 2015-04-02 16:46 -0700
  Re: Simple if-exercises v_borchert@despammed.com (Volker Borchert) - 2015-04-03 06:29 +0000
    Re: Simple if-exercises Martin Gregorie <martin@address-in-sig.invalid> - 2015-04-03 10:29 +0000
      Re: Simple if-exercises v_borchert@despammed.com (Volker Borchert) - 2015-04-03 18:05 +0000
        Re: Simple if-exercises Martin Gregorie <martin@address-in-sig.invalid> - 2015-04-03 19:31 +0000
          Re: Simple if-exercises Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-03 16:07 -0400
            Re: Simple if-exercises Martin Gregorie <martin@address-in-sig.invalid> - 2015-04-03 21:45 +0000
          Re: Simple if-exercises v_borchert@despammed.com (Volker Borchert) - 2015-04-03 23:49 +0000
    Re: Simple if-exercises Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-03 07:38 -0400
      Re: Simple if-exercises Jeff Higgins <jeff@invalid.invalid> - 2015-04-03 20:06 -0400

csiph-web