• Checking whether a string is numeric

    From K.S. Bhaskar@21:1/5 to All on Wed Sep 8 14:08:52 2021
    I confess that I am a MUMPS newbie, because it has only been something over 26 years since I wrote my first M program.

    What is the "correct" way to check whether or not a string is a number? 1E3, 1000, 1E0, and 1E-3 should be identified as numbers, but 1-2E3 should be identified as not a number.

    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mpepper%ieee.org@gtempaccount.com@21:1/5 to K.S. Bhaskar on Wed Sep 8 17:54:35 2021
    On Wednesday, September 8, 2021 at 4:08:54 PM UTC-5, K.S. Bhaskar wrote:
    I confess that I am a MUMPS newbie, because it has only been something over 26 years since I wrote my first M program.

    What is the "correct" way to check whether or not a string is a number? 1E3, 1000, 1E0, and 1E-3 should be identified as numbers, but 1-2E3 should be identified as not a number.

    Regards
    – Bhaskar

    It depends, so I'll ask the question one asks of a newbie, "what are you trying to do?"

    The logical comparison, +x=x is a simple way to determine if x is in canonical form, but that's not what you asked.
    In a former lifetime, I worked on a non-standard MUMPS which had a number-test function that was true if the string-to-number conversion made it to the end of the string before terminating. I think this is the function you want, but I know of no
    equivalent in standard M.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ed de moel@21:1/5 to All on Thu Sep 9 06:56:30 2021
    I don't want to turn this into a programming contest, but I have seen many attempts at addressing this issue, and most of them worked well for the situation where they were used.
    One warning though, +"1E100" may result in a "numeric overflow" error, so you probably don't just want to know whether the value of a string qualifies as a "number", but also whether it is a number that is "usable" in your implementation.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to mpepper%...@gtempaccount.com on Thu Sep 9 11:57:04 2021
    On Wednesday, September 8, 2021 at 8:54:37 PM UTC-4, mpepper%...@gtempaccount.com wrote:
    On Wednesday, September 8, 2021 at 4:08:54 PM UTC-5, K.S. Bhaskar wrote:
    I confess that I am a MUMPS newbie, because it has only been something over 26 years since I wrote my first M program.

    What is the "correct" way to check whether or not a string is a number? 1E3, 1000, 1E0, and 1E-3 should be identified as numbers, but 1-2E3 should be identified as not a number.

    Regards
    – Bhaskar
    It depends, so I'll ask the question one asks of a newbie, "what are you trying to do?"

    The logical comparison, +x=x is a simple way to determine if x is in canonical form, but that's not what you asked.
    In a former lifetime, I worked on a non-standard MUMPS which had a number-test function that was true if the string-to-number conversion made it to the end of the string before terminating. I think this is the function you want, but I know of no
    equivalent in standard M.

    A fair question, Maury. I am trying to write an M expression where a canonical number is recognized as a canonical number. The best solution I have been able to come up with, assuming x is the string, is: x?1(1"",1"+",1"-")1(1".",1"").N1(1"E",1"E-",1"").N

    To answer Ed's question, if the number is outside the range where the implementation can handle it, that's a different issue

    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rod Dorman@21:1/5 to mpepper%...@gtempaccount.com on Thu Sep 9 18:20:07 2021
    In article <84a8d0f5-0e76-46ea-8a5e-2772cd611116n@googlegroups.com>, mpepper%...@gtempaccount.com <mpepper%ieee.org@gtempaccount.com> wrote:
    ...
    The logical comparison, +x=x is a simple way to determine if x is in >canonical form, but that's not what you asked.

    Even that has issues, its fine for what a typical user might enter but
    not so good when theres an exponent.

    The strings 2E3 and 2000 are both canonical and have the same numeric
    value but testing 2E3 with +x=x fails

    --
    -- Rod --
    rodd(at)polylogics(dot)com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ed de moel@21:1/5 to All on Thu Sep 9 12:04:22 2021
    1E+2 should be OK too, so I'd suggest: x?.1(1"+",1"-").N.1".".N.1(1"E"1.N,1"E-"1.N,1"E+"1.N)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to ed de moel on Thu Sep 9 13:35:35 2021
    On Thursday, September 9, 2021 at 3:04:23 PM UTC-4, ed de moel wrote:
    1E+2 should be OK too, so I'd suggest: x?.1(1"+",1"-").N.1".".N.1(1"E"1.N,1"E-"1.N,1"E+"1.N)

    Thanks Ed. You are much less of a MUMPS newbie than I am!

    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to K.S. Bhaskar on Thu Sep 9 14:22:43 2021
    On Thursday, September 9, 2021 at 4:35:37 PM UTC-4, K.S. Bhaskar wrote:
    On Thursday, September 9, 2021 at 3:04:23 PM UTC-4, ed de moel wrote:
    1E+2 should be OK too, so I'd suggest: x?.1(1"+",1"-").N.1".".N.1(1"E"1.N,1"E-"1.N,1"E+"1.N)
    Thanks Ed. You are much less of a MUMPS newbie than I am!

    Regards
    – Bhaskar

    In retrospect, $char(0)]]x is probably the simplest test.

    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From retired developer@21:1/5 to K.S. Bhaskar on Fri Sep 10 03:21:52 2021
    On 08.09.21 23:08, K.S. Bhaskar wrote:
    I confess that I am a MUMPS newbie, because it has only been something over 26 years since I wrote my first M program.

    What is the "correct" way to check whether or not a string is a number? 1E3, 1000, 1E0, and 1E-3 should be identified as numbers, but 1-2E3 should be identified as not a number.

    In addition to what others already wrote, you have to clarify the
    "string is a number" definition:

    - do you consider a string like "00123" or "123.4500" as numbers?
    - do you consider a string like "-12" or "---123" or "-+-12" as numbers?

    I would say, for a "string" to be a number:

    - the string must contain at least one digit
    - a period (if present) must be followed by a digit
    - during the string to number conversion, all the characters
    of that string must be consumed as part of the resulting number

    For example:
    "123.40" is a number
    "123.4X" is not a number, "x" wasn't used to build the value 123.4
    "123.,45" is not a number, "," and the digits 4 and 5 were not used
    "+-+087.90" is a number
    "12." is (not?) a number (a "." should be followed by a digit - or not?)

    Also, in US, ".34" is the canonical form for european "0.34". Some 30-40
    years ago, I worked with a MUMPS implementation where +".34" returned 0
    because of the missing leading zero...


    Regards
    Julius

    --
    An old Windows has old security holes.
    A new Windows has new security holes.
    Another OS has other security holes.
    For safety you must care yourself.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to retired developer on Fri Sep 10 07:29:16 2021
    On Thursday, September 9, 2021 at 9:21:55 PM UTC-4, retired developer wrote:
    On 08.09.21 23:08, K.S. Bhaskar wrote:
    I confess that I am a MUMPS newbie, because it has only been something over 26 years since I wrote my first M program.

    What is the "correct" way to check whether or not a string is a number? 1E3, 1000, 1E0, and 1E-3 should be identified as numbers, but 1-2E3 should be identified as not a number.
    In addition to what others already wrote, you have to clarify the
    "string is a number" definition:

    - do you consider a string like "00123" or "123.4500" as numbers?
    - do you consider a string like "-12" or "---123" or "-+-12" as numbers?

    I would say, for a "string" to be a number:

    - the string must contain at least one digit
    - a period (if present) must be followed by a digit
    - during the string to number conversion, all the characters
    of that string must be consumed as part of the resulting number

    For example:
    "123.40" is a number
    "123.4X" is not a number, "x" wasn't used to build the value 123.4
    "123.,45" is not a number, "," and the digits 4 and 5 were not used "+-+087.90" is a number
    "12." is (not?) a number (a "." should be followed by a digit - or not?)

    Also, in US, ".34" is the canonical form for european "0.34". Some 30-40 years ago, I worked with a MUMPS implementation where +".34" returned 0 because of the missing leading zero...


    Regards
    Julius

    --
    An old Windows has old security holes.
    A new Windows has new security holes.
    Another OS has other security holes.
    For safety you must care yourself.

    Thanks for the comments. I wanted to identify M canonical numbers (so, "+-+0.87.90" is an expression that evaluates to a number, but is not a number). In that regard at least, with YottaDB:

    set x="123.40" write $char(0)]]x
    0
    set x="123.4" write $char(0)]]x
    1
    set x="123,45" write $char(0)]]x
    0
    set x="+-+087.90" write $char(0)]]x
    0
    set x="+-+087.9" write $char(0)]]x
    0
    set x="12." write $char(0)]]x
    0
    set x="12" write $char(0)]]x
    1
    set x=".34" write $char(0)]]x
    1
    set x="0.34" write $char(0)]]x
    0
    set x="-12" write $char(0)]]x
    1
    set x="---123" write $char(0)]]x
    0


    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From retired developer@21:1/5 to K.S. Bhaskar on Sat Sep 11 00:39:59 2021
    On 10.09.21 16:29, K.S. Bhaskar wrote:

    Thanks for the comments. I wanted to identify M canonical numbers (so, "+-+0.87.90" is an expression that evaluates to a number, but is not a number).


    OK, you are talking about "canonical numbers". I was misled by your
    original question:
    What is the "correct" way to check whether or not a string is a
    number?

    and thought, you want check an user input or something like that. Hence
    my example, "123.40" is a number but "123.4xy" is not.

    Probably the correct question should be "What is the "correct" way to
    check whether or not a string is a canonical number? -----------------------------^^^^^^^^^^^^^^^^

    But never mind, we are there to clarify things.

    Rehards
    Julius

    --
    An old Windows has old security holes.
    A new Windows has new security holes.
    Another OS has other security holes.
    For safety you must care yourself.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)