Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ucla-cs!ucla-ma!euphemia!pmontgom From: pmontgom@euphemia.math.ucla.edu (Peter Montgomery) Newsgroups: comp.lang.fortran Subject: Re: MAX function, but carry sign Message-ID: <1991Apr12.194835.9162@math.ucla.edu> Date: 12 Apr 91 19:48:35 GMT References: <282@venice.water.ca.gov> <1991Apr12.151728.12916@craycos.com> Sender: news@math.ucla.edu Organization: UCLA Mathematics Dept. Lines: 33 In article <1991Apr12.151728.12916@craycos.com> jrbd@craycos.com (James Davies) writes: >In article <282@venice.water.ca.gov> rfinch@caldwr.water.ca.gov (Ralph Finch) writes: >>I wish to use the max function on absolute values, but carry the sign >>of the original values. That is: >>a=max(abs(a1),abs(a2)) ! but a has sign of either a1 or a2 >If you want the result to carry the sign of the maximum value >(which is the most likely case), then you could use the sign of the sum >of the two, i.e. > a = sign(max(abs(a1),abs(a2)), a1+a2) >This will give you the positive value if they are the same, but the sign >of the larger absolute value if they are different. Another way uses the Fortran Extended MERGE function, which adds a much-needed conditional expression construct to Fortran. As I recall, MERGE(x, y, bool) returns x if bool is TRUE, y if bool is FALSE. Then a = MERGE(a1, a2, ABS(a1).ge.ABS(a2)) . This makes it obvious to a reader that a = a1 or a = a2. Depending upon your system, it may be preferable to replace the last test by the mathematically equivalent a1**2 .ge. a2**2 (assuming no overflow/underflow will occur) or by (a1+a2)*(a1-a2).ge.0.0 . -- Peter L. Montgomery pmontgom@MATH.UCLA.EDU Department of Mathematics, UCLA, Los Angeles, CA 90024-1555 If I spent as much time on my dissertation as I do reading news, I'd graduate.