Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.fortran

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-14-2012, 03:38 PM
Phillip Helbig---undress to reply
Guest
 
Posts: n/a
Default SIGN

SIGN(A,B) is defined as ABS(A)*sgn(B) where sgn(B) is 1 if B >= 0 and -1
if B < 0.

It seems to me it would be simpler to define it with just one argument,
i.e. like "sgn" above.

Presumably there is a reason why this is not the case. What is it?

One possible reason would be that there are common situations where the
definition would make for more compact code, but I find that it makes
for less compact code.

I could see the point if it were defined as A*sgn(B), i.e. instead of
multiplying a quantity by the sign function we have the quantity as the
first argument, but that is not the case.

Consider the following (real-example) code (bonus points if you can
guess what it is for). OMEGA is always >=0, LAMBDA is unrestricted and
K = OMEGA + LAMBDA - 1. (Thus the limit for LAMBDA=0 and K=0 should be
infinity, right?)

ALPHA = SIGN(1,K)*27*OMEGA**2*LAMBDA/(4*K**3) !K=0 handled separately

To me, it would make more sense to write

ALPHA = SIGN(27*OMEGA**2*LAMBDA/(4*K**3),K) !K=0 handled separately

but that is not how it works.

Does anyone have a real-life example which indicates WHY the SIGN
function is defined as it is?

The first example above is what is required. This leads to ALPHA being
positive if LAMBDA is. IN THIS PARTICULAR CASE, I could then write

ALPHA = SIGN(27*OMEGA**2*LAMBDA/(4*K**3),LAMBDA)

and get the desired result, but such a transformation is of course not
always possible. In general I find that the definition of SIGN leads to
less compact, not more compact, code.

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-14-2012, 04:05 PM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: SIGN

Phillip Helbig---undress to reply <helbig@astro.multiclothesvax.de> wrote:

> SIGN(A,B) is defined as ABS(A)*sgn(B) where sgn(B) is 1 if B >= 0 and -1
> if B < 0.


> It seems to me it would be simpler to define it with just one argument,
> i.e. like "sgn" above.


> Presumably there is a reason why this is not the case. What is it?


As far as I know, it has always been that way back to Fortran I.

It might have to do with the instructions available on the 704.
(Which is a sign-magnitude machine, if that matters.)

-- glen
Reply With Quote
  #3 (permalink)  
Old 04-14-2012, 05:31 PM
Tim Prince
Guest
 
Posts: n/a
Default Re: SIGN

On 04/14/2012 11:38 AM, Phillip Helbig---undress to reply wrote:
> SIGN(A,B) is defined as ABS(A)*sgn(B) where sgn(B) is 1 if B>= 0 and -1
> if B< 0.
>
> It seems to me it would be simpler to define it with just one argument,
> i.e. like "sgn" above.
>
> Presumably there is a reason why this is not the case. What is it?
>


Back then, probably no one wanted to tackle optimizing around an
expensive multiplication for this case.

> Does anyone have a real-life example which indicates WHY the SIGN
> function is defined as it is?
>


Most "real-life examples" are coded under the early Cray influence,
where SIGN was the only Fortran intrinsic which could be employed to
vectorize conditionals. MERGE has become widely available only in the
last 2 decades. I don't know whether you're asking for a justification
for SIGN to remain as it was historically without taking history into
account.
Reply With Quote
  #4 (permalink)  
Old 04-14-2012, 10:54 PM
Phillip Helbig---undress to reply
Guest
 
Posts: n/a
Default Re: SIGN

In article <9utqilFlgU1@mid.individual.net>, Tim Prince
<tim@nospamcomputer.org> writes:

> MERGE has become widely available only in the
> last 2 decades. I don't know whether you're asking for a justification
> for SIGN to remain as it was historically without taking history into
> account.


I just want to understand the history. Would MERGE offer a better
solution?

Reply With Quote
  #5 (permalink)  
Old 04-14-2012, 11:50 PM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: SIGN

Phillip Helbig---undress to reply <helbig@astro.multiclothesvax.de> wrote:
> In article <9utqilFlgU1@mid.individual.net>, Tim Prince
> <tim@nospamcomputer.org> writes:


>> MERGE has become widely available only in the
>> last 2 decades. I don't know whether you're asking for a justification
>> for SIGN to remain as it was historically without taking history into
>> account.


> I just want to understand the history. Would MERGE offer a better
> solution?


In most cases, MERGE has different uses.

SIGN goes back to at least Fortran II, when it would have been
called SIGNF.

Note that on sign-magnitude machines, it can be done by just replacing
the sign bit with the sign bit from another register.

Manuals for the instruction set of the 704 and 709 are available.

I believe that there is some work toward running IBSYS and the
Fortran II compiler on emulated 7090s. It would then be possible
to compiler a program using SIGNF or XSIGNF and see the generated
code. (The 704 through 7094 are sign magnitude in both fixed
and floating point.)

For extra challenge, someone should write a Fortran 90 compiler
for the 7090. (Too much to ask for 2003 or 2008.)

-- glen
Reply With Quote
  #6 (permalink)  
Old 04-15-2012, 12:24 AM
dpb
Guest
 
Posts: n/a
Default Re: SIGN

On 4/14/2012 10:38 AM, Phillip Helbig---undress to reply wrote:
> SIGN(A,B) is defined as ABS(A)*sgn(B) where sgn(B) is 1 if B>= 0 and -1
> if B< 0.
>
> It seems to me it would be simpler to define it with just one argument,
> i.e. like "sgn" above.
>
> Presumably there is a reason why this is not the case. What is it?
>
> One possible reason would be that there are common situations where the
> definition would make for more compact code, but I find that it makes
> for less compact code.
>
> I could see the point if it were defined as A*sgn(B), i.e. instead of
> multiplying a quantity by the sign function we have the quantity as the
> first argument, but that is not the case.

....

"Why" I've no insight on other than "because".

If it bugs you sufficiently, why not write a function that does work as
you wish?

--
Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 09:49 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.