# Other [quiz] Compare signs

Discussion in 'Android Questions' started by Erel, Oct 17, 2018.

We want to check whether the two values are either non-negative or negative.

Code:
`If (Func1 >= 0 And Func2 >= 0) Or (Func1 < 0 And Func2 < 0) Then Log("Good!")Else Log("Bad")End IfSub Func1 As Int'...End SubSub Func2 As int'...End Sub`
How can we avoid calling each sub twice? Without adding any variable.

Peter Simpson likes this.
2. ### JordiCPWell-Known MemberLicensed User

My guess
Code:
`if Bit.XOR(Func1,Func2)>=0 Then   Log("Good!")Else   Log("Bad")End if`

Nice solution! Not the one I thought of and actually more clever

Peter Simpson and JordiCP like this.

Quiz still open for another solution. The other solution also works with Float and Double types.

5. ### alwaysbusyExpertLicensed User

my try:

Code:
`If (func1 < 0) = (func2 < 0) Then       Log("good")Else       Log("bad")End If`

Star-Dust, Erel and JordiCP like this.
6. ### Star-DustExpertLicensed User

Code:
`If (Func1 * Func2 >= 0) Then Log("Good!")Else Log("Bad")End If`
Or
Code:
`If (Func1 / Func2 >= 0) Then Log("Good!")Else Log("Bad")End If`

alwaysbusy likes this.

That was my solution as well.

This solution is not 100% correct.

It has two problems:
- Not correct when one of the subs returns 0.
- Multiplying two large positive numbers will overflow and result with a negative value.

Johan Hormaza and alwaysbusy like this.
8. ### Guardian17Active MemberLicensed User

My try, assuming "<>" means "not equal to" (though I'm not at my B4A computer to try it):
Code:
`If (Func1 <> 0 And  Func2 <> 0) Then Log("Good!")Else Log("Bad")End If`

9. ### alwaysbusyExpertLicensed User

this will return "good" for e.g. func1=2 and func2=-2, although the signs do not match.

10. ### RandomCoderWell-Known MemberLicensed User

I love these quizzes that @Erel presents every now and then. I'd have done Func1 * Func2 as already suggested by @Star-Dust but I see that Erel has found a potential flaw in doing it this way.
So how about just utilising the sign bit....
Code:
`If Bit.Xor(Func1,Func2) >= 0 Then   Log("Good")Else   Log("Bad")End If`
It'll be a first if I have the answer right!

11. ### MaharesWell Known MemberLicensed User

I hate to rain on your parade @RandomCoder, but @JordiCP already offered that in the second post

12. ### RandomCoderWell-Known MemberLicensed User

Damn it I missed the spoiler button
Ah well at least I'm happy in the knowledge that I've not cheated.
I see that Erel has another possible solution that also works for floating point numbers. I'll have to put my thinking cap on again!

EDIT... I have just seen that the solution has already been given.
Code:
`If (func1 < 0) = (func2 < 0) Then       Log("good")Else       Log("bad")End If`
I really should give more time to read posts properly

Last edited: Oct 17, 2018
Erel and JordiCP like this.