Not a number is a number, problem

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Not a number is a number, problem

Post by Davidv » Fri Jan 15, 2021 8:28 am

List1 holds some data which will be used for updating List2.

List2 is, in principle, a pure subset of List1 in terms of its 3-character keys.

The code is thus a fairly simple step through of both lists, updating when the keys are equal. Logically, it goes in pseudocode:
if key(List1) < key(List2) then move to next List1 key
If key(List1) = key(List2) then update the List 2 line, move to next List2 key
else[ key(List1) > key(List2)] warn the user. -- This is an unlikely event, but when I ran it, I got a user warning on a key which was in both lists. The key in question was the string "NAN".

There is nothing useful I can do to change the key. It is the Australian Stock Exchange code for the company Nanosonics.

This is the relevant code fragment:
put field "ASX Code" into tVar
if ((item 1 of inLine) > tVar) then -- inLine is a comma-delimited string variable from a repeat for each loop

Further testing has shown that "item 1 of inLine" is treated as the string "NAN" while tVar containing "NAN" is treated as the number NaN (a number which is not a number, an amusement in itself). I will try using field "ASX Code" directly (a little less convenient in further code) rather than putting it into a variable which is I think where it is converted to a number. Pending that trial, my queries are:

1. Has anyone else encountered this problem, so have a solution for it?
2. Is there a way of forcing the second variable to be a string? I suspect there is but do not recall it. Ampersand an empty string to it?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Not a number is a number, problem

Post by richmond62 » Fri Jan 15, 2021 9:21 am

Everything in LiveCode is treated as a string.

In previous programming languages I learnt (c.f. BASIC) there was clear distinction made between numerical and string variables.

LiveCode does not distinguish between string variables and numeric variables.

This means that if you have a variable that contains ANY non-numerical characters LiveCode will treat it as a string.

Imagine, if you will, this sort of thing:

Code: Select all

put "N12" into XYZ
it should be obvious that LiveCode will treat XYZ as text and not a number.

You could do some 'stripping' like this:

Code: Select all

put "N12" into XYZ
delete the first char of XYZ
At which point XYZ = 12 so LiveCode will now see it as a number.

While in other languages (c.f. BASIC) it is impossible to put non-numerical stuff into a numerical variable,
and string variables have names that finish with a '$' sign.
-
Screenshot 2021-01-15 at 10.17.26.png
-
Screenshot 2021-01-15 at 10.24.58.png
-
In LiveCode you can "mix and match", but that comes at the price of the programmer having to be
clear as to what each variable contains.
Last edited by richmond62 on Fri Jan 15, 2021 5:12 pm, edited 1 time in total.

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

Re: Not a number is a number, problem

Post by dunbarx » Fri Jan 15, 2021 3:01 pm

Hi.

What Richmond said.

"NAN" is not quite a "Number that is not A Number", rather it is simply, "Not A Number". :wink:

LiveCode variables are typeless, and are typed at compile time purely based on context. But I am positive, since we have control down to the character level, and with chunk expressions so powerful, that a very simple solution can be found right away.

Please post a concrete example of a string that you want to be able to transform into something "usable". We will write a simple function that will handle everything from there on.

Craig

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Not a number is a number, problem

Post by Davidv » Sat Jan 16, 2021 1:44 am

richmond42 & dunbarx, thank you for your responses. I fear that my thread title and jocularity around NaN being a number may have misled you. While you are both fully correct, and I fully apprehend those things, my question related to the code fragment I presented in my original post:
put field "ASX Code" into tVar
if ((item 1 of inLine) > tVar)

which is something else. I can clarify that with worked examples.

In the multiline message box, run this:
put "NAN,fred" into aVar
put "NAN" into bVar
put isNumber(item 1 of aVar) && isNumber(bVar)

As you might expect, you will get "true true" in response, so you should have equality between those if you were to compare them directly.

Now try this short script (sorry about the lost indentation):
on xTest
put "NAN,HTH,42" into inLine
put "NAN" into aCode
if item 1 of inLine < aCode then
answer "item NAN < var NAN"
else
if item 1 of inLine = aCode then
answer "item NAN = var NAN"
else
answer "item NAN > var NAN"
put "NAN" = "NAN" into msg
end if
end if
end xTest

