What does function SIGN(double x,double y) do in C++ language and how to translate it to c#
-
its not a sign(double) whic returns sign of number this one takes two arguments and I can't find it nowhere on the net – Милош Чолић Jun 21 '12 at 21:58
-
Maybe because it itsn't a standard C++ function. – mfontanini Jun 21 '12 at 21:59
-
Which C++ library are you using? – MiJyn Jun 21 '12 at 22:02
-
@thb It's lacking important information. Since it's not a standard function, the question can so far only be answered "Whatever the coder made it do, and for translating it, first find out what it does". Or with wild guesses. – Daniel Fischer Jun 21 '12 at 22:15
3 Answers
A function like that exists in Fortan http://gcc.gnu.org/onlinedocs/gfortran/SIGN.html
SIGN(A,B) returns the value of A with the sign of B.
If B >= 0 then the result is ABS(A), else it is -ABS(A).
Possibly someone implemented the same function in C++ (but something custom, not a standard part of C++ or the standard libraries).
A c# version of that would be
A = Math.Abs(A);
if (B<0.0) A = -A;

- 15,919
- 5
- 47
- 57
-
If `A = B = -1` then `A * Math.Sign(B)` will not have the same sign as `B`. – aioobe Jun 21 '12 at 22:07
-
-
1
-
-
Math.Sign won't work because it returns 0 if B == 0. – hatchet - done with SOverflow Jun 21 '12 at 22:13
-
It's a good idea to either clearly document the preconditions or otherwise throw an exception if `B >= 0` and `A = MIN_VAL` though. Just looked it up: `Math.Abs` in C# does already throw an exception (good thing, java got that wrong), so the code is wrong for one corner case but handles the other fine. – Voo Jun 21 '12 at 22:23
-
@Voo - I can see that with integral types, but I don't think that would be an issue for double type inputs since Double.MaxValue = Abs(Double.MinValue). – hatchet - done with SOverflow Jun 21 '12 at 22:29
-
@hatchet Ah I see - I thought we were talking about ints here (didn't notice the `< 0.0` check). Yep works fine for doubles. – Voo Jun 21 '12 at 22:31
In the C++ code you are translating to C#, look for a macro definition or inline function definition for SIGN
. The definition should spell out exactly how you should implement it in C#.
If I had to guess from the name, SIGN(x,y)
returns true if x
and y
have the same sign, and false otherwise.

- 69,070
- 8
- 110
- 193
The C++ standard library has no SIGN()
function. Your question does not give enough information for me to tell you for certain what this SIGN()
is, but C++ succeeds the older programming language C, and my informed hunch is that SIGN()
is a preprocessor macro defined in the old C style.
Search your code for a line that begins #define SIGN
, possibly with more than one space between the #define
and the SIGN
. If you find the line, it is likely to tell you what your SIGN()
is and does.

- 13,796
- 3
- 40
- 68