Announcement

Collapse
No announcement yet.

Bizarre math bug

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

  • #21
    In the Visual Basic help file, it explains that the base argument
    can be negative as long as the exponent argument is an integer
    value. Looking at help files from other compilers, I see that
    they do it the same way for equivalent Power functions. Although
    the PB help file does not have an explanation for the ^ operator,
    I assume that it has always used this same standard. Does
    someone from PB care to shed some light on how the ^ operator is
    implemented internally in PB? The mysterious result that I got
    only appears under certain circumstances, so it appears not to
    be the intended compiler behavior.

    ------------------
    Daniel Corbier
    UCalc Fast Math Parser
    http://www.ucalc.com
    Daniel Corbier
    uCalc Fast Math Parser
    uCalc Language Builder
    sigpic

    Comment


    • #22
      Daniel,

      I have conducted many tests, and my results show that the exponent
      is converted to the nearest integer so that:

      (-2)^2.501 = -8
      (-2)^2.499 = 4

      And my test show that it doesn't matter what kind of data elenment
      receives the answer. I believe that the problem has to do with the
      automatic register assignments. For example:

      Code:
      #COMPILE EXE
      FUNCTION PBMAIN
         DIM a AS EXT, b AS EXT, c AS EXT, d AS EXT
         MSGBOX STR$(-1 + (-2)^3.45)   'wrong ans
         MSGBOX STR$((-2)^3.45 - 1)    'right ans 
      END FUNCTION
      
      whereas...
      
      #COMPILE EXE
      FUNCTION PBMAIN
         DIM a AS DOUBLE, b AS DOUBLE, c AS DOUBLE, d AS DOUBLE
         MSGBOX STR$(-1 + (-2)^3.45)   'right ans
         MSGBOX STR$((-2)^3.45 - 1)    'right ans 
      END FUNCTION
      But if #REGISTER NONE is added, or if less than four unused variables
      are allocated, the result is always right.

      Therefore, it would appear to me that the problem rests totally on
      how the registers are automatically assigned by the compiler.



      ------------------


      [This message has been edited by Charles Dietz (edited August 25, 2000).]

      Comment


      • #23
        Charles, you have done some great detective work here. I've asked R&D to review this topic, guys, so thanks for the info! We'll see what "develops"



        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #24
          R&D report that it is due to an FPU register stack overflow. It is now on the list. Thanks again guys!



          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #25
            Originally posted by Tim Wisseman:
            >>
            Is the problem not with raising a negative number to a power?
            This is how i would have done it.
            MSGBOX STR$ (-1 +((ABS(-1) ^2)/1))
            so that the negation is removed before raising to the power of 2.
            Answer 0
            >>

            My tests show that the problem only hits when you raise
            a negative number to a power. Using ABS() is a good work
            around to the problem. Your code will work fine.

            Tim


            Its been years since I've done this but a negative power is an
            imaginary number if I remember correctly.

            sqr(-2) = sqr(2)i

            I might be incorrect on the formula but I think that is somewhat
            right.

            ------------------
            -Greg
            -Greg
            [email protected]
            MCP,MCSA,MCSE,MCSD

            Comment


            • #26
              Originally posted by Lance Edmonds:
              Thanks guys... R&D tell me that this problem has been identified and will be sorted out in the next update to the compiler.

              Also, remind them to create a patch for those who can't afford an update, and just want what they already have to work. Thank you.


              Sincerely,

              Marcus

              Comment


              • #27
                >>
                Its been years since I've done this but a negative power is an
                imaginary number if I remember correctly.

                sqr(-2) = sqr(2)i

                I might be incorrect on the formula but I think that is somewhat
                right.
                >>

                Yes, the square root of a negative requires complex math
                and imaginary numbers. Negative square roots requires much
                more then the SQR() function.

                However, powers are just good old floating point math.

                (-2) ^ 2 = (-2) * (-2) = 4

                2 ^ (1) = 2
                2 ^ (0) = 1
                2 ^ (-1) = 0.5

                . . . and we are just dealing with powers when it comes
                to this problem. When a program made with PowerBasic raises
                a negative number to a power and things are "just right" the
                FPU stack gets messed up.

                SQR() is not envolved with this.


                ------------------

                Comment


                • #28
                  Originally posted by Marcus Moore:
                  Also, remind them to create a patch for those who can't afford an update, and just want what they already have to work. Thank you.
                  Updates are usually free, Upgrades are not. There is expected to be an update to the current version of the compiler, but at this time, no information, pricing, or details are available..

                  ------------------
                  Lance
                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>
                  Lance
                  mailto:[email protected]

                  Comment


                  • #29
                    Originally posted by Lance Edmonds:
                    Updates are usually free, Upgrades are not. There is expected to be an update to the current version of the compiler, but at this time, no information, pricing, or details are available..
                    If updates are free, why would "pricing" apply? By the way, where are past updates posted? Forgive me, I'm new around here. Thanks.


                    Sincerely,

                    Marcus


                    ------------------

                    Comment


                    • #30
                      Did you note my emphasis on the word "usually"? Unless we start giving away products, upgrades and new purchases will continue to involve charges. However, in the past, updates (which are diffent from upgrades) have been issued for free to registered customers.

                      The SUPPORT section of this web site details the current edition/build of our products, which is determined by the file's date & time (the DAY indicates the release build number, and the time indicates the version number).

                      If your product is older than the relevant date shown in the SUPPORT page, then you can send a request to mailto:[email protected][email protected]</A> with your serial number and your zip code (US residents), and they will send you the relevant update. If (and the emphasis is on IF) there are any costs involved, Sales will sort it out with you at that time.

                      I hope this explains things...
                      http://www.powerbasic.com/support



                      ------------------
                      Lance
                      PowerBASIC Support
                      mailto:[email protected][email protected]</A>
                      Lance
                      mailto:[email protected]

                      Comment

                      Working...
                      X