Re: Troubles with inequality
Thanks - I don’t completely understand this as an economical solution, but I think what you’re saying is that any number, even stored in a variable, can be treated as a string - then things can be compared character by character. This is almost the solution I adopted, but I used fields since they stay as strings from the off. Graham On 7 Feb 2015, at 02:38, Kay C Lan lan.kc.macm...@gmail.com wrote: On Thu, Feb 5, 2015 at 7:02 AM, Graham Samuel livf...@mac.com wrote: Thanks Jacque, a gold mine of information as usual. But it’s pretty obscure, isn’t it? All this started for me because I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were ‘engineering equal’ as it were - say to six places of decimals. So I tried to truncate them to that length and then compare them. More fool me. But now I think I know what should be done, thanks to you - and to everyone else who replied. Graham Why not a simple: (in the multline msg box) put 1.901 into a put 1.902 into b put a = b into msg put cr (char 1 to 8 of a = char 1 to 8 of b) after msg put 1.0 into a put 1.01 into b put cr (a = b) after msg put cr (char 1 to 8 of a = char 1 to 8 of b) after msg 8 characters is to 6 decimal places and the fact that LC automatically casts to numbers where it can nicely solves the problem that although string 1.0 1.00 in your case you are interested in the numeric value so you get the correct answer. LCs chunking capabilities are brilliant, we sometimes just need to remember that a number is a word in LC, and words have characters, and characters can be compared. The nice thing about LC is when presented with a number Cat you don't have to skin it with only number solutions; you can engineer any result you want ;-) ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
On Thu, Feb 5, 2015 at 7:02 AM, Graham Samuel livf...@mac.com wrote: Thanks Jacque, a gold mine of information as usual. But it’s pretty obscure, isn’t it? All this started for me because I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were ‘engineering equal’ as it were - say to six places of decimals. So I tried to truncate them to that length and then compare them. More fool me. But now I think I know what should be done, thanks to you - and to everyone else who replied. Graham Why not a simple: (in the multline msg box) put 1.901 into a put 1.902 into b put a = b into msg put cr (char 1 to 8 of a = char 1 to 8 of b) after msg put 1.0 into a put 1.01 into b put cr (a = b) after msg put cr (char 1 to 8 of a = char 1 to 8 of b) after msg 8 characters is to 6 decimal places and the fact that LC automatically casts to numbers where it can nicely solves the problem that although string 1.0 1.00 in your case you are interested in the numeric value so you get the correct answer. LCs chunking capabilities are brilliant, we sometimes just need to remember that a number is a word in LC, and words have characters, and characters can be compared. The nice thing about LC is when presented with a number Cat you don't have to skin it with only number solutions; you can engineer any result you want ;-) ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Graham wrote: I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were 'engineering equal' as it were - say to six places of decimals. Perhaps performing a statround() on the numbers prior to checking equality might help. Jim Lambert ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
So here’s what I came up with. Pretty simple. on mouseUp put 5.226 into myVar set the numberFormat to 0.00 add .001 to myVar put myvar cr into myString set the numberFormat to 0.000 put myvar after myString put myString end mouseUp I get 5.23 5.227 Clearly the value in the variable is not being affected at all. This makes me feel a lot better. The moral of this is, always set the numberFormat appropriately prior to calculations of any kind, and don’t use the contents of fields in calculations. Bob S On Feb 4, 2015, at 15:20 , J. Landman Gay jac...@hyperactivesw.com wrote: It's obscure, yes, though the dictionary has this: Important! Changing the numberFormat does not automatically change the format of a number that's already in a container. It affects numbers only when they are calculated and then displayed or used as strings. Otherwise, the number retains its full numeric precision. The already in a container should be more precise though and specify in a variable. Fields are containers, but they only hold strings. On 2/4/2015 5:02 PM, Graham Samuel wrote: Thanks Jacque, a gold mine of information as usual. But it’s pretty obscure, isn’t it? All this started for me because I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were ‘engineering equal’ as it were - say to six places of decimals. So I tried to truncate them to that length and then compare them. More fool me. But now I think I know what should be done, thanks to you - and to everyone else who replied. Graham On 4 Feb 2015, at 23:44, J. Landman Gay jac...@hyperactivesw.com wrote: On 2/4/2015 3:01 PM, Graham Samuel wrote: So really, if I want an uncomplicated string of characters derived from a number via setting the numberFormat and then doing a calculation, how do I get it? You can turn it into a string by putting empty after it: put 1.5 into tVar -- number set the numberformat to 0.00 add 1 to tVar -- still a number put tVar into fld 1 -- numberformat applied now, field contains a string 2.55 add 1 to tVar -- still a number, contains 3.5 put empty after tVar -- numberformat applies, tVar is a string -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
It's almost like there should be a cast() function to change the type of a variable, although that would be most unLivecodeLike. Pete lcSQL Software http://www.lcsql.com Home of lcStackBrowser http://www.lcsql.com/lcstackbrowser.html and SQLiteAdmin http://www.lcsql.com/sqliteadmin.html On Wed, Feb 4, 2015 at 3:20 PM, J. Landman Gay jac...@hyperactivesw.com wrote: It's obscure, yes, though the dictionary has this: Important! Changing the numberFormat does not automatically change the format of a number that's already in a container. It affects numbers only when they are calculated and then displayed or used as strings. Otherwise, the number retains its full numeric precision. The already in a container should be more precise though and specify in a variable. Fields are containers, but they only hold strings. On 2/4/2015 5:02 PM, Graham Samuel wrote: Thanks Jacque, a gold mine of information as usual. But it’s pretty obscure, isn’t it? All this started for me because I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were ‘engineering equal’ as it were - say to six places of decimals. So I tried to truncate them to that length and then compare them. More fool me. But now I think I know what should be done, thanks to you - and to everyone else who replied. Graham On 4 Feb 2015, at 23:44, J. Landman Gay jac...@hyperactivesw.com wrote: On 2/4/2015 3:01 PM, Graham Samuel wrote: So really, if I want an uncomplicated string of characters derived from a number via setting the numberFormat and then doing a calculation, how do I get it? You can turn it into a string by putting empty after it: put 1.5 into tVar -- number set the numberformat to 0.00 add 1 to tVar -- still a number put tVar into fld 1 -- numberformat applied now, field contains a string 2.55 add 1 to tVar -- still a number, contains 3.5 put empty after tVar -- numberformat applies, tVar is a string -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Graham. You might consider using the round function, with its precision set to whatever decimal value you wish, likely that value that you used to consider setting the numberformat. This will do almost the same thing, and trunc at that level, which is what I believe you thought the numberformat did. Almost, because note that the rounding at that level may introduce a complication for you. Craig Newman on mouseUp put 1.234567 into temp put 1.2345678 into aa set the numberFormat to 0.### put temp * 1 into temp put aa * 1 into aa answer aa = temp answer word 1 of aa = word 1 of temp answer word 1 of aa end mouseUp -Original Message- From: Colin Holgate co...@verizon.net To: How to use LiveCode use-livecode@lists.runrev.com Sent: Wed, Feb 4, 2015 10:16 am Subject: Re: Troubles with inequality The numberformat only affect the appearance of the number, not its value. If you want to compare them based on the appearance characters, you can compare them as strings instead of numbers. Try this: on mouseUp put 1.234567 into temp put 1.2345678 into aa set the numberFormat to 0.### put temp * 1 into temp put aa * 1 into aa answer aa = temp answer word 1 of aa = word 1 of temp answer word 1 of aa end mouseUp ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Round has a problem in that rounding could change the nature of something. For example, you want to see if 1.9995 is similar to 1.9994, but you end up checking 1.9 against 2.0. One nice addition would be to give Trunk the same second parameter that Round has. Then you would keep the nature of the string. ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Colin. That would be a great feature. But trunc() takes no second param. I will ask for one, though. Craig -Original Message- From: Colin Holgate co...@verizon.net To: How to use LiveCode use-livecode@lists.runrev.com Sent: Wed, Feb 4, 2015 11:56 am Subject: Re: Troubles with inequality Round has a problem in that rounding could change the nature of something. For example, you want to see if 1.9995 is similar to 1.9994, but you end up checking 1.9 against 2.0. One nice addition would be to give Trunk the same second parameter that Round has. Then you would keep the nature of the string. ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Fascinating! Up to this moment, it seemed to me that the **appearance** of a number in a variable or more particularly in a field, **is** the number - where is the space where the engine can put an extended value? Is there something hidden in the object, perhaps in word 2? I mean, I have always thought that setting the numberFormat ensured that the result of a subsequent calculation was in effect a character string whose length was determined by the numberFormat setting. From your code, it seems this is not the case. I don’t think this issue is tackled at all in the LC Dictionary, tho I may be wrong, as I have been about everything else on this topic. So really, if I want an uncomplicated string of characters derived from a number via setting the numberFormat and then doing a calculation, how do I get it? Do I have to copy out the characters of Word 1 one by one? Graham On 4 Feb 2015, at 16:14, Colin Holgate co...@verizon.net wrote: The numberformat only affect the appearance of the number, not its value. If you want to compare them based on the appearance characters, you can compare them as strings instead of numbers. Try this: on mouseUp put 1.234567 into temp put 1.2345678 into aa set the numberFormat to 0.### put temp * 1 into temp put aa * 1 into aa answer aa = temp answer word 1 of aa = word 1 of temp answer word 1 of aa end mouseUp ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Colin: But: I don’t see this. Say your target accuracy is six figures. You would us: put round(myNum1,6) into num1 put round(myNum2,6) into num2 So, for example, 3.1234569 would round to 3.123457 3.1234563 would round to 3.123456, which would be unequal, but 3.1234567 would round up to 3.123457 and show as equal. If you use too many digits, you will run into the limits of the binary number accuracy. If you do round(1.9,1) and round(2.0), you should be comparing 1.9 to 2.0. which would show as unequal. Bill On Feb 4, 2015, at 8:55 AM, Colin Holgate co...@verizon.net wrote: Round has a problem in that rounding could change the nature of something. For example, you want to see if 1.9995 is similar to 1.9994, but you end up checking 1.9 against 2.0. One nice addition would be to give Trunk the same second parameter that Round has. Then you would keep the nature of the string. ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Yes, my example wasn’t good. Interestingly, round(1.49) returns 1 and round (1.49) returns 2. ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
On 2/4/2015 3:01 PM, Graham Samuel wrote: Fascinating! Up to this moment, it seemed to me that the **appearance** of a number in a variable or more particularly in a field, **is** the number - where is the space where the engine can put an extended value? The number retains its full value as long as it remains in RAM. The numberformat only works when an operation is performed on it that converts it to a string. So, placing it into a field will cause it to become the visible representation of the number if you pull it back out of the field again later. Putting the number into a variable frequently turns it into a string unless the engine determines the variable is actually a number and retains it as such. put 12.5 into tVar -- still a number add 2 to tVar -- still a number put tVar into fld 1 -- tVar remains a number, field contains a string put x after tVar -- now it's a string Numberformat does only change the representation of the number, but is only applied when something turns that number into a string. put 1.5 into tVar -- number set the numberformat to 0.00 add 1 to tVar -- still a number put tVar into fld 1 -- numberformat applied here, field contains a string 2.56 add 1 to tVar -- still a number, contains 3.5 -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
It's obscure, yes, though the dictionary has this: Important! Changing the numberFormat does not automatically change the format of a number that's already in a container. It affects numbers only when they are calculated and then displayed or used as strings. Otherwise, the number retains its full numeric precision. The already in a container should be more precise though and specify in a variable. Fields are containers, but they only hold strings. On 2/4/2015 5:02 PM, Graham Samuel wrote: Thanks Jacque, a gold mine of information as usual. But it’s pretty obscure, isn’t it? All this started for me because I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were ‘engineering equal’ as it were - say to six places of decimals. So I tried to truncate them to that length and then compare them. More fool me. But now I think I know what should be done, thanks to you - and to everyone else who replied. Graham On 4 Feb 2015, at 23:44, J. Landman Gay jac...@hyperactivesw.com wrote: On 2/4/2015 3:01 PM, Graham Samuel wrote: So really, if I want an uncomplicated string of characters derived from a number via setting the numberFormat and then doing a calculation, how do I get it? You can turn it into a string by putting empty after it: put 1.5 into tVar -- number set the numberformat to 0.00 add 1 to tVar -- still a number put tVar into fld 1 -- numberformat applied now, field contains a string 2.55 add 1 to tVar -- still a number, contains 3.5 put empty after tVar -- numberformat applies, tVar is a string -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
Thanks Jacque, a gold mine of information as usual. But it’s pretty obscure, isn’t it? All this started for me because I wanted to test if two numbers were equal, knowing that they probably weren’t exactly equal to the last decimal place but nevertheless were ‘engineering equal’ as it were - say to six places of decimals. So I tried to truncate them to that length and then compare them. More fool me. But now I think I know what should be done, thanks to you - and to everyone else who replied. Graham On 4 Feb 2015, at 23:44, J. Landman Gay jac...@hyperactivesw.com wrote: On 2/4/2015 3:01 PM, Graham Samuel wrote: So really, if I want an uncomplicated string of characters derived from a number via setting the numberFormat and then doing a calculation, how do I get it? You can turn it into a string by putting empty after it: put 1.5 into tVar -- number set the numberformat to 0.00 add 1 to tVar -- still a number put tVar into fld 1 -- numberformat applied now, field contains a string 2.55 add 1 to tVar -- still a number, contains 3.5 put empty after tVar -- numberformat applies, tVar is a string -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
On 2/4/2015 3:01 PM, Graham Samuel wrote: So really, if I want an uncomplicated string of characters derived from a number via setting the numberFormat and then doing a calculation, how do I get it? You can turn it into a string by putting empty after it: put 1.5 into tVar -- number set the numberformat to 0.00 add 1 to tVar -- still a number put tVar into fld 1 -- numberformat applied now, field contains a string 2.55 add 1 to tVar -- still a number, contains 3.5 put empty after tVar -- numberformat applies, tVar is a string -- Jacqueline Landman Gay | jac...@hyperactivesw.com HyperActive Software | http://www.hyperactivesw.com ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Troubles with inequality
Using LC 7.0.2-rc-2, I have two numbers, each in its own variable ('temp' and 'aa' below) that may differ at say the eleventh decimal place but are otherwise identical. This bit of code: set the numberformat to 0.## -- to get 6 dec place precision just for the comparison put temp*1 into temp -- force a calculation to make a character string of the correct length put aa*1 into aa -- force a calculation to make a character string of the correct length if temp aa then answer error No! aa must be aa length= (number of chars of aa) , but it is actually temp length= (number of chars of temp) [...] always results in an error even though their length as strings is the same and the actual strings are by inspection identical, i.e. the numbers are being seen as unequal even though their representation as strings is undoubtedly equal. If I code if length (aa) length (temp) or (char 1 to length(aa) of temp) (char 1 to length(aa) of aa) Then I get the expected result and the error message is not shown. So it looks as if either I have made a massive mistake (always possible, to put it mildly) or the comparison is for the numeric values of the original quantities and not the string representations which I have tried to achieve. This goes against common sense - where are these more precise numbers being stored? Sadly I can't (yet) reduce this to a simple demonstration to report it as a bug, but I shall continue to try. Just wondered if anyone else had seen anything at all like this. Still trying, but puzzled. Graham ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Troubles with inequality
The numberformat only affect the appearance of the number, not its value. If you want to compare them based on the appearance characters, you can compare them as strings instead of numbers. Try this: on mouseUp put 1.234567 into temp put 1.2345678 into aa set the numberFormat to 0.### put temp * 1 into temp put aa * 1 into aa answer aa = temp answer word 1 of aa = word 1 of temp answer word 1 of aa end mouseUp ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode