why does the calculation of the sign of product of two variables use absolute values?

Fortran has never specified a function like signs_differ(x,y), so one generally implements such a thing personally.

x*y<0 (and x*y.lt.0) is not asking the same thing as “are x and y of different sign?”. While the product of x and y being positive means x and y are the same sign in the (mathematical) real numbers, this is not true for (computational) floating point numbers.

Floating point multiplication x*y may overflow, result in a signed infinite value (raising a IEEE flag) with the comparison returning the expected logical value, but that isn’t always true. There were many non-IEEE systems and IEEE systems may see that flag being raised and abort (or have some expensive handling diversion). That’s totally not the same thing as “do x and y have the same sign?”.

x*(y/dabs(y)) doesn’t overflow, is “portable” and is potentially cheaper than (x/dabs(x))*(y/dabs(y)) – ignoring the issues surrounding dabs() and signed zeros.

Modern Fortran has functions such as sign, ieee_copy_sign and ieee_signbit which didn’t exist 40 years ago.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top