Announcement

Collapse
No announcement yet.

Understanding Currency

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

  • Understanding Currency

    I was wondering how CURRENCY is stored in memory. Does someone have a explanation on how to go from 10,65,49,0,0,0,0,0,0 to 481?

    Code:
    #COMPILER PBWIN
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN() AS LONG
       LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 10,10,50,85 TO lDebug 'Thanks Dave!
    
       LOCAL C AS CURRENCY
       LOCAL I AS LONG
    
       C = 481
       FOR I = VARPTR(C) TO VARPTR(C) + 8
          TXT.PRINT HEX$(PEEK(I))
       NEXT I
       TXT.PRINT "Press any key to exit (just like the old days!)": TXT.WAITKEY$: TXT.END
    END FUNCTION

  • #2
    From Help:

    Internally, Currency and Extended-currency numbers are stored as Quad-integers with an implied decimal point (at 4 places for Currency, and at 2 places for Extended-currency). This approach ensures that all of the digits of the variables can be represented exactly.

    Maybe this makes it clearer:
    '
    Code:
    #COMPILER PBWIN
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN() AS LONG
       LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 50,50,50,85 TO lDebug 'Thanks Dave!
    
       LOCAL C AS CURRENCY
       LOCAL I,x AS LONG
       LOCAL q,cq AS QUAD
       C = 481.0001
       q = 4810001
           TXT.PRINT "C   Q"
           TXT.PRINT "-----"
       FOR I = 7  TO 0 STEP -1
          TXT.PRINT HEX$(PEEK(VARPTR(C) + I)) & "   " & HEX$(PEEK(VARPTR(q) + I))
       NEXT I
    
       cq = PEEK(VARPTR(c) ) + _
            PEEK(VARPTR(c) + 1) * 256 + _
            PEEK(VARPTR(c) + 2) * 256^2 + _
            PEEK(VARPTR(c) + 3) * 256^3 + _
            PEEK(VARPTR(c) + 4) * 256^4 + _
            PEEK(VARPTR(c) + 5) * 256^5 + _
            PEEK(VARPTR(c) + 6) * 256^6 + _
            PEEK(VARPTR(c) + 7) * 256^7
       TXT.PRINT
       TXT.PRINT  "Currency peek sum (CPS): " STR$(cq)
       TXT.PRINT "CPS/10000:"STR$(cq/10000)
    
       TXT.PRINT "Press any key to exit (just like the old days!)": TXT.WAITKEY$: TXT.END
    END FUNCTION
    '

    Comment


    • #3
      Still not seeing it. You proved that quads and currency are stored the same but how do you get from 49h, 65h, 10h to 481?

      Comment


      • #4
        Originally posted by Frank Rogers View Post
        Still not seeing it. You proved that quads and currency are stored the same but how do you get from 49h, 65h, 10h to 481?
        I've edited the above code to sum the byte values which should answer your question.

        Specifically
        PEEK(VARPTR(c) ) + PEEK(VARPTR(c) + 1) * 256 + PEEK(VARPTR(c) + 2) * 256^2)
        is 4810000 as a quad or 481.0000 as currency

        Try:
        TXT.PRINT (&H10 + &H65 * 256 + &H49 * 256^2)/10000

        Comment


        • #5
          ahh, that's what I needed.
          Code:
          TXT.PRINT DEC$((&H10 + &H65 * 256 + &H49 * 256^2) / 10000)

          Comment


          • #6
            CURRENCY is a fixed decimal with 4 digits right of decimal point.
            Or, it is the integer count of "ten thousanths" (where QUAD is the integer count of "ones").
            So 481 is 4810000 ten thousanths.
            (481 x 10000 = 4810000)
            Hex for 4810000 is 496510.
            Integers are stored low endian (the least significant byte first).
            So, one byte at a time as hex is 10, 65, 49.
            Cheers,
            Dale

            Comment


            • #7
              I see the "light bulb over head" turned on in post 5. yea!
              Dale

              Comment


              • #8
                Code:
                UNION CQUNION
                  C AS CURRENCY
                  Q  AS QUAD
                END UNION
                
                     LOCAL CQU  AS CQUNION
                     LET   CQU.C  =   [email protected]
                     PRINT CQU.C     <<  Shows 481  ...  the data when read as a Currency
                     PRINT CQU.Q      <<<   Shows 4810000 .. the same data when viewed as a QUAD
                ahh, that's what I needed.
                I can't imagine any "need" other than trying to interpret some data file. ???


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

                Comment


                • #9
                  This approach [to storage] ensures that all of the digits of the variables can be represented exactly.

                  It also allows arithmetic to be done as integer arithmetic, eliminating the rounding which occurs when working with floating point. I have used CUR for monetary amounts for the 20+ years PB has offered them, even when CUR was BCD rather than the current decimal scaled binary. (CUR was BCD in PB/DOS).

                  IMNSHO absolutely nothing better for handling monetary amounts.
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Originally posted by Michael Mattias View Post
                    I can't imagine any "need" other than trying to interpret some data file. ???
                    MCM
                    You nailed it. Someone else was trying to read my datafile. They had the 8 byte string and could not figure how to get 481 out of it.

                    Comment


                    • #11
                      Originally posted by Dale Yarker View Post
                      I see the "light bulb over head" turned on in post 5. yea!
                      The bulb might not be as bright as it was 40 years ago, but it still flickers from time to time!

                      Comment

                      Working...
                      X