Announcement

Collapse
No announcement yet.

Multiple Logical Operators

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Logical Operators

    I have been reviewing some extensive backtesting and realised that I may have had an error in my code.

    To simplify things I have one part that states (A || B && C) where A, B and C are different conditions that need to be true for a trade to be placed.

    I want the test to return true if A is true irrespective of B or C.

    If A is false I want the test to return true only if both B and C are true.

    I believe I should have coded it with extra brackets as (A || (B && C)).

    However I do not want to make the change and rerun the tests (which has taken me days to do) unless I have to.

    So based on how I currently have it coded what will the test return if A is false and either of B or C is false?

  • #2
    Re: Multiple Logical Operators

    richthomas

    So based on how I currently have it coded what will the test return if A is false and either of B or C is false?
    That will return false
    Alex


    Originally posted by richthomas
    I have been reviewing some extensive backtesting and realised that I may have had an error in my code.

    To simplify things I have one part that states (A || B && C) where A, B and C are different conditions that need to be true for a trade to be placed.

    I want the test to return true if A is true irrespective of B or C.

    If A is false I want the test to return true only if both B and C are true.

    I believe I should have coded it with extra brackets as (A || (B && C)).

    However I do not want to make the change and rerun the tests (which has taken me days to do) unless I have to.

    So based on how I currently have it coded what will the test return if A is false and either of B or C is false?

    Comment


    • #3
      your last statement is correct. You need

      if (A || (B && C)) {

      Remember algebra in school??

      You need to segment complex math/logic to control order.

      Sounds like you need to re-run your tests.

      Let me know if you need more help.
      Brad Matheny
      eSignal Solution Provider since 2000

      Comment


      • #4
        richthomas
        FWIW your code as is will do what you are expecting (see the screenshots attached to each condition)
        Alex

        I want the test to return true if A is true irrespective of B or C.




        If A is false I want the test to return true only if both B and C are true.


        So based on how I currently have it coded what will the test return if A is false and either of B or C is false?


        Comment


        • #5
          I believe there is one case that isn't tested in the code Alex did, and would result in incorrect test results:

          If A is true and B & C is false, my belief is this will return false. I also believe you would want this to return true in this case.

          I haven't tested, but based on the way the logic should be implicitly executed, this should be true.

          In general, I would always force any logical operation to be evaluated in the order you want (explicate execution). The same is true of math operations.

          Garth
          Last edited by gspiker; 05-29-2009, 03:59 PM.
          Garth

          Comment


          • #6
            Garth
            That case is returning true
            Alex




            Originally posted by gspiker
            I believe there is one case that isn't tested in the code Alex did, and would result in incorrect test results:

            If A is true and B & C is false, my belief is this will return false. I also believe you would want this to return true in this case.

            I haven't tested, but based on the way the logic should be implicitly executed, this should be true.

            In general, I would always force any logical operation to be evaluated in the order you want (explicate execution). The same is true of math operations.

            Garth

            Comment


            • #7
              I stand corrected (thanks for testing this Alex).

              In verifying operator precedence, it does say that && is evaluated before ||. I thought they would have equal precedence and therefore be resolved left to right.

              One reason I always do things explicitly is so I don't have to remember this stuff, and in other languages it also makes code portability more reliable.

              Garth
              Garth

              Comment


              • #8
                Thanks for all the responses.
                I'm glad I don't have to rerun all my tests!
                Richard

                Comment

                Working...
                X