You cannot trust arithmetic anymore

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: You cannot trust arithmetic anymore

Post by jacque » Wed Dec 30, 2020 7:17 pm

I happened to run into a similar problem with the mod function. If my bug report feels like a good fit you could add your comments and example here:

https://quality.livecode.com/show_bug.cgi?id=23042

So far LC thinks the problem is related to scrollbars but I think it's bigger than that, and your issue doesn't involve a scrollbar.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: You cannot trust arithmetic anymore

Post by mwieder » Wed Dec 30, 2020 9:00 pm

Craig-

I don't think this is the mod function at work here:

Look at the difference between

Code: Select all

put (11.025 * 28) / 11.025
vs

Code: Select all

set the numberformat to "#.######"; put (11.025 * 28) / 11.025

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10335
Joined: Wed May 06, 2009 2:28 pm

Re: You cannot trust arithmetic anymore

Post by dunbarx » Wed Dec 30, 2020 11:04 pm

@Mark.

It was Jacque who mentioned "mod".

I get "28" with each of your two tests. I was not surprised; I would not have thought the numberFormat would have any effect.

Anyway, there is no issue if you just use "*" and "/".

The problem arises when you do this:

Code: Select all

put trunc((11.025 * 28) / 11.025) into fld 1
   put (11.025 * 28) / 11.025 into line 2 of fld 1
The trunc function is not working as I think it should. You get 28 without it, and 27 with it.That is why I mentioned, essentially, that:

Code: Select all

trunc(28) <> 28
which seems odd to me. Of course, if you just try that last line alone somewhere, it works fine.

@ Jacque.

I will check it out. I bet there is a sinister connection. :wink:

Craig

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: You cannot trust arithmetic anymore

Post by mwieder » Thu Dec 31, 2020 1:22 am

I think you may be running into the limits of the binary math library, which is why there has been for years now a push to get a decimal math library involved.

Try

Code: Select all

   put trunc(27.99999999999999) after field 1 -- 27
   put trunc(27.999999999999999) after field 1 -- 28
Not quite sure why the decimal -> binary -> math -> decimal conversion isn't coming up exactly with 28, but my guess is that's whats going on under the hood. For fun to see this in action, try

Code: Select all

   set the numberformat to "#.##############"
   put (11.025 * 28.0) & cr into fld 1 -- <> 308.7

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: You cannot trust arithmetic anymore

Post by jacque » Thu Dec 31, 2020 3:19 am

If the trunc function is flawed then that actually could relate the mod function to the math problem you're having. Trunc should never return a decimal number.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10335
Joined: Wed May 06, 2009 2:28 pm

Re: You cannot trust arithmetic anymore

Post by dunbarx » Thu Dec 31, 2020 4:03 am

Jacque.

I don't see that Mark meant that. It was just that extra decimal 9 "pushed" the math library into thinking it was larger than it purported to be. It is what I called "fingers and toes" or what you meant when you said that integers were the only thing "div" should be used for.

But integers are only pixels on the screen to us. As I mentioned above:

Code: Select all

trunc((11.025 * 28) / 11.025) = 27
It only *seems* that "28 in" means" 28 out". No toes, you know. :wink:

Craig

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: You cannot trust arithmetic anymore

Post by mwieder » Thu Dec 31, 2020 4:43 am

Setting the numberformat to more decimal places gives you a more exact window into what the engine sees coming from the calculation. It seems that the result of the multiplication is *almost* 308.7, but not quite, and so dividing it again will give you *almost* 28 but not quite. It'll be 27 followed by a bunch of 9s. Truncating it still won't get you 28. Rounding it will, but that won't get you what you want if I'm understanding your "fitting into the area" algorithm.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10335
Joined: Wed May 06, 2009 2:28 pm

Re: You cannot trust arithmetic anymore

Post by dunbarx » Thu Dec 31, 2020 3:15 pm

It seems that the result of the multiplication is *almost* 308.7, but not quite, and so dividing it again will give you *almost* 28 but not quite. It'll be 27 followed by a bunch of 9s.
All true. Fingers and toes missing from the miracle of modern computing. Alas.

So, apart from the almost comical and rather embarrassing kludge of adding, say, "0.0001" to all of my values, I see no other solution to reliably using "div" for my needs. The comical part is that this method has to be remembered and added by hand every time I need it.

I am thinking wistfully of HC, which allowed one to intercept built-in commands and rewrite them. I cannot remember if operators worked similarly; I suspect not.

Anyway, I could simply train myself to use a new custom library function, "goodByeDiv":

Code: Select all

on mouseUp
   put (11.025 * 28) / 11.025 into tValue
   put goodByeDiv(tValue,11.025) into fld 1
end mouseUp

function goodByeDiv tValue,tDivisor
   return tValue + 0.0001 div tDivisor
end goodDiv
Thanks to all who indulged me in this. I wonder if there is a less kludgey solution.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10053
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: You cannot trust arithmetic anymore

Post by FourthWorld » Sat Jan 02, 2021 9:33 pm

dunbarx wrote:
Thu Dec 31, 2020 3:15 pm
I am thinking wistfully of HC, which allowed one to intercept built-in commands and rewrite them.
I went to mat with Dr Raney on that back in the day. He left me with a question: "Give me a business case where you'd want custom behavior without a custom name."

I never could come up with one.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: You cannot trust arithmetic anymore

Post by jacque » Sat Jan 02, 2021 11:04 pm

FourthWorld wrote:
Sat Jan 02, 2021 9:33 pm
dunbarx wrote:
Thu Dec 31, 2020 3:15 pm
I am thinking wistfully of HC, which allowed one to intercept built-in commands and rewrite them.
I went to mat with Dr Raney on that back in the day. He left me with a question: "Give me a business case where you'd want custom behavior without a custom name."

I never could come up with one.
I've only had one occasion and it was years and years ago. A company hired me to convert a HyperCard stack to MetaCard. The CEO's brother had written the original HC accounting software and was enthusiastic about adding sound effects to almost every user action (because he could, I guess,) which was driving the poor woman who had to actually use the software crazy. I was specifically asked to remove all sounds (and I sure didn't blame her.) They were scattered around everywhere, you could hardly move the mouse without making noise.

I would have given almost anything to just block the "play" command and be done with it. As it was, I had to track down all of them, and there were a gazillion of them -- because the author had copied the play command into every individual control across a dozen stacks. I have better ways of finding those things now, but back then it was incredibly tedious.

Fortunately that hasn't happened again. So yeah.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: You cannot trust arithmetic anymore

Post by mwieder » Mon Jan 04, 2021 10:07 pm

There's an item in bugzilla where Mark Waddingham talks about implementing decimal arithmetic soon.

It's been in there for ten years now. :roll:

https://quality.livecode.com/show_bug.cgi?id=9349

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10112
Joined: Fri Feb 19, 2010 10:17 am

Re: You cannot trust arithmetic anymore

Post by richmond62 » Tue Jan 05, 2021 7:42 am

It's been in there for ten years now.
Time to do some "love bombing" round these parts.

Everyone who wants decimal arithmetic should be posting a nag on the bugzilla report.
-
Screenshot 2021-01-05 at 9.01.52.png

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am

Re: You cannot trust arithmetic anymore

Post by sphere » Tue Jan 05, 2021 1:06 pm

There are a bunch more "implementations" or bugs who reside for years in the quality list and are not being solved or hibernated, maybe for the devs not important enough.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10112
Joined: Fri Feb 19, 2010 10:17 am

Re: You cannot trust arithmetic anymore

Post by richmond62 » Tue Jan 05, 2021 1:11 pm

maybe for the devs not important enough
Funnily enough I thought LiveCode would listen to their customers . . .

I have also attracted a lot of flack from time to time by suggesting that LiveCode might like to
retrench a bit and sort out quite a lot of bugs BEFORE going hell for leather for the next big thing.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10335
Joined: Wed May 06, 2009 2:28 pm

Re: You cannot trust arithmetic anymore

Post by dunbarx » Tue Jan 05, 2021 2:44 pm

Richard.
"Give me a business case where you'd want custom behavior without a custom name."
Well, one might say that having the "div" operator work as if decimal arithmetic was the norm was a case in point. The implication being that "custom behavior" in this context is what humans think of as "normal behavior".

Craig

Post Reply