Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking whether a string is numeric

121 views
Skip to first unread message

K.S. Bhaskar

unread,
Sep 8, 2021, 5:08:54 PM9/8/21
to
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

mpepper%...@gtempaccount.com

unread,
Sep 8, 2021, 8:54:37 PM9/8/21
to
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.

ed de moel

unread,
Sep 9, 2021, 9:56:34 AM9/9/21
to
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.

Rod Dorman

unread,
Sep 9, 2021, 2:20:08 PM9/9/21
to
In article <84a8d0f5-0e76-46ea...@googlegroups.com>,
mpepper%...@gtempaccount.com <mpepper%ieee...@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

K.S. Bhaskar

unread,
Sep 9, 2021, 2:57:05 PM9/9/21
to
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

ed de moel

unread,
Sep 9, 2021, 3:04:23 PM9/9/21
to
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)

K.S. Bhaskar

unread,
Sep 9, 2021, 4:35:37 PM9/9/21
to
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

K.S. Bhaskar

unread,
Sep 9, 2021, 5:22:44 PM9/9/21
to
In retrospect, $char(0)]]x is probably the simplest test.

Regards
– Bhaskar

retired developer

unread,
Sep 9, 2021, 9:21:55 PM9/9/21
to
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.

K.S. Bhaskar

unread,
Sep 10, 2021, 10:29:17 AM9/10/21
to
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:

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

Regards
– Bhaskar

retired developer

unread,
Sep 10, 2021, 6:40:02 PM9/10/21
to
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
0 new messages