Other [quiz] Compare signs

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

  1. Erel

    Erel Administrator Staff Member Licensed User

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

    Code:
    If (Func1 >= 0 And Func2 >= 0Or (Func1 < 0 And Func2 < 0Then
     
    Log("Good!")
    Else
     
    Log("Bad")
    End If

    Sub Func1 As Int
    '...
    End Sub
    Sub Func2 As int
    '...
    End Sub
    How can we avoid calling each sub twice? Without adding any variable.
     
    Peter Simpson likes this.
  2. JordiCP

    JordiCP Well-Known Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    Nice solution! Not the one I thought of and actually more clever :)
     
    Peter Simpson and JordiCP like this.
  4. Erel

    Erel Administrator Staff Member Licensed User

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

    alwaysbusy Expert Licensed User

    my try:

    Code:
    If (func1 < 0) = (func2 < 0Then
           
    Log("good")
    Else
           
    Log("bad")
    End If
     
    Star-Dust, Erel and JordiCP like this.
  6. Star-Dust

    Star-Dust Expert Licensed User

    Code:
    If (Func1 * Func2 >= 0Then
     
    Log("Good!")
    Else
     
    Log("Bad")
    End If
    Or
    Code:
    If (Func1 / Func2 >= 0Then
     
    Log("Good!")
    Else
     
    Log("Bad")
    End If
     
    alwaysbusy likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    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. Guardian17

    Guardian17 Active Member Licensed 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 <> 0Then
     
    Log("Good!")
    Else
     
    Log("Bad")
    End If
     
  9. alwaysbusy

    alwaysbusy Expert Licensed User

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

    RandomCoder Well-Known Member Licensed 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. Mahares

    Mahares Well Known Member Licensed User

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

    RandomCoder Well-Known Member Licensed User

    Damn it I missed the spoiler button :oops:
    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 < 0Then
           
    Log("good")
    Else
           
    Log("bad")
    End If
    I really should give more time to read posts properly :oops:
     
    Last edited: Oct 17, 2018
    Erel and JordiCP like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice