You cannot trust arithmetic anymore
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: You cannot trust arithmetic anymore
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.
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
HyperActive Software | http://www.hyperactivesw.com
Re: You cannot trust arithmetic anymore
Craig-
I don't think this is the mod function at work here:
Look at the difference between
vs
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
Code: Select all
set the numberformat to "#.######"; put (11.025 * 28) / 11.025
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: You cannot trust arithmetic anymore
@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:
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:
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.
Craig
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
Code: Select all
trunc(28) <> 28
@ Jacque.
I will check it out. I bet there is a sinister connection.

Craig
Re: You cannot trust arithmetic anymore
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
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
Try
Code: Select all
put trunc(27.99999999999999) after field 1 -- 27
put trunc(27.999999999999999) after field 1 -- 28
Code: Select all
set the numberformat to "#.##############"
put (11.025 * 28.0) & cr into fld 1 -- <> 308.7
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: You cannot trust arithmetic anymore
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
HyperActive Software | http://www.hyperactivesw.com
Re: You cannot trust arithmetic anymore
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:
It only *seems* that "28 in" means" 28 out". No toes, you know.
Craig
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

Craig
Re: You cannot trust arithmetic anymore
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: You cannot trust arithmetic anymore
All true. Fingers and toes missing from the miracle of modern computing. Alas.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.
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
Craig
-
- VIP Livecode Opensource Backer
- Posts: 10053
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: You cannot trust arithmetic anymore
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: You cannot trust arithmetic anymore
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.FourthWorld wrote: ↑Sat Jan 02, 2021 9:33 pmI 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 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
HyperActive Software | http://www.hyperactivesw.com
Re: You cannot trust arithmetic anymore
There's an item in bugzilla where Mark Waddingham talks about implementing decimal arithmetic soon.
It's been in there for ten years now.
https://quality.livecode.com/show_bug.cgi?id=9349
It's been in there for ten years now.

https://quality.livecode.com/show_bug.cgi?id=9349
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
-
- Livecode Opensource Backer
- Posts: 10112
- Joined: Fri Feb 19, 2010 10:17 am
Re: You cannot trust arithmetic anymore
Time to do some "love bombing" round these parts.It's been in there for ten years now.
Everyone who wants decimal arithmetic should be posting a nag on the bugzilla report.
-
Re: You cannot trust arithmetic anymore
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.
-
- Livecode Opensource Backer
- Posts: 10112
- Joined: Fri Feb 19, 2010 10:17 am
Re: You cannot trust arithmetic anymore
Funnily enough I thought LiveCode would listen to their customers . . .maybe for the devs not important enough
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.
Re: You cannot trust arithmetic anymore
Richard.
Craig
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"."Give me a business case where you'd want custom behavior without a custom name."
Craig