Reputation: 271
Exists there a function in Julia that is equivalent to the SIGN(A,B)
function in Fortran?
SIGN(A,B)
in Fortran returns the value of A
with the sign of B
. If B > 0
then the result is ABS(A)
, else it is -ABS(A)
. For instance, SIGN(10,-1)
would give you -10
.
Upvotes: 1
Views: 233
Reputation: 69949
You have copysign
function that does what you want.
The only distinction from what you have specified is that copysign(1.0, 0.0)
return 1.0
and you specified it to be -1.0
(by saying B>0
), but I guess you want this to evaluate to 1.0
like copysign
behaves. Right?. Note that copysign(1.0, -0.0)
is -1.0
.
Upvotes: 4
Reputation: 18580
EDIT: Check Bogumil's answer. Turns out there is a function in Base
that does this.
I don't think there is a function in Base
that does this. However, adding a new method to sign
is an easy solution:
Base.sign(a, b) = abs(a)*sign(b)
You can use methods(sign)
to check that your new method won't clash with any existing methods. In this case, it definitely won't since all existing methods only have a single input.
The behaviour of the new method will also be consistent with all existing input types, eg sign(missing, -1)
will return missing
, and sign(true, false)
will return false
.
Alternatively, you could come up with your own new function name.
Upvotes: 2