comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: SIGN function?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: SIGN function? [message #6055] Wed, 03 April 1996 00:00
agraps is currently offline  agraps
Messages: 35
Registered: September 1994
Member
llobet@elpp1.epfl.ch (Xavier Llobet i Sales EPFL-CRPP 1015 Lausanne CH) writes:


> Maybe today I am specially dumb, but I cannot find the IDL (version 3.6.1)
> function that gives the sign of a number (I suppose it exists!!!).

I don't know. I wrote my own, attached below. It works for a number
of cases, but there are no guarantees.

Amara
-----------------------

function SIGN, num
;+
;NAME:
; SIGN
;
;PURPOSE:
; To compute the sign of a number. This function mimics
; Matlab's sign function.
;
;CALLING SEQUENCE:
; Result = SIGN(num)
;
;INPUTS:
; Num: Any number or array
;
;OUTPUTS:
; SIGN returns either 1, 0, or -1.
;
;MODIFICATION HISTORY:
; Amara Graps, BAER, December 1994.
; (c) copyright Amara Graps 1995, 1996.
;-

t = SIZE(num)
testtype = t(0)

IF testtype NE 0 THEN BEGIN
;The value num is an array.. Find the sign of ALL of the elements
;create same size and kind of array
n_ele = N_ELEMENTS(t)
CASE t(n_ele-2) OF
2: sgn = num * 0 ;integer
3: sgn = num * 0L ;long integer
4: sgn = num * 0.0 ;floating
5: sgn = num * 0.0D0 ;double floating
ELSE: print, 'Not a good array type in sign!'
ENDCASE
copy_num= num

;neg values
indx = WHERE(copy_num LT 0)
;check for bad index
ts = SIZE(indx)
CASE 1 OF
ts(0) EQ 0: ;no change
ELSE: sgn(indx) = -1
ENDCASE

;positive values
indx = WHERE(copy_num GT 0)
;check for bad index
ts = SIZE(indx)
CASE 1 OF
ts(0) EQ 0: ;no change
ELSE: sgn(indx) = +1
ENDCASE

;zero values
indx = WHERE(copy_num EQ 0)
;check for bad index
ts = SIZE(indx)
CASE 1 OF
ts(0) EQ 0: ;no change
ELSE: sgn(indx) = 0
ENDCASE


ENDIF ELSE BEGIN

;t(0) = 0, scalar.. what we want

IF t(1) NE 6L then begin
;Not a complex scalar

CASE 1 OF
num LT 0: sgn = -1
num EQ 0: sgn = 0
num GT 0: sgn = 1
ELSE: print, 'Not a valid number!'
ENDCASE
ENDIF ELSE BEGIN
;This is a complex scalar
sgn = num / abs(num)
END ;END
END

RETURN, sgn

END

--

************************************************************ *************
Amara Graps email: agraps@netcom.com
Computational Physics vita: finger agraps@best.com
Multiplex Answers URL: http://www.amara.com/
************************************************************ *************
"Picture a massless particle." --A Koan of Modern Physics
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL bus errors
Next Topic: Re: linked lists?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:16:50 PDT 2025

Total time taken to generate the page: 0.00484 seconds