Announcement

Collapse

Documentation

All current versions of the PowerBASIC documentation are available for download/viewing here:

PowerBASIC Links
See more
See less

VAL() With Very Long Strings

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

  • VAL() With Very Long Strings

    VAL() always returns 0 under certain conditions with very long strings

    Where %MaxInt = 32767 ( maximum value of an Integer):
    VAL(s) = 0 where LEN(s) > %MaxInt AND (LEN(S) <= %MaxInt *2 OR LEN(s) \ %MaxInt) MOD 2 = 0
    (I think I've got that right)

    Note VAL(s,x) not tested for various conditions!

  • #2
    Isn't %MAXWORD = 64k? Should use %MAXINT, no?
    "Not my circus, not my monkeys."

    Comment


    • #3
      Originally posted by Eric Pearson View Post
      Isn't %MAXWORD = 64k? Should use %MAXINT, no?
      Doh! I've corrected it above. Thank you.

      Comment


      • #4
        Make me wiser here, if the largest numeric data size is an 80 bit FP value, where would you use a 32k string to convert a string to a number ? I mean I have heard of big number libraries but 32k ?
        hutch at movsd dot com
        The MASM Forum

        www.masm32.com

        Comment


        • #5
          It wasn't intentional. Reading/parsing text files... incorrect delimiters in (external) file... parsing error... entire 32k+ string gets passed to VAL instead of just one field...
          "Not my circus, not my monkeys."

          Comment


          • #6
            Originally posted by Steve Hutchesson View Post
            Make me wiser here, if the largest numeric data size is an 80 bit FP value, where would you use a 32k string to convert a string to a number ? I mean I have heard of big number libraries but 32k ?
            A 32KB data string that starts with a number. rather than one that represents a number.

            Comment


            • #7
              Sounds like a character scan and a left$ would do it a lot better. Read up to the end of the number then just grab the numbers.
              hutch at movsd dot com
              The MASM Forum

              www.masm32.com

              Comment


              • #8
                Originally posted by Stuart McLachlan View Post
                VAL() always returns 0 under certain conditions with very long strings

                Where %MaxInt = 32767 ( maximum value of an Integer):
                VAL(s) = 0 where LEN(s) > %MaxInt AND (LEN(S) <= %MaxInt *2 OR LEN(s) \ %MaxInt) MOD 2 = 0
                (I think I've got that right)

                Note VAL(s,x) not tested for various conditions!
                As documented in help.
                If no number is found, the VAL() function returns zero (0).
                How long it tries is not specified. 2G max string length is only a logical assumption.

                Cheers,
                Dale

                Comment


                • #9
                  Originally posted by Dale Yarker View Post
                  > If no number is found, the VAL() function returns zero (0).

                  As documented in help. How long it tries is not specified. 2G max string length is only a logical assumption.
                  That's not the issue. This is::
                  '
                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  FUNCTION PBMAIN() AS LONG
                      LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 200,50,40,85 TO lDebug
                      TXT.PRINT VAL("123" & SPACE$(32000)) 'returns 123
                      TXT.PRINT VAL("123" & SPACE$(33000)) 'returns 0
                      TXT.PRINT VAL("123" & SPACE$(66000)) ' returns 123
                      TXT.PRINT VAL("123" & SPACE$(99000)) 'returns 0
                      TXT.PRINT VAL("123" & SPACE$(140000)) ' returns 123
                       TXT.PRINT "  ....Press any key to exit": TXT.WAITKEY$: TXT.END
                  END FUNCTION
                  '

                  Comment


                  • #10
                    That is exactly the issue. It is just integer roll-over.
                    33000 is negative looking at low 16 bits.
                    66000 is positive looking at low 16 bits.
                    (same for 99000 and 140000)

                    "2G max string length is only a logical assumption." but should be corrected (hopefully someday (soon))

                    Cheers,
                    Dale

                    Comment


                    • #11
                      Simpler criterion than post#1 for whether VAL() will work with a large string. VAL() only works correctly:
                      IF BIT(Len(string),15) = 1 AND LO(WORD,Len(String)) >= LEN(numericsubstring)

                      '
                      Code:
                      #COMPILE EXE
                      #DIM ALL
                      FUNCTION PBMAIN() AS LONG
                          LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 200,50,40,85 TO lDebug
                          LOCAL s AS STRING
                          LOCAL l AS LONG
                          TXT.PRINT "Strings in the form ""1234xxx....."""
                          TXT.PRINT "String Len","Bit 15","Lo(word,len)","Val()
                          s = "1234" & SPACE$(65530):l = LEN(s): TXT.PRINT  l, BIT(l,15),LO(WORD,l), VAL(s)'returns 0
                          s = "1234" & SPACE$(65531):l = LEN(s): TXT.PRINT  l, BIT(l,15),LO(WORD,l), VAL(s)'returns 0
                          s = "1234" & SPACE$(65532):l = LEN(s): TXT.PRINT  l, BIT(l,15),LO(WORD,l), VAL(s)'returns 0
                          s = "1234" & SPACE$(65533):l = LEN(s):  TXT.PRINT l, BIT(l,15),LO(WORD,l), VAL(s)'returns 1
                          s = "1234" & SPACE$(65534):l = LEN(s):  TXT.PRINT l, BIT(l,15),LO(WORD,l), VAL(s)'returns 12
                          s = "1234" & SPACE$(65535):l = LEN(s):  TXT.PRINT l, BIT(l,15),LO(WORD,l), VAL(s)'returns 123
                          s = "1234" & SPACE$(65536):l = LEN(s):  TXT.PRINT l, BIT(l,15),LO(WORD,l), VAL(s)'returns 1234
                          TXT.PRINT "   ...press any key":TXT.WAITKEY$:TXT.END
                      END FUNCTION
                      '
                      Last edited by Stuart McLachlan; 9 Jul 2021, 09:34 AM.

                      Comment


                      • #12
                        ... also on the topic.
                        PB VAL() - Problem ... maybe - PowerBASIC Peer Support Community

                        Comment


                        • #13
                          Originally posted by Plamen Chobanov View Post
                          Yep Wayne analysed this in post#4 of your thread. These sort of problems tend to get lost in all the other threads over time, which is why we established this "PowerBasic Documentation" area to collect descriptions of identified bugs and documentation error/deficiencies in one place.

                          This entry was prompted by this thread: https://forum.powerbasic.com/forum/u...ion#post808948

                          Comment


                          • #14
                            This is one of the rare bugs in PB!

                            The Val() function chokes on strings larger than 32764 bytes and starts returning 0, which is a bug. It works properly again at 65534 bytes. This behavior keeps repeating further up


                            JK

                            Comment


                            • #15
                              Originally posted by Juergen Kuehlwein View Post
                              The Val() function chokes on strings larger than 32764 bytes and starts returning 0, which is a bug. It works properly again at 65534 bytes. This behavior keeps repeating further up
                              32767 actually and it's not that simple. It starts working properly again when the string length is 65536 + the length of the numeric substring
                              See edited post #11

                              Comment

                              Working...
                              X