Page 1 of 3

Whole numbers

Posted: Thu Sep 03, 2020 11:53 pm
by EddieLee
Hi all,

Is there any livecode function or code that can detect if a variable is a whole number?

Code: Select all

Put a * b into c
If c is a whole number then
.......
Else
.......
End if
I can’t use contains “.” In this context or is there a decimal identifier as well in livecode?

Re: Whole numbers

Posted: Fri Sep 04, 2020 1:34 am
by Opaquer
The trunc function is super handy in that it removes everything after a decimal point, so super useful if you only need to look at the number before the decimal point. Because of that, you can do something like this to see if the trunc of a number is equal to a number, which means you've got a whole number on your hands!

Code: Select all

if c = trunc(c) then
answer "This is a whole number"
else
answer "This is not a whole number"
end if
That should do the trick for you! Not sure if there's better ways to do it, but that should definitely be a great starting point :)

Re: Whole numbers

Posted: Fri Sep 04, 2020 2:01 am
by EddieLee
Opaquer wrote:
Fri Sep 04, 2020 1:34 am
The trunc function is super handy in that it removes everything after a decimal point, so super useful if you only need to look at the number before the decimal point. Because of that, you can do something like this to see if the trunc of a number is equal to a number, which means you've got a whole number on your hands!

Code: Select all

if c = trunc(c) then
answer "This is a whole number"
else
answer "This is not a whole number"
end if
That should do the trick for you! Not sure if there's better ways to do it, but that should definitely be a great starting point :)
Hi,

So does it mean this way?

Code: Select all

Put 18.80 into c
if c = trunc(c) then
answer "This is a whole number"
else
answer "This is not a whole number"
end if
So this will answer “this is not a whole number”? If it does show that then this is what I wanted so that i know whether variable c is a whole number

Re: Whole numbers

Posted: Fri Sep 04, 2020 3:28 am
by Opaquer
Yup, exactly that! Basically, if you have a number, like 18.80, the trunc function gets rid of anything after the decimal point. So in this case, if you have c=18.80, then trunc(c) will be 18. Since 18 <> 18.80, we know that c is not a whole number. On the other hand, if we put 15 into c, trunc(c) now will be 15 - since there's no decimal point we just end up with the same number. Since c=trunc(c) [15=15], we know that in this case, c is a whole number!

Re: Whole numbers

Posted: Fri Sep 04, 2020 3:54 am
by rkriesel
EddieLee wrote:
Thu Sep 03, 2020 11:53 pm
Is there any livecode function or code that can detect if a variable is a whole number?

Code: Select all

if c is an integer
Hi, Eddie. See "integer" in the dictionary.
-- Dick

Re: Whole numbers

Posted: Fri Sep 04, 2020 4:13 am
by Opaquer
Ah ha, there was an easier way to do it! TIL - that will come in very handy for me too! Thanks rkriesel!

Re: Whole numbers

Posted: Fri Sep 04, 2020 5:12 am
by dunbarx
If you are asking if a number is either an integer or a whole number, then, and this may be purely academic, the integer keyword will return "true" for both "6" and "6.00000".

A "whole number" (and maybe even an integer :wink: ) expressed as, say, "6.00000", might not, however, be permitted in whatever gadget you are working on. So after determining that the number has no decimal or fractional VALUE, you may well want to also determine that it does not contain a decimal point at all.

And watch out about the dictionary definition. It says:
If the value contains digits (and an optional minus sign) and no other characters, it is an integer.
Implying that zeroes can be anywhere in the string, like "00011000". But it also categorically states that a decimal point, which is NOT a digit, is not allowed. I actually agree with this completely, that "1.0" is not an integer, though "1" certainly is. This is semantics, I suppose, but technically the dictionary is wrong.
Craig

Re: Whole numbers

Posted: Fri Sep 04, 2020 6:36 pm
by stam
dunbarx wrote:
Fri Sep 04, 2020 5:12 am
I actually agree with this completely, that "1.0" is not an integer, though "1" certainly is. This is semantics, I suppose, but technically the dictionary is wrong.
Craig

Agreed - i think this should actually be bug report personally. Almost all languages would infer that 1.0 is not an integer.

Re: Whole numbers

Posted: Fri Sep 04, 2020 8:41 pm
by dunbarx
That's two of us. Anyone else think this is important? That "01" and "1.0" are integers? It doesn't matter in doing arithmetic, but seems so very wrong.

Oh yes, do we think "1.0" is a "whole number"?

Craig

Re: Whole numbers

Posted: Sat Sep 05, 2020 6:21 pm
by jacque
I agree it's worth a bug report. There's an easy workaround so I'm not sure how much priority it would receive but it may as well be in the queue.

Re: Whole numbers

Posted: Sat Sep 05, 2020 6:29 pm
by stam
jacque wrote:
Sat Sep 05, 2020 6:21 pm
I agree it's worth a bug report. There's an easy workaround so I'm not sure how much priority it would receive but it may as well be in the queue.
Agree it’s low priority but unless reported it will never get addressed... if no one has submitted a bug report I’ll do it at some point after work.
dunbarx wrote:
Fri Sep 04, 2020 8:41 pm
Oh yes, do we think "1.0" is a "whole number"?
I think the programmatic equivalent of “whole number” is the unsigned Integer (i.e. a positive integer).
Mathematically an “integer” would be a signed integer (I.e. positive or negative integer).

So no, I don’t think so. 1.0 may be in real terms the same as 1, but the “.0” implies it can take other decimal values as well, hence it’s a Rational number (float or double in programmatic speak), or at least that’s what I think... ;)

Re: Whole numbers

Posted: Sat Sep 05, 2020 11:50 pm
by dunbarx
Jacque.
There's an easy workaround
Sure, I assume you mean a function for tNumber like:

Code: Select all

if tNumber is an integer then return trunc(tNumber)
My real question is whether anyone thinks this is important, entirely apart from the departure from what an integer is or should be.
I do not think that any conceivable arithmetical operation would be affected. Does anyone have such a case?

And maybe this loose interpretation might break somebody's app concerning what an integer is, as I mentioned in my first post. The "right" and expected answer might be "6", but the user might enter "06.00", and LC passes it as true.

Craig

Re: Whole numbers

Posted: Sun Sep 06, 2020 12:08 am
by stam
dunbarx wrote:
Sat Sep 05, 2020 11:50 pm
My real question is whether anyone thinks this is important, entirely apart from the departure from what an integer is or should be.
Actually you probably have a point there... as variable typing isn't a thing in livecode it won't really affect any arithmetical operation.
And display-wise you can modify it to return the trunc() anyway.

I guess it's only in the event you want to use introspection to see if a variable is of type 'integer' or not that this may be important... but in the sphere of normal LiveCode operations this may be a very rare need...

Re: Whole numbers

Posted: Sun Sep 06, 2020 7:45 am
by jacque
Actually I was thinking:

Code: Select all

if tNumber is an integer and "." is not in tNumber then 
just in case the format matters. But probably most of the time it won't.

Re: Whole numbers

Posted: Sun Sep 06, 2020 2:00 pm
by dunbarx
Jacque.
if tNumber is an integer and "." is not in tNumber then
But then "003" would pass as an integer. By using "trunc", that oddity is removed, as well as ".".

Craig