Whole numbers

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

EddieLee
Posts: 118
Joined: Thu Oct 25, 2018 1:06 pm

Whole numbers

Post by EddieLee » Thu Sep 03, 2020 11:53 pm

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?
Eddie :D

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Whole numbers

Post by Opaquer » 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 :)

EddieLee
Posts: 118
Joined: Thu Oct 25, 2018 1:06 pm

Re: Whole numbers

Post by EddieLee » Fri Sep 04, 2020 2:01 am

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
Eddie :D

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Whole numbers

Post by Opaquer » Fri Sep 04, 2020 3:28 am

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!

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: Whole numbers

Post by rkriesel » Fri Sep 04, 2020 3:54 am

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

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Whole numbers

Post by Opaquer » Fri Sep 04, 2020 4:13 am

Ah ha, there was an easier way to do it! TIL - that will come in very handy for me too! Thanks rkriesel!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Whole numbers

Post by dunbarx » Fri Sep 04, 2020 5:12 am

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
Last edited by dunbarx on Fri Sep 04, 2020 8:44 pm, edited 2 times in total.

stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Whole numbers

Post by stam » Fri Sep 04, 2020 6:36 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Whole numbers

Post by dunbarx » Fri Sep 04, 2020 8:41 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Whole numbers

Post by jacque » 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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Whole numbers

Post by stam » Sat Sep 05, 2020 6:29 pm

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... ;)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Whole numbers

Post by dunbarx » Sat Sep 05, 2020 11:50 pm

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

stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Whole numbers

Post by stam » Sun Sep 06, 2020 12:08 am

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...

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Whole numbers

Post by jacque » Sun Sep 06, 2020 7:45 am

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Whole numbers

Post by dunbarx » Sun Sep 06, 2020 2:00 pm

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”