Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.mumps

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2009, 03:48 PM
David Hubball
Guest
 
Posts: n/a
Default Checking if a Mumps variable is a number or not?

Hi

Would anyone know of a better way to check if a variable is a number.
I've written the code below which works set a variable to equal an
empty string if its not a number.

; Test program to check that a variable is either
; a whole or a decimal number. If not then set number to equal "
;
S NUMBER="345"

I NUMBER'?1N.N d
.. I NUMBER'?1N.N1"."1N.N S NUMBER=""; check if decimal number
.. q

W NUMBER

Q

Much appreciated if anyone knows of a better (or tried and tested) way
of checking if variable NUMBER is actually a number or not.

Cheers
David
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 10-01-2009, 05:26 PM
OldMster
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

Well, it depends on what you mean by number....
If you want to confirm it is a real, rational number with no leading
0's, no trailing 0's, no unnecesary + at the beginning then:
IF +NUMBER'=NUMBER W "NOT A NUMBER"
would work.
If you want to confirm that NUMBER only contains characters valid for
numbers, but want leading/trailing zeros and + to be valid, you could
use:

IF $TR(NUMBER,"1234567890.+-","")'="" W "NOT A NUMBER"

Would work, but it would still allow for multiple ., -, or +, or
misplaced - or + which might cause you grief, so you might need

IF $TR(NUMBER,"1234567890.+-","")="",$l(NUMBER,".")<3,$l(NUMBER,"-")
<3,$l(NUMBER,"+")<3,$f(NUMBER,"-")<3,$f(NUMBER,"+")<3 w "IS A NUMBER"

(assuming you want any +- only at the beginning of the number)

Just be cautious if you are using the 'loose' check, and then use the
value for a subscript - if it doesn't pass the 'tight' check, it will
be ASCII collated in the array, not numerically collated.

Mark


On Oct 1, 10:48*am, David Hubball <davidhubb...@talktalk.net> wrote:
> Hi
>
> Would anyone know of a better way to check if a variable is a number.
> I've written the code below which works set a variable to equal an
> empty string if its not a number.
>
> ; Test program to check that a variable is either
> ; a *whole or a decimal number. If not then set number to equal "
> ;
> S NUMBER="345"
>
> I NUMBER'?1N.N d
> . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number
> . q
>
> W NUMBER
>
> Q
>
> Much appreciated if anyone knows of a better (or tried and tested) way
> of checking if variable NUMBER is actually a number or not.
>
> Cheers
> David


Reply With Quote
  #3 (permalink)  
Old 10-01-2009, 09:23 PM
Julius Kavay
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

David Hubball wrote:

>
> Would anyone know of a better way to check if a variable is a number.


> S NUMBER="345"
>
> I NUMBER'?1N.N d
> . I NUMBER'?1N.N1"."1N.N S NUMBER=""; check if decimal number
> . q


> Much appreciated if anyone knows of a better (or tried and tested) way
> of checking if variable NUMBER is actually a number or not.


It depends on how you define a number. Signed, unsigned, scientific,
canonical...

Check for unsigned integers/reals:
If NUM?.n1(1"."1.n,.n)

Check for signed/unsigned integers/reals
If NUM?.(1"-",1"+").n1(1"."1.n,.n)

Check for signed/unsigned integers/reals/scientific
If NUM?.(1"-",1"+").n1(1"."1.n,.n).1(1(1"E",1"e").1(1"-",1"+")1.3n)

Additionally, a
If +NUM=NUM checks, if NUM is in canonical form.

hth and
have a nice day
Julius

--
(my real mail address ends with a .net tld)
Reply With Quote
  #4 (permalink)  
Old 10-01-2009, 09:57 PM
al.veliev
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

On Oct 1, 6:48*pm, David Hubball <davidhubb...@talktalk.net> wrote:
> Hi
>
> Would anyone know of a better way to check if a variable is a number.
> I've written the code below which works set a variable to equal an
> empty string if its not a number.
>
> ; Test program to check that a variable is either
> ; a *whole or a decimal number. If not then set number to equal "
> ;
> S NUMBER="345"
>
> I NUMBER'?1N.N d
> . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number
> . q
>
> W NUMBER
>
> Q
>
> Much appreciated if anyone knows of a better (or tried and tested) way
> of checking if variable NUMBER is actually a number or not.
>
> Cheers
> David


