Announcement

Collapse
No announcement yet.

Numeric literals near QUAD limits

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

  • Numeric literals near QUAD limits

    Is there a problem with the last digit at/near the positive and negative limits of a QUAD integer?

    Code:
    PRINT "9223372036854775807"
    PRINT
    PRINT DEC$((2^63)-1,18)
    PRINT DEC$(9223372036854775807&&,18)
    PRINT
    PRINT HEX$((2^63)-1,16)
    PRINT HEX$(9223372036854775807&&,16)
    
    '...produces...
    
    9223372036854775807
    
    9223372036854775807
    9223372036854775800
    
    7FFFFFFFFFFFFFFF
    7FFFFFFFFFFFFFF0
    "Not my circus, not my monkeys."

  • #2
    I recall something about the 19th digit. Let me see if I can track it down.

    I guess I was thinking about the Help entry under STR$, but it may be the same going the other way from a literal in source code to the value.

    Although a Quad integer actually has 19 digits of precision, only 18 digits of accuracy can be "displayed" with STR$. A 19-digit value will be rounded to 18 digits in scientific notation when used with STR$. STR$ works with up to 16 significant digits by default, so the enhanced form of STR$ (eg: STR$(var,18)), must be used to generate the 17th and 18th digits of a Quad integer for display purposes.


    Comment


    • #3
      Interestingly this works corrrectly:
      ? DEC$(9223372036854775800+7)

      (But 1+6, 2+5 etc don't.)

      Comment


      • #4
        With PBCC 6 I see...

        Code:
        0922337203685477587
        0CCCCCCCCCCCCCD3
        Added: You must have removed the HEX$ line from your post.
        "Not my circus, not my monkeys."

        Comment


        • #5
          Also, for me,
          ? HEX$(9223372036854775807&&,16) produces 7FFFFFFFFFFFFFF8 rather than your 7FFFFFFFFFFFFFF0
          (PB WIn 10.04)

          Comment


          • #6
            Originally posted by Eric Pearson View Post
            Added: You must have removed the HEX$ line from your post.
            Yep, I needed to do a bit more playing wih HEX$

            Comment


            • #7
              Turns out it's been discussed several times. See https://forum.powerbasic.com/forum/u...807#post758332
              which leads to https://forum.powerbasic.com/forum/u...rals#post61991

              Comment


              • #8
                Well I'll be darned.
                "Not my circus, not my monkeys."

                Comment


                • #9
                  If you have issues handling "many" decimal digits, you could always adapt the approach I took in (unsure either of year or why I did this in the first place) .

                  Code and text available from my website.

                  FACTOR.ZIP
                  Tutorial and source code for Qbasic, QuickBASIC and PowerBASIC showing a technique to calculate large numbers to at least 154 significant
                  digits. Freeware.

                  http://talsystems.com/tsihome_html/techcorner.html request this file.
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Just to clarify, It's not "near quad limits", it's 19th digit of quad literals. Same problem occurs with

                    ? DEC$(1111111111111111111,19)
                    ? DEC$(2222222222222222222,19)
                    etc and
                    ? HEX$(1000000000000000009,16) & $LF & HEX$(1000000000000000010,16)

                    Edit: I've added an entry in the Documentation sub-forum: https://forum.powerbasic.com/forum/u...git#post807333

                    Comment


                    • #11
                      Originally posted by Michael Mattias View Post
                      If you have issues handling "many" decimal digits, you could always adapt the approach I took in (unsure either of year or why I did this in the first place) .

                      Code and text available from my website.

                      FACTOR.ZIP
                      Tutorial and source code for Qbasic, QuickBASIC and PowerBASIC showing a technique to calculate large numbers to at least 154 significant
                      digits. Freeware.

                      http://talsystems.com/tsihome_html/techcorner.html request this file.
                      That 25 year old code does not address the issue of this thread in any shape or form

                      Comment


                      • #12
                        That 25 year old code does not address the issue of this thread in any shape or form
                        ???

                        Sure it does. Of course, the title used to describe the software does not.

                        But what it does is show you that there are additional ways to handle "many" digits.. depending on your requirements. Seems to me this thread starts with trying to format numbers for display in either decimal or hexadecimal.

                        What I have will not handle the "2^63-1" embedded arithmetic operation in the above, but formatting numbers in decimal and/or hex is a good application for the technique I demonstrate.

                        Maybe you are just jealous of the fact I did it 25 years ago?

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          > Maybe you are just jealous

                          What are you, seven?
                          "Not my circus, not my monkeys."

                          Comment


                          • #14
                            Originally posted by Michael Mattias View Post

                            ???

                            Sure it does. Of course, the title used to describe the software does not.

                            But what it does is show you that there are additional ways to handle "many" digits.. depending on your requirements. Seems to me this thread starts with trying to format numbers for display in either decimal or hexadecimal.
                            You obvious don't understand the orginal post at all . It has nothing to do with "trying to format numbers for display"

                            This thread starts with a problem in accurately storing the value of a 19 decimal digit literal QUAD.
                            The display was purely for illustrative purposes showing the difference between how the compiler handles the same number depending on how it was coded i.e. PRINT HEX$((2^63)-1,16) versus PRINT HEX$(9223372036854775807&&,16)

                            The Thread Subject should be a clue: Numeric literals near QUAD limits

                            Please show the code in your link that deals with accuracy of the value of a 19 digit Quad literal (or even the storage of any QUAD or other large literal)

                            Comment


                            • #15
                              This thread starts with a problem in accurately storing the value of a 19 decimal digit literal QUAD.
                              ??? Post number one uses no variables. If it did and/or you read the help, you'd know there is no problem STORING the value of a QUAD integer. Any 'problems' are in the input and output of same in a zoned decimal presentation.

                              And as you note, the thread title is a clue: it talks about LITERALS. Literals are not forms of storage.
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                Originally posted by Michael Mattias View Post
                                ??? Post number one uses no variables.
                                ???? Who mentioned variables?
                                If it did and/or you read the help, you'd know there is no problem STORING the value of a QUAD integer. Any 'problems' are in the input and output of same in a zoned decimal presentation.

                                And as you note, the thread title is a clue: it talks about LITERALS. Literals are not forms of storage.
                                You are stilling missing the point. The problem lies in the compiler correctly storing * the value of a QUAD which is expressed in the source code as a 19 digit literal.
                                * storing being the process of placing the source code literal in a sequence of bytes representing a QUAD in the compiled code. .

                                There is no problem with input or ouput (whether as a "zoned decimal presentation" or any other presentation) of a 19 digit decimal, as is obvious from the code in the original post - if you take the time to to read and understand it.

                                The problem is entirely a compilation issue with 19 digit integral literals in the source code. ( which your linked code does not address with in any shape or form)

                                Maybe this simple example will help you see what the issue is:
                                '
                                Code:
                                #COMPILE EXE
                                #DIM ALL
                                %A = 1111111111111111111
                                %B = 1111111111111111110 + 1
                                FUNCTION PBMAIN() AS LONG
                                    LOCAL c,d AS QUAD
                                    c = 1111111111111111111
                                    d = 1111111111111111110 + 1
                                   ? DEC$(%A) & $LF & DEC$(%B) & $LF & DEC$(c) & $LF & DEC$(d)
                                END FUNCTION
                                '
                                Output:
                                ---------------------------
                                PowerBASIC
                                ---------------------------
                                1111111111111111110
                                1111111111111111111
                                1111111111111111110
                                1111111111111111111
                                ---------------------------
                                OK
                                ---------------------------

                                Comment


                                • #17
                                  Who mentioned variables?
                                  You did when you mentioned 'storage.' There is no "storage" without variables, including implicit ("temporary" ) variables.

                                  Regardless, we are not on the same page here.

                                  Please show the code in your link that deals with accuracy of the value of a 19 digit Quad literal (or even the storage of any QUAD or other large literal). we have...
                                  Literals have no value and are stored only as character strings; therefore 'accuracy' does not apply.

                                  I think you could store a "large decimal value with many digits" in a VARIANT of type DECIMAL... yup, I found the reference..

                                  https://docs.microsoft.com/en-us/off...imal-data-type

                                  Note there is no direct arithmetic supported.. you will probably need to use something like... well, like what I posted in #11 !!!

                                  MCM
                                  Michael Mattias
                                  Tal Systems (retired)
                                  Port Washington WI USA
                                  [email protected]
                                  http://www.talsystems.com

                                  Comment


                                  • #18
                                    And the real simple solution for the "problem" of using 18-19 digit decimal literals with PowerBASIC is... don't do that.
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      Originally posted by Michael Mattias View Post

                                      You did when you mentioned 'storage.' There is no "storage" without variables, including implicit ("temporary" ) variables.
                                      Really???

                                      Maybe you should read what BOB wrote:
                                      PowerBASIC programs process two distinct classes of data: variables and constants. A variable is allowed to change its value as a program runs. A constant's value is fixed at compile-time, and cannot change during program execution (hence, it remains constant). PowerBASIC supports four types of constants: string literals, numeric literals, string equates and numeric equates.

                                      %MyConstant = 123 is not a variable (implicit or otherwise)
                                      ? str$( 12) doesn't use any variable (implicit or otherwise).

                                      In both cases, numbers are STORED in locations in the compiled executable file.

                                      Again, quoting Bob:
                                      • You can also force a literal value to be stored with a given precision by following the constant with one of the variable type-specifiers (%, &, &&, ?, ??, ???, !, #, ##, @, @@).
                                      ...
                                      If a type-specifier does not follow a numeric constant, the following rules are used to determine the precision the value will be stored in:
                                      ...
                                      If the value is an integer too large to fit in a Long or Double-word, but small enough to fit in a Quad integer, it will be stored as a Quad intege
                                      r.


                                      Comment


                                      • #20
                                        Originally posted by Michael Mattias View Post
                                        And the real simple solution for the "problem" of using 18-19 digit decimal literals with PowerBASIC is... don't do that.
                                        There is no problem with using 18 digit decimal literals. Why would you not do that?

                                        As for 19 digit ones, we have already identified a simple work-around - use "xxxxxxxxxxxxxxxxxx0 + y" in place of "xxxxxxxxxxxxxxxxxxy"

                                        Comment

                                        Working...
                                        X