Your challenge for this month’s Exercise is to write a sign() function, which returns 1, 0, or -1 depending on the sign of an integer. A relatively simple thing to code — I hope!
For my solution, the sign() function uses two comparisons. Assuming a
is the value passed:
if( a<0 ) return(-1);
If a
is less than zero, -1 is returned. Otherwise:
if( a>0 ) return(1);
When a
is greater than zero, 1 is returned.
The only option left is zero, which is handled by a single return statement:
return(0);
Here is the full code:
2021_03-Exercise.c
#include <stdio.h> int sign(int a) { /* value is negative */ if( a<0 ) return(-1); /* value is positive */ if( a>0 ) return(1); /* value is zero */ return(0); } int main() { int values[20] = { -34, 27, 0, 48, -25, 28, -55, 66, -17, -78, 0, -20, 40, -98, 63, -44, 59, 6, 65, 90 }; int x; for( x=0; x<20; x++ ) { printf("Sign of %3d is ", values[x] ); switch( sign( values[x] )) { case -1: puts("negative"); break; case 1: puts("positive"); break; case 0: puts("zero"); break; default: puts("unknown"); } } return(0); }
The output reflects the value returned by the sign() function; text is appended to the value reflecting the proper sign: negative, positive, or zero:
Sign of -34 is negative
Sign of 27 is positive
Sign of 0 is zero
Sign of 48 is positive
Sign of -25 is negative
Sign of 28 is positive
Sign of -55 is negative
Sign of 66 is positive
Sign of -17 is negative
Sign of -78 is negative
Sign of 0 is zero
Sign of -20 is negative
Sign of 40 is positive
Sign of -98 is negative
Sign of 63 is positive
Sign of -44 is negative
Sign of 59 is positive
Sign of 6 is positive
Sign of 65 is positive
Sign of 90 is positive
I hope you found the solution easy and enjoyed the challenge. Remember, my solution represents only one approach; as long as your sign() function behaves similarly, it’s good.
A sign() function comes in handy, which is probably why the BASIC language featured the SGN
command that performs the same feat. This function is used in an upcoming Lesson where knowing the sign of an integer plays an important role.
Please make the exercises a little harder.
Okay! Remember, I mix it up because not everyone here is at the same understanding level. But I can toss in a difficult Exercise, no problem. 🙂
Due to the way signed numbers are represented in binary you can have +0 and -0 which in JavaScript at least are different, ie -0 == +0 evaluates to false. Not sure how other languages handle this. Of course there is no mathematical significance to this as 0 is neither – or +.
(JavaScript always gives the impression of being thrown together in a hurry by just one person. No idea why!)
Interesting question, Chris. I’m unfamiliar with Java, but I just ran a test in C to set the sign bit for a zero value. It had no effect on comparison or output. I’ll run more tests and do a Lesson on this topic later. Thanks!
Oh! I goofed. Signed zero is different from zero in C. I’ll cover more later.
This is a link to an article on Medium about JavaScript but some of the information is general. (It’s a subscription site but anyone without a subscription can read 5 articles a month free. Hope you don’t mind me posting this link but please go ahead and delete it if not.)
https://medium.com/coding-at-dawn/is-negative-zero-0-a-number-in-javascript-c62739f80114