I get the dialog box saying "item NAN > var NAN", which is inconsistent (LiveCode Business 9.6.1 Build 15522). Reiterating from my earlier post, having the string "NAN" in the relevant field in the original code is not a choice, but how things are, it being the ticker code for the company Nanosonics (one way to avoid the problem would be to sell those shares :D ). Also, I do not really care if Livecode thinks it is comparing two instances of Not-a-Number, so long as it recognises they are the same thing. However, it does not, and there lies the problem.

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

Re: Not a number is a number, problem

Post by dunbarx » Sat Jan 16, 2021 5:33 am

David.

Is this just an academic exercise? "NAN" is not a native LC word (it is not in the dictionary) and "var" is just short for "variable".

It is true that "NAN" is greater than any number:

Code: Select all

answer NAN > 44 --returns "true"
but that is because any alpha char is greater than any number, because their ASCII values are greater. That is, "c" > "9".

On another note, please put all your code samples inside code brackets (select "/>" from the icons above).

I am frankly a bit lost on what you are trying to achieve, and what you are struggling with. Perhaps others are more astute. Anyway, I ask again to post a code snippet that is failing to do what you want it to. It is likely that LC is acting as it should in the comparisons of value. The trick for you probably lies simply in massaging the data into a LC-friendly form.

Craig

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Not a number is a number, problem

Post by Davidv » Sat Jan 16, 2021 9:33 am

Hi Craig, not at all an academic exercise, which I can explain further if need be. It is non-commercial but important to me. Thank you for the tip about code tags. Having only a few posts, I am unfamiliar with this forum's details despite having been registered since 2006.

Here are those code snippets again, properly tagged:
In the multiline message box, run

Code: Select all

put "NAN,fred" into aVar
put "NAN" into bVar
put isNumber(item 1 of aVar) && isNumber(bVar)
with expected results "true true".
In a script run (slightly simplified from my previous example)

Code: Select all

on xTest
 put "NAN,HTH,42" into inLine
 put "NAN" into aCode
 if item 1 of inLine < aCode then
  answer "NAN < NAN"
 else
  if item 1 of inLine = aCode then
   answer "NAN = NAN"
  else 
   answer "NAN > NAN"
   put "NAN" = "NAN" into msg
  end if
 end if
end xTest
with unexpected actual result "NAN > NAN" in the dialog then "true" in msg.

Please try these and advise whether you get the same results and, if so, any conjecture why they seem to be different?

The code I have given is a condensation of a repeat loop with lots of other stuff. There is one particular record, that which is keyed "NAN", where it fails as I perceive it. The code I have provided is sufficient to characterise the problem (no match of NAN to NAN), in that it reproduces it perfectly when I run it.

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

Re: Not a number is a number, problem

Post by dunbarx » Sat Jan 16, 2021 5:18 pm

OK, now I see.

The issue is with "NAN".

In our heads, the handler reads just fine, but the string "NAN" itself is fouling up the works. Substitute anything else, like "NANA" and the handler proceeds as it "should".

@Everyone else on the planet, since "NAN" is well known as the result of certain illegal arithmetic operations, but is not in the dictionary, what do we make of this? An undocumented feature that fouls up the thinking of otherwise nice people who use "42" in their posts, as if they are members of some secret society?

@David. You need to lose that string. Replace it with anything else and then restore if you need to. This should be easily manageable.

Craig

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

Re: Not a number is a number, problem

Post by dunbarx » Sat Jan 16, 2021 5:38 pm

I tried to find "NAN" and came up with:
http://forums.livecode.com/viewtopic.php?f=7&t=20636
and of interest:
https://ntgard.medium.com/why-nan-nan-3d41af988d30
The only mystery to me is where the OP in the forum thread saw "NAN" at all.

I vaguely recall seeing the debugger throw this string, but cannot duplicate it. Might it only have been a notification in earlier versions?

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Not a number is a number, problem

Post by SparkOut » Sat Jan 16, 2021 6:56 pm

I confirm the same issue with comparison of variables containing "NAN" failing when they should be identical literal strings on Windows LC 9.6.2-rc2

You can "solve" the issue by forcing evaluation of the variable thus:

Code: Select all

on xTest
   put "NAN,HTH,42" into inLine
   put "NAN" into aCode
   if value(item 1 of inLine) < aCode then
      answer "NAN < NAN"
   else
      if value(item 1 of inLine) = aCode then
         answer "NAN = NAN"
      else 
         answer "NAN > NAN"
         put "NAN" = "NAN" into msg
      end if
   end if
end xTest
but this is downright wrong. It is a bug, and needs to be reported.

Also, why does only the first operand need to have evaluation forced, and not the operand after the comparison operator?

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

Re: Not a number is a number, problem

Post by dunbarx » Sat Jan 16, 2021 7:48 pm

Sparkout.

First and foremost, how do we deal with an entity that has such backRoom influence, yet no presence at all in the lexicon?

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Not a number is a number, problem

Post by SparkOut » Sat Jan 16, 2021 7:58 pm

Hi Craig, I'm not sure what you mean "how do we deal with it"?
If I set a value to be a string literal, I want that value to be treated as a string literal.
This is a bug, needs to be reported and fixed in the engine, end of story.
It matters not whether there's a reference to NaN or NAN or Not a Number in the engine/lexicon/daily newspaper.
The "helpful" evaluation of variable contents which are somewhat ambiguous or not defined in LiveCode is welcome and I don't want to get into strict typing and casting of variables, but a string literal "NAN" OR ANYTHING ELSE defined as such, should never be hijacked by the engine's interpretation. If we wanted to use a "not a number" token "NAN" then this should only be done in specific functions with specific syntax.
I don't have to avoid use of "width" as a string literal because there is a property with that name, or "false" because there is a boolean constant.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Not a number is a number, problem

Post by bn » Sat Jan 16, 2021 8:09 pm

For your pleasure:

Code: Select all

on mouseUp
   put "Infinity" into tInf
   put "NAN" into tNan
   put tInf > tNan into tResult1
   if tResult1 then
      put tNan & " is larger than " & tInf into tResult1
   end if
   put tNan > tInf into tResult2
   if tResult2 then
      put tInf & " is larger than " & tNan  into tResult2
   end if
   put "2e2" < tInf into tResult3
   answer tResult1 & cr & tResult2 & cr & cr & "which is kind of "  & tResult3
end mouseUp
Kind regards
Bernd

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

Re: Not a number is a number, problem

Post by dunbarx » Sat Jan 16, 2021 10:00 pm

If I set a value to be a string literal, I want that value to be treated as a string literal.
Sparkout. II am not arguing that, I am with you:

Code: Select all

answer zero = 0 --true, as it should be
answer "zero" = 0 --false, as it should be
I posted the "NAN" thing as an anomaly at best, and agree with you, a bug at worst. I really was only asking where in the LC world is "NAN"?

As Bernd points out, "inf", or "infinity", is an odd beast, but at least spoken about in the dictionary.

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Not a number is a number, problem

Post by SparkOut » Sun Jan 17, 2021 1:14 am

I must confess not having executed Bernd's script, I suspected it was a demonstration of ASCII comparison rather than token evaluation.
"NAN" > "Infinity" because of alphabetical order.
I am not able to check what that script actually demonstrates, but I suspect there's more to it.
But as for "NAN" being a token or keyword like "Infinity" - no there's no reference, and I'm not even sure whether there would be behind the scenes.
I am floored by this.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Not a number is a number, problem

Post by bn » Sun Jan 17, 2021 1:30 am

OK,

maybe I have to explain.

Infinitiy > NAN
And at the same time
NAN > Infinity

Both are mathematically evaluated and both comparisons return true which is what I was alluding to. But who is to argue with infinity...

Furthermore "2e2" is a number in Livecode and is smaller than Infinity.

I just wanted to show that there are more surprises in Livecode's handling of Numbers /automatic number detection.

I do agreee that "NAN" should be documented.

Kind regards
Bernd

Post Reply

Return to “Talking LiveCode”