Teaching software development? Using LiveCode to build your curriculum? This is the forum for you.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
andresdt
- Posts: 146
- Joined: Fri Aug 16, 2019 7:51 pm
Post
by andresdt » Thu Mar 04, 2021 2:55 pm
Hello
I share with you this function that works perfectly for me. Determine if the value of a variable is a numerical value according to the ECMA-404 standard.
The function code:
Code: Select all
function isNumericValue pValue
local tFirstChar
put char 1 of pValue into tFirstChar
if tFirstChar is 0 and char 2 of pValue is not "." or tFirstChar is not in "-0123456789"
then return false
# Scientific notifications
if "e" is in pValue then
set the itemdel to "e"
if the number of items of pValue is 2 and char 1 of pValue is not in".e" and char -1 of pValue is not in".e" then
if char 1 of item 2 of pValue is in "+-" then
delete char 1 of item 2 of pValue
end if
put the item 1 of pValue & the item 2 of pValue into pValue
else
return false
end if
end if
split pValue by "."
if the number of elements of pValue > 2 then return false
if pValue[ 2 ] is empty then return pValue[ 1 ] is an integer
return pValue[ 1 ] is an integer and pValue[ 2 ] is an integer
end isNumericValue
-
andresdt
- Posts: 146
- Joined: Fri Aug 16, 2019 7:51 pm
Post
by andresdt » Thu Mar 04, 2021 10:53 pm
I ran into a bug in the function code.
Well, since when evaluating the number 0 it returned false.
fixed
Code: Select all
function isNumericValue pValue
local tFirstChar
put char 1 of pValue into tFirstChar
if pValue is not an number then return false
put char 1 of pValue into tFirstChar
if tFirstChar is 0 and char 2 of pValue is not "." and the length of pValue is not 1 or tFirstChar is not in "-0123456789"
then return false
# Scientific notifications
if "e" is in pValue then
set the itemdel to "e"
if the number of items of pValue is 2 and char 1 of pValue is not in".e" and char -1 of pValue is not in".e" then
if char 1 of item 2 of pValue is in "+-" then
delete char 1 of item 2 of pValue
end if
put the item 1 of pValue & the item 2 of pValue into pValue
else
return false
end if
end if
split pValue by "."
if the number of elements of pValue > 2 then return false
if pValue[ 2 ] is empty then return pValue[ 1 ] is an integer
return pValue[ 1 ] is an integer and pValue[ 2 ] is an integer
end isNumericValue
-
andresdt
- Posts: 146
- Joined: Fri Aug 16, 2019 7:51 pm
Post
by andresdt » Fri Mar 05, 2021 3:17 pm
I was reviewing the function and I noticed that it could be optimized a lot. Since if I use the LiveCode function (value is a number) I would only have to worry about the numbers that start with 0 and are not 0 or 0. # and the LiveCode constants that are numbers and other strange ones like infinity. So this is how the new version looks it is considerably smaller and faster.
Code: Select all
function _isNumericValue pValue
if pValue is not a number then return false
if char 1 of pValue is 0 and char 2 of pValue is not "." and the length of pValue is not 1 or char 1 of pValue is not in "-0123456789"
then return false
return true
end _isNumericValue
-
Thierry
- VIP Livecode Opensource Backer
- Posts: 875
- Joined: Wed Nov 22, 2006 3:42 pm
Post
by Thierry » Fri Mar 05, 2021 3:38 pm
andresdt wrote: ↑Fri Mar 05, 2021 3:17 pm
....
So this is how the new version looks it is considerably smaller and faster.
Code: Select all
function _isNumericValue pValue
if pValue is not a number then return false
if char 1 of pValue is 0 and char 2 of pValue is not "." and the length of pValue is not 1 or char 1 of pValue is not in "-0123456789"
then return false
return true
end _isNumericValue
or a bit smaller and probably easier to read?
Code: Select all
function _isNumericValue pValue
if pValue is not a number then return false
return char 1 of pValue is 0 \
and char 2 of pValue is not "." \
and the length of pValue is not 1 \
or char 1 of pValue is not in "-0123456789"
end _isNumericValue
Kind regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
andresdt
- Posts: 146
- Joined: Fri Aug 16, 2019 7:51 pm
Post
by andresdt » Fri Mar 05, 2021 4:38 pm
Thanks @Thierry.
In its version, for it to work correctly you must put everything that is returned in the second line between parentheses. Then evaluate whether everything in parentheses is false. Since like this , the opposite of what we want to obtain returns us. (For example for 2 it returns false and 002 returns true.)
In this way we can also put the first (if) on this line and it is even smaller. Also, it is a few milliseconds faster.
Thanks again. This is what it would look like:
Code: Select all
function _isNumericValue pValue
return pValue is a number and (char 1 of pValue is 0 \
and char 2 of pValue is not "." \
and the length of pValue is not 1 \
or char 1 of pValue is not in "-0123456789") is false
end _isNumericValue
-
Thierry
- VIP Livecode Opensource Backer
- Posts: 875
- Joined: Wed Nov 22, 2006 3:42 pm
Post
by Thierry » Fri Mar 05, 2021 4:55 pm
Hi,
in fact, I didn't check the validity of your function; I did trust you
just rewriting your code a tiny bit....
this time, I drop the last part "is false" and add not before (
Code: Select all
function _isNumericValue pValue
return pValue is a number \
and not ( char 1 of pValue is 0 \
and char 2 of pValue is not "." \
and the length of pValue is not 1 \
or char 1 of pValue is not in "-0123456789")
end _isNumericValue
and I don't think you'll win few milliseconds,
but the code is certainly more beautiful.
So, what's next
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!