Like previous authors:

w Num, " is ",$s(+Num:"",1:"not "),"number".

And that is enough. If it is not number, it will not be calculated in
ANY form.
But if you want to check the FORM of number, look the previous
examples.
Worse with literals. The completely backward standard defines only
USASC 7bit reactions so better to define own functions and templates
so if You will check another codepage. International values with the ?
operation it will have always false results.
Reply With Quote
  #5 (permalink)  
Old 10-01-2009, 11:38 PM
Julius Kavay
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

al.veliev wrote:

>
> w Num, " is ",$s(+Num:"",1:"not "),"number".
>
> And that is enough. If it is not number, it will not be calculated in
> ANY form.


Set Num=0
w Num, " is ",$s(+Num:"",1:"not "),"number".

0 is not number.


Set Num="2 banana and 3 apples"
w Num, " is ",$s(+Num:"",1:"not "),"number".

2 banana and 3 apples is number

Anyway, I have to correct my grasp about numbers...
and have a nice day
Julius


--
(my real mail address ends with a .net tld)
Reply With Quote
  #6 (permalink)  
Old 10-02-2009, 03:42 AM
dahakon
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?


> Would anyone know of a better way to check if a variable is a number.


Try:
i $c(0)]]string w !,"string is either null or a number"

+string=string works for most cases, but will throw errors on numeric
overflow (like with string="1E999").
Reply With Quote
  #7 (permalink)  
Old 10-02-2009, 05:44 AM
rms
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote:
> > Would anyone know of a better way to check if a variable is a number.

>
> Try:
> i $c(0)]]string w !,"string is either null or a number"
>
> +string=string works for most cases, but will throw errors on numeric
> overflow (like with string="1E999").


Nice!
Reply With Quote
  #8 (permalink)  
Old 10-02-2009, 08:28 AM
David Hubball
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

On 2 Oct, 06:44, rms <suba...@gmail.com> wrote:
> On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote:
>
> > > Would anyone know of a better way to check if a variable is a number.

>
> > Try:
> > i $c(0)]]string w !,"string is either null or a number"

>
> > +string=string works for most cases, but will throw errors on numeric
> > overflow (like with string="1E999").

>
> Nice!


I love this short solution :-
i $c(0)]]string

but it does not work with say -1.2 or with a minus or plus sign at the
front of it.

cheers
David
Reply With Quote
  #9 (permalink)  
Old 10-02-2009, 05:59 PM
OldMster
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

That is cool indeed. It does work with "-1.2", but not with "+1.2".
Mark


On Oct 2, 3:28*am, David Hubball <davidhubb...@talktalk.net> wrote:
> On 2 Oct, 06:44, rms <suba...@gmail.com> wrote:
>
> > On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote:

>
> > > > Would anyone know of a better way to check if a variable is a number.

>
> > > Try:
> > > i $c(0)]]string w !,"string is either null or a number"

>
> > > +string=string works for most cases, but will throw errors on numeric
> > > overflow (like with string="1E999").

>
> > Nice!

>
> I love this short solution :-
> i $c(0)]]string
>
> but it does not work with say -1.2 or with a minus or plus sign at the
> front of it.
>
> cheers
> David


Reply With Quote
  #10 (permalink)  
Old 10-02-2009, 06:28 PM
OldMster
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

My testing with Cache indicates the only advantage it has over +NUMBER
is that it doesn't error with 1E99. Non-canonical numbers still say
they are 'not numbers', which is technically correct, but may not be
what the poster was looking for.

USER>f x=1,-1.2,0,"a","","1..2","1E99","+1.2","01","1.0" w !,x,?5,$c
(0)]]x

1 1
-1.2 1
0 1
a 0
1
1..2 0
1E99 0
+1.2 0
01 0
1.0 0

On Oct 2, 12:59*pm, OldMster <msi...@verizon.net> wrote:
> That is cool indeed. *It does work with "-1.2", but not with "+1.2".
> Mark
>
> On Oct 2, 3:28*am, David Hubball <davidhubb...@talktalk.net> wrote:
>
>
>
> > On 2 Oct, 06:44, rms <suba...@gmail.com> wrote:

>
> > > On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote:

