Announcement

Collapse
No announcement yet.

Numeric equate expressions pre-calculated?

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

  • #21
    I once worked with a woman whose first husband was the original Creature from the Black Lagoon.

    Look, a squirrel!
    "Not my circus, not my monkeys."

    Comment


    • #22
      One of my old work buddies was an extra in a famous scene from Blazing Saddles: https://www.youtube.com/watch?v=cjNIARQGIYA
      "Not my circus, not my monkeys."

      Comment


      • #23
        Of course it will slow down the executable, because there is no such thing as a free lunch.
        Detection of floating point errors for overflow, underflow, divide by zero, loss of precision and invalid operations do not slow down your executable. They're already being detected but the compiler ignores them.

        Comment


        • #24
          >[overflow is ] already being detected but the compiler ignores them.

          I did not know that.

          Is that something you can detect with say, SetUnhandledExceptionFilter()?

          Ok, Michael.. LOOK IT UP.. Well, i guess so..

          .....
          EXCEPTION_FLT_DENORMAL_OPERAND One of the operands in a floating-point operation is denormal. A denormal value is one that is too small to represent as a standard floating-point value.
          EXCEPTION_FLT_DIVIDE_BY_ZERO The thread tried to divide a floating-point value by a floating-point divisor of zero.
          EXCEPTION_FLT_INEXACT_RESULT The result of a floating-point operation cannot be represented exactly as a decimal fraction.
          EXCEPTION_FLT_INVALID_OPERATION This exception represents any floating-point exception not included in this list.
          EXCEPTION_FLT_OVERFLOW The exponent of a floating-point operation is greater than the magnitude allowed by the corresponding type.
          EXCEPTION_FLT_STACK_CHECK The stack overflowed or underflowed as the result of a floating-point operation.
          EXCEPTION_FLT_UNDERFLOW The exponent of a floating-point operation is less than the magnitude allowed by the corresponding type.
          Guess I'd just have to check for one of those in my EXCEPTION_RECORD. And then figure out what to do with this knowledge.

          Thank you.

          But what this might miss would be "PB Data type overflow" ... eg
          Code:
           LOCAL A, B, C as INTEGER 
           
            A =   32767
            B =   32767
            C =   A + B       <== overflows capacity of INTEGER
          I guess I'd need some more "user-created" code to handle those.

          MCM
          Last edited by Michael Mattias; 21 Jan 2016, 11:06 AM.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #25
            > Is that something you can detect

            IIRC the compiler actually masks some things. Therein lies the core debate.

            Added: About numeric errors I mean. Not equate math or squirrels.
            Last edited by Eric Pearson; 21 Jan 2016, 11:23 AM.
            "Not my circus, not my monkeys."

            Comment


            • #26
              http://www.powerbasic.com/support/pb...ad.php?t=37821

              Integer overflows are different and can be detected but do slow down execution slightly.

              Comment


              • #27
                IIRC the compiler actually masks some things.
                The FPU does all the floating point calculations and simultaneously does checks on the operands and results. These checks take place any time the FPU does anything, they can't be turned off and there is no speed loss.

                If an error occurs then a status bit is set in the FPU to indicate the error.

                The 2 intended ways (i.e.built in to the FPU hardware) to deal with these errors are:
                1) check the status flags after the calculation. If a flag is set then an error occurred. The flags are "sticky" so once set they remain set until explicitly cleared.

                2) clear the mask bit in the appropriate FPU mask register. Then if an error occurs an exception is immediately raised.

                The PB compiler sets all the mask bits so no exceptions are raised when errors are detected.
                It's not clear if the compiler preserves the status flags at all times. It should do.

                Comment


                • #28
                  Then there is the same question for macros.

                  Macro X = 2
                  Macro Y = X + 3


                  On compilation, is 5 substituted for Y, or 2 + 3?

                  I suspect the former, because the Help file points out the following trick:

                  Macro Const = Macro

                  That enables you to use Const as in VisualBasic, including having real numbers as constants not just integers. If (in what I started with above) the latter, then this trick in general would be inefficient.
                  Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                  Comment


                  • #29
                    Originally posted by Mark Hunter View Post
                    Then there is the same question for macros.

                    Macro X = 2
                    Macro Y = X + 3


                    On compilation, is 5 substituted for Y, or 2 + 3?

                    I suspect the former, because the Help file points out the following trick:

                    Macro Const = Macro

                    That enables you to use Const as in VisualBasic, including having real numbers as constants not just integers. If (in what I started with above) the latter, then this trick in general would be inefficient.
                    Macros are direct text substitutions pre-compilation (no calcuations involved) in a first pass by the compiler through the source code.
                    By the time the code is actually compiled, neither X nor Y exist.

                    First, all occurrences of "X" in the source would be replaced with "2", so "Macro Y = X+3" is converted to "Macro Y = 2 + 3"
                    Then all occurences of "Y" are replace by "2 + 3",
                    so a line such as "lngResult = Y + 4" would be converted to "lngResult = 2 + 3 + 4"

                    What the compiled does with the "2 + 3" there is a different matter and the fact that those characters came from macros is unknown to the compiler.

                    Comment


                    • #30
                      What the compiled does with the "2 + 3" there is a different matter
                      Unless something changed, the Windows compiler does reduce it to 5 at compile time, just as PBDOS does. Not hard to test... write a program that uses the outer macro 10k times, compile and note size of EXE, simplify the macros and compile again. I did that during one of the betas.

                      Added: Previous post almost 6 years ago, and Mark picks up like it's yesterday.

                      Then there is the same question for macros.
                      ... and I'll admit that I didn't re-read posts 1-27.
                      "Not my circus, not my monkeys."

                      Comment


                      • #31
                        Trouble is, the evaluation is not done as the constants are defined but rather in the code where they are used. This can lead to order of operations bugs unless you enclose the macro in parenthesis. For example

                        Macro X = 2
                        Macro Y = X * 3


                        Z = 6 / Y

                        Z will be computed as
                        Code:
                        6 / X * 3   =   6 / 2 * 3   =   3 * 3   =   9
                        not as
                        Code:
                        6 / 6   =   1
                        But if you stick with good ol’ equates
                        %X = 2
                        %Y = %X * 3

                        it works without putting parentheses about %X * 3.
                        Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                        Comment


                        • #32
                          Originally posted by Mark Hunter View Post
                          Trouble is, the evaluation is not done as the constants are defined...
                          That is not "trouble". It is expected, documented behaviour.
                          Macros DON"T "define constants". That's where your confusion arises.

                          You misunderstand what is happening (even though both Help and my previous post explain it). They provide text substitution in the source code - an entirely different thing to "constant declaration".

                          Macro X = 2
                          Macro Y = X * 3

                          Z = 6 / Y

                          First TEXT substitution:
                          Y = 2 * 3
                          Second TEXT substitution
                          Z = 6 / 2 * 3
                          That is what the compiler sees in the "macro expanded" source code and what it evaluates.

                          Exactly as documented.

                          To quote Help:
                          "When using single-line macros that contain numeric expressions, use parentheses around the macro body to guard against unexpected order of precedence problems when the macro is used within an expression.".

                          i.e
                          MACRO Y = (X * 3) is what Help says to do, and it works as expected.

                          In essence, it's just a smart "find and replace" of text in the source code.

                          Comment


                          • #33
                            Stuart,

                            You are not disagreeing with me.

                            If you follow the Help documentation and use
                            Macro Const = Macro
                            expecting to be able to use Const as in Visual Basic and other compilers, you will run into Trouble.

                            That's Trouble with a capital T and that rhymes with P and that stands for PowerBasic.

                            From the PBCC4 Help:
                            ... a simple single-line macro can allow PowerBASIC to emulate the CONST syntax used in Visual Basic ...

                            The single-line MACRO offers a cunning way to retain the CONST syntax used in MSBASIC and Visual Basic in your PowerBASIC code, while maintaining the low overhead advantage of PowerBASIC.
                            Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                            Comment


                            • #34
                              Again, you will only run into trouble if you don't understand what a macro does. . Note that ."emulates" does not mean "replicates".

                              MACRO CONST = MACRO
                              works fine unless you ignore the advice to wrap any expression in a Macro in parentheses in accordance with the quote in post #32.

                              If you understand what Macros do, it makes perfect sense and to not use parentheses around the expression in CONST = <expression> is a fundamental programming error.

                              The only "trouble" that will occur is the result of erroneously assuming that a Macro expression is somehow evaluated by a text substituion process when the macro is expanded.

                              FWIW, the only reason I can think of to use the clumsy MACRO CONST = MACRO is if you are naively converting VB code to PB.
                              MACOR CONST = MACRO
                              CONST X = 2
                              is still just macro expansion, not a constant declaration.
                              PB has it's own simpler Constant syntax which evaluates the constants when their declaration is encountered.
                              %X = 2

                              Comment


                              • #35
                                The PBCC4 help says:
                                ... a simple single-line macro can allow PowerBASIC to emulate the CONST syntax used in Visual Basic ...

                                The single-line MACRO offers a cunning way to retain the CONST syntax used in MSBASIC and Visual Basic in your PowerBASIC code, while maintaining the low overhead advantage of PowerBASIC.
                                Will the recommended macro emulate Visual Basic's Const? Don't you think saying it will a tad misleading?

                                In case you missed it, the business about Trouble was a takeoff from Meredith Willson's Broadway musical The Music Man.

                                Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                                Comment


                                • #36
                                  Whatever.

                                  Comment


                                  • #37
                                    Will the recommended macro emulate Visual Basic's Const? Don't you think saying it will a tad misleading?
                                    No!

                                    emulate - Wiktionary
                                    Dale

                                    Comment


                                    • #38
                                      Merriam-Webster is more reliable. Anyway, if "emulate" is ambiguous then the PB Help ought to use a more precise word.

                                      I maintain that only 49 angels can dance on the head of a pin. Anyone want to argue about it?
                                      Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                                      Comment


                                      • #39
                                        Creature from Black Lagoon and blazing saddles are classic! I was at the set of “my step mother is an alien” during some filming but didn’t make it into any scenes. Squirrel!

                                        Comment


                                        • #40
                                          FWIW, the only reason I can think of to use the clumsy MACRO CONST = MACRO is if you are naively converting VB code to PB.
                                          Rather than "naively" I have multiple posts here which use the word "mindless" for that kind of port/conversion.

                                          You must be more polite or something.
                                          Michael Mattias
                                          Tal Systems (retired)
                                          Port Washington WI USA
                                          [email protected]
                                          http://www.talsystems.com

                                          Comment

                                          Working...
                                          X