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

Home » Public Forums » archive » Re: SIGN function: signof(num or array of num)
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: signof(num or array of num) [message #70131] Tue, 16 March 2010 09:41
Foldy Lajos is currently offline  Foldy Lajos
Messages: 268
Registered: October 2001
Senior Member
On Tue, 16 Mar 2010, Ding wrote:

> although signof(0) cause algorithm error, since 0/0 is defined as 0,
> signof(0)=0, signof(Inf)=0. it can be used in an expression
> signof(numeric variable or array)*10.0/5. etc.

0/0 is undefined in mathematics.

regards,
lajos
Re: SIGN function: signof(num or array of num) [message #70132 is a reply to message #70131] Tue, 16 March 2010 09:36 Go to previous message
Carsten Lechte is currently offline  Carsten Lechte
Messages: 124
Registered: August 2006
Senior Member
I wrote:
> One would have to extract the sign bit (assuming IEEE format,
> taking into account endianness, precision etc.) and base the
> calculation on that.

Alternatively, one could read the documentation and discover the
SIGN keyword to the FINITE() function ;-)


chl
Re: SIGN function: signof(num or array of num) [message #70133 is a reply to message #70132] Tue, 16 March 2010 09:25 Go to previous message
Carsten Lechte is currently offline  Carsten Lechte
Messages: 124
Registered: August 2006
Senior Member
Ding wrote:

> IDL> print,!version
> { x86_64 linux unix linux 6.4 Apr 26 2007 64 64}

Mine is:
{ x86 linux unix linux 6.4 Apr 26 2007 32 64}
and I confirmed your results on a 64bit machine.


chl
Re: SIGN function: signof(num or array of num) [message #70134 is a reply to message #70133] Tue, 16 March 2010 09:17 Go to previous message
Ding is currently offline  Ding
Messages: 20
Registered: March 2008
Junior Member
On Mar 16, 3:46 pm, Carsten Lechte <c...@toppoint.de> wrote:
> Ding wrote:
>> IDL> a=[[1,0][-1,0]]
>> IDL> print,signof(a)
>>      1  0
>>     -1  0
>
> Still, you should mention under RESTRICTIONS that the
> input must be an integer type:
>
> IDL> a=[[1,0],[-1,0]]
> IDL> print, signof(a)
>         1       0
>        -1       0
> % Program caused arithmetic error: Integer divide by 0
> IDL> print, signof(double(a))
>         1  -32768
>        -1  -32768
> % Program caused arithmetic error: Floating illegal operand
> IDL> print, signof(complex(a))
>         1  -32768
>        -1  -32768
> % Program caused arithmetic error: Floating illegal operand
>
> chl

This is all the information I have, including the !version:

IDL> print,!version
{ x86_64 linux unix linux 6.4 Apr 26 2007 64 64}
IDL> print,a
1 0
-1 0
IDL> print,double(a)
1.0000000 0.0000000
-1.0000000 0.0000000
IDL> print,complex(a)
( 1.00000, 0.00000)( 0.00000, 0.00000)
( -1.00000, 0.00000)( 0.00000, 0.00000)
IDL> print,signof(a)
1 0
-1 0
% Program caused arithmetic error: Integer divide by 0
IDL> print,signof(double(a))
1 0
-1 0
% Program caused arithmetic error: Floating illegal operand
IDL> print,signof(complex(a))
1 0
-1 0
% Program caused arithmetic error: Floating illegal operand
IDL>

Let's compare the version of the result and see if it is IDL or
processor that cause the difference!
Re: SIGN function: signof(num or array of num) [message #70136 is a reply to message #70134] Tue, 16 March 2010 08:46 Go to previous message
Carsten Lechte is currently offline  Carsten Lechte
Messages: 124
Registered: August 2006
Senior Member
Ding wrote:
> IDL> a=[[1,0][-1,0]]
> IDL> print,signof(a)
> 1 0
> -1 0

Still, you should mention under RESTRICTIONS that the
input must be an integer type:

IDL> a=[[1,0],[-1,0]]
IDL> print, signof(a)
1 0
-1 0
% Program caused arithmetic error: Integer divide by 0
IDL> print, signof(double(a))
1 -32768
-1 -32768
% Program caused arithmetic error: Floating illegal operand
IDL> print, signof(complex(a))
1 -32768
-1 -32768
% Program caused arithmetic error: Floating illegal operand


chl
Re: SIGN function: signof(num or array of num) [message #70137 is a reply to message #70136] Tue, 16 March 2010 08:43 Go to previous message
Ding is currently offline  Ding
Messages: 20
Registered: March 2008
Junior Member
On Mar 16, 10:51 am, Ding <gardener_2...@hotmail.com> wrote:
> Dear IDL users,
>
> I cannot find the SIGN function in IDL procedures, but one in
> Solarsoft ssw/maths/ SIGN which occupy the name, but different
> purpose( sign(a)*b). so I wrote the SIGNOF() function which is to get
> the sign of a number or array,
>
> Hopefully I am not repeating someone' work! you are welcome to
> comments!
>
> ;    return the sign of the input data in an array of the same size
> ;    -1 for negative elements
> ;    1 for positive elements
> ;    0 for 0 and complex number
> ;    only works for numerics

return the sign of the input data in an array of the same size
-1 for negative elements
1 for positive elements
0 for 0 and complex number
only works for numerics
>a=[[1,-2],[0,3]]
> print, signof(a)
1 -1
0 1
>
More notes:

although signof(0) cause algorithm error, since 0/0 is defined as 0,
signof(0)=0, signof(Inf)=0. it can be used in an expression
signof(numeric variable or array)*10.0/5. etc.
Re: SIGN function: signof(num or array of num) [message #70138 is a reply to message #70137] Tue, 16 March 2010 08:35 Go to previous message
Carsten Lechte is currently offline  Carsten Lechte
Messages: 124
Registered: August 2006
Senior Member
H. Evans wrote:
> Could be either + or -ve:
> IDL> print,!values.f_nan, - !values.f_nan
> NaN -NaN

Then, a totally different approach might be necessary to get
it completely right.

FINITE() tells you if you have a NaN, but since every
comparison that involves NaNs evaluates to FALSE, you cannot
find out if it's + or -NaN be equating to !VALUES.F_NAN.

One would have to extract the sign bit (assuming IEEE format,
taking into account endianness, precision etc.) and base the
calculation on that.

For my private version, I will add a term + ( FINITE( x) EQ 0)
so all NaNs are assumed positive, since the one value that
SIGNUM( NaN) should not have is 0.

I wonder in what situation one would care about the correct sign
of something that is not a number.


chl
Re: SIGN function: signof(num or array of num) [message #70139 is a reply to message #70138] Tue, 16 March 2010 08:32 Go to previous message
Ding is currently offline  Ding
Messages: 20
Registered: March 2008
Junior Member
On Mar 16, 1:31 pm, FÖLDY Lajos <fo...@rmki.kfki.hu> wrote:
> On Tue, 16 Mar 2010, Ding wrote:
>> Dear IDL users,
>
>> I cannot find the SIGN function in IDL procedures, but one in
>> Solarsoft ssw/maths/ SIGN which occupy the name, but different
>> purpose( sign(a)*b). so I wrote the SIGNOF() function which is to get
>> the sign of a number or array,
>
>> Hopefully I am not repeating someone' work! you are welcome to
>> comments!
>
>> return, fix(abs(input)/input)
>
> IDL> input=0 & help, fix(abs(input)/input)
> <Expression>    INT       =        0
> % Program caused arithmetic error: Integer divide by 0
>
> I think SIGNOF() should not cause an arithmetic error for finite numbers.
>
> regards,
> lajos

This is expected in my algorithm, as the sign of 0 is considered to be
0. although there is an error in the IDL language, however 0/0=0, is
still true. the power of this in array calculation

IDL> a=[[1,0][-1,0]]
IDL> print,signof(a)
1 0
-1 0

Ding Yuan
Re: SIGN function: signof(num or array of num) [message #70140 is a reply to message #70139] Tue, 16 March 2010 07:30 Go to previous message
H. Evans is currently offline  H. Evans
Messages: 18
Registered: December 2009
Junior Member
On Mar 16, 2:57 pm, Carsten Lechte <c...@toppoint.de> wrote:
> F LDY Lajos wrote:
>> I think SIGNOF() should not cause an arithmetic error for finite numbers.
>
> I use the expression
>
> (x GT 0) -1* (x LT 0)
>
> to get the sign of e.g.
>
> x = [ -2e4, 5, 0, 1d/0, -1d/0, 0d/0 ]
>
> but it uses 2 comparisons and even a multiplication. Does anyone have
> a simpler/faster version? Furthermore, what should the sign of NaN be?
>
> chl

Could be either + or -ve:
IDL> print,!values.f_nan, - !values.f_nan
NaN -NaN
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to read data with format codes?
Next Topic: Predefined iTool Visualization in existing iTool

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

Current Time: Wed Oct 08 11:35:31 PDT 2025

Total time taken to generate the page: 0.00829 seconds