>
> > > > > Would anyone know of a better way to check if a variable is a number.

>
> > > > Try:
> > > > i $c(0)]]string w !,"string is either null or a number"

>
> > > > +string=string works for most cases, but will throw errors on numeric
> > > > overflow (like with string="1E999").

>
> > > Nice!

>
> > I love this short solution :-
> > i $c(0)]]string

>
> > but it does not work with say -1.2 or with a minus or plus sign at the
> > front of it.

>
> > cheers
> > David


Reply With Quote
  #11 (permalink)  
Old 10-03-2009, 10:56 PM
al.veliev
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

On Oct 2, 12:23*am, Julius Kavay
<ka...@gmx.net.valid.address.changed.to.invalid> wrote:
> David Hubball wrote:
>
> > Would anyone know of a better way to check if a variable is a number.
> > S NUMBER="345"

>
> > I NUMBER'?1N.N d
> > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number
> > . q
> > Much appreciated if anyone knows of a better (or tried and tested) way
> > of checking if variable NUMBER is actually a number or not.

>
> It depends on how you define a number. Signed, unsigned, scientific,
> canonical...
>
> Check for unsigned integers/reals:
> If NUM?.n1(1"."1.n,.n)
>
> Check for signed/unsigned integers/reals
> If NUM?.(1"-",1"+").n1(1"."1.n,.n)
>
> Check for signed/unsigned integers/reals/scientific
> If NUM?.(1"-",1"+").n1(1"."1.n,.n).1(1(1"E",1"e").1(1"-",1"+")1.3n)
>
> Additionally, a
> If +NUM=NUM checks, if NUM is in canonical form.
>
> hth and
> have a nice day
> Julius
>
> --
> (my real mail address ends with a .net tld)


(1234) is in accounting a form of -1234.
It does not in any match and is not calculated ))))

Reply With Quote
  #12 (permalink)  
Old 10-06-2009, 04:03 AM
OldMster
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

Heh, since my degree is in accounting, I can say this - *NO*
Accounting number is a real number!
:-)
Mark


On Oct 3, 5:56*pm, "al.veliev" <al.vel...@gmail.com> wrote:
> On Oct 2, 12:23*am, Julius Kavay
>
>
>
>
>
> <ka...@gmx.net.valid.address.changed.to.invalid> wrote:
> > David Hubball wrote:

>
> > > Would anyone know of a better way to check if a variable is a number.
> > > S NUMBER="345"

>
> > > I NUMBER'?1N.N d
> > > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number
> > > . q
> > > Much appreciated if anyone knows of a better (or tried and tested) way
> > > of checking if variable NUMBER is actually a number or not.

>
> > It depends on how you define a number. Signed, unsigned, scientific,
> > canonical...

>
> > Check for unsigned integers/reals:
> > If NUM?.n1(1"."1.n,.n)

>
> > Check for signed/unsigned integers/reals
> > If NUM?.(1"-",1"+").n1(1"."1.n,.n)

>
> > Check for signed/unsigned integers/reals/scientific
> > If NUM?.(1"-",1"+").n1(1"."1.n,.n).1(1(1"E",1"e").1(1"-",1"+")1.3n)

>
> > Additionally, a
> > If +NUM=NUM checks, if NUM is in canonical form.

>
> > hth and
> > have a nice day
> > Julius

>
> > --
> > (my real mail address ends with a .net tld)

>
> (1234) is in accounting a form of -1234.
> It does not in any match and is not calculated ))))


Reply With Quote
  #13 (permalink)  
Old 10-27-2009, 11:14 PM
al.veliev
Guest
 
Posts: n/a
Default Re: Checking if a Mumps variable is a number or not?

On Oct 6, 6:03*am, OldMster <msi...@verizon.net> wrote:
> Accounting number is a real number!

Yes, the number is real, but the result sometimes is a VERY
fictitious
And do not forget please that we are in different countries and our
standards are DIFFERENT. And numbers are different too(I mean its show
from the input stream for example). In this context I suppose that the
best result would be with the user function which analyzes stream byte-
by-byte and returns true or false depending of the results of
comparison.
In any case all this topic is a very good illustration how the cavalry
jump may be a hazard for good programming.
The very good topic and question indeed - thanks to David for his
question and all who answered.

Alexander.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:28 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.