Hidden variables?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

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

Hidden variables?

Post by dunbarx » Thu Jun 22, 2017 1:01 am

Back in the beginners section. Again.

I have a handler that loads a two separate floating point numbers into two separate variables. The actual code snippet of interest is:

Code: Select all

put fixtureLength / boardLength into x --286.65 / 11.025
put fixtureLength div boardLength into y -- 286.65 div 11.025
FixtureLength = 286.65, and boardLength = 11.025. In case anyone is wondering, 11.025 * 26 = 286.65.

The division gives the answer 26 in x.
The div gives 25 in y.

May I assume there are tiny differences somewhere in the 17th decimal place that make these two different? I tore my hair out trying to find a bug in my code, until I just happened to try the division in place of the div, just for grins, and now all is well. Except my understanding of how computers actually work.

Craig Newman

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

Re: Hidden variables?

Post by dunbarx » Thu Jun 22, 2017 1:17 am

Another take on this:

Code: Select all

on mouseUp
   put 11.025 into x
   put 11.025 * 26 into y
   put "286.65" into z
   if y = z then
      put y / x into a
      put y div x into b
      put z / x into c
      put z div x into d
   end if
end mouseUp
The handler passes quietly through the conditional. I get:

a = 26
b = 26
c = 26
d = 25

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Hidden variables?

Post by FourthWorld » Thu Jun 22, 2017 1:27 am

"div" and "/" are different operators: "/" does straight division; "div" returns only the whole portion of the number, without any fractional part.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Hidden variables?

Post by SparkOut » Thu Jun 22, 2017 1:33 am

Reading the dictionary just shocked me, I have always habitually used / to divide rather than the keyword div but I had assumed they were synonyms. Apparently not
div
Type: operator
Syntax: number div divisor
Summary: Divides one number by another and returns the integer part of the result.
/
Type: operator
Syntax: dividend / divisor
Summary: Divides one number by another number or one array by another array.
Interestingly "divide" (container by...) results in a slight difference as well:

Code: Select all

   put 286.55 into fixtureLength
   put 11.025 into boardLength
   put fixtureLength into field 1
   put fixtureLength / boardLength into x  --286.65 / 11.025 yields 25.9909
   put fixtureLength div boardLength into y  --286.65 div 11.025 yields 25
   divide field 1 by boardLength  --field 1 yields 25.99093

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

Re: Hidden variables?

Post by dunbarx » Thu Jun 22, 2017 3:26 am

@Richard.

I know how div works; I am only speaking about whole numbers.

If a number goes into another evenly, I would have thought that "/" and "div" would be equivalent. They are not, and I tested both the divisor and dividend for equality.

@Sparkout.

Div and mod are "modulo" operators. You know, wholes and remainders. Division is a straightforward arithmetic operator. The each have very specific strengths.

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Hidden variables?

Post by bogs » Thu Jun 22, 2017 3:39 am

Back in the beginners section. Again.
I think I must be in the dunces corner, I never would have thought to even look at catching that :shock:
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Hidden variables?

Post by FourthWorld » Thu Jun 22, 2017 8:45 am

dunbarx wrote:@Richard.

I know how div works; I am only speaking about whole numbers.
I guess the fractional numbers in the original example threw me off.

If it's a big please report it.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Hidden variables?

Post by bogs » Thu Jun 22, 2017 4:56 pm

Sure looks like a "big"
ideBug.png
ideBug.png (15.58 KiB) Viewed 6080 times
to me, report report report !! :mrgreen:
Image

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

Re: Hidden variables?

Post by dunbarx » Thu Jun 22, 2017 5:25 pm

Reported #19938.

Code: Select all

on mouseUp
   put 11.025 into x
   put 286.65 into y
   
   put y / x into a
   put y div x into b
end mouseUp
Originally noted in v 6.7.9.
Verified in v. 8.1.4

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Hidden variables?

Post by FourthWorld » Thu Jun 22, 2017 6:34 pm

dunbarx wrote:Reported #19938.
When I attempt to find that report in LC's Bugzilla I get "Bug #19938 does not exist."

URL?

Code: Select all

on mouseUp
   put 11.025 into x
   put 286.65 into y
   
   put y / x into a
   put y div x into b
end mouseUp
Originally noted in v 6.7.9.
Verified in v. 8.1.4
Earlier you wrote: "I know how div works; I am only speaking about whole numbers."
That example uses fractional numbers, and compares results from two different operators. I'm not clear on the nature of the problem.

In which version of LC do you see the result you're expecting, and what was that result?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Hidden variables?

Post by bogs » Fri Jun 23, 2017 12:02 am

If I understand it correctly Richard, if you look at his original post, even though the numbers he is dividing are fractional, they should result in 26, a whole number with no fraction/remainder. Instead, one form gives him the expected answer of 26, the other 25.
FixtureLength = 286.65, and boardLength = 11.025. In case anyone is wondering, 11.025 * 26 = 286.65.
The division gives the answer 26 in x.
The div gives 25 in y.
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Hidden variables?

Post by FourthWorld » Fri Jun 23, 2017 12:21 am

Thanks. The original code wasn't presented in executable form so I was eyeballing it.

Seems he surmised the answer in the OP:
May I assume there are tiny differences somewhere in the 17th decimal place that make these two different?
My hunch is that this will result on one of those long posts from lead engineer Mark Waddingham about how math processors handle rounding at different levels of precision.

Once we get a working bug ID we'll be able to follow along.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Hidden variables?

Post by FourthWorld » Fri Jun 23, 2017 12:24 am

Found it:
http://quality.livecode.com/show_bug.cgi?id=19930

It did elicit a good explanation from Mark Waddingham, but he's also flagging it for "Expert Review" to consider options for possibly working around it.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Hidden variables?

Post by bogs » Fri Jun 23, 2017 2:01 am

My hunch is that this will result on one of those long posts from lead engineer Mark Waddingham about how math processors handle rounding at different levels of precision.
I absolutely LOVE long posts from engineers, thanks for posting the link for easier reference !

*Edit - His explanation certainly makes sense, but I agree with him that it is 'jarring' to see it represent itself. I'm not so sure about the 'how to round' statements, I always understood those to be a given, but then, I am not a mathematician by training or trade.
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Hidden variables?

Post by FourthWorld » Fri Jun 23, 2017 2:21 am

Division in scripting languages yields this sort of confusion now and then. Probably lower-level languages too. But I see this sort of discussion come up about every year or so, and in circles outside of LiveCode.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”