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
On Wednesday, September 8, 2021 at 4:08:54 PM UTC-5, K.S. Bhaskar wrote:equivalent in standard M.
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.
RegardsIt depends, so I'll ask the question one asks of a newbie, "what are you trying to do?"
– Bhaskar
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
...
The logical comparison, +x=x is a simple way to determine if x is in >canonical form, but that's not what you asked.
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)
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
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.
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.
set x="123.40" write $char(0)]]x0
set x="123.4" write $char(0)]]x1
set x="123,45" write $char(0)]]x0
set x="+-+087.90" write $char(0)]]x0
set x="+-+087.9" write $char(0)]]x0
set x="12." write $char(0)]]x0
set x="12" write $char(0)]]x1
set x=".34" write $char(0)]]x1
set x="0.34" write $char(0)]]x0
set x="-12" write $char(0)]]x1
set x="---123" write $char(0)]]x0
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).
number?What is the "correct" way to check whether or not a string is a
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 428 |
Nodes: | 16 (2 / 14) |
Uptime: | 111:18:55 |
Calls: | 9,053 |
Files: | 13,395 |
Messages: | 6,016,121 |