Calculation errors

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Calculation errors

Post by Alistair » Wed Sep 24, 2008 1:17 am

I am running into calculation errors that seem to be coming from Revolution.

I am reading the settings of three scrollbars using statements of the form:

put the thumbposition of scrollbar "Efield" into Efield

When I display Efield the value is correct. If I move one or more of the scrollbars and then bring them back to their original positions, the values of the variables are always correct but the result of the calculation changes. How can this occur.

Also, if a set a scrollbar to range from 0 to 100 I find that it is not possible to set the scrollbar to either 50 or 150.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Sep 24, 2008 1:21 am

post your script
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Wed Sep 24, 2008 1:27 am

Here is the part of the script where the problem is occurring

I am using the second PUT statement to watch the values of the variables and the result of part of the calculation



put the thumbposition of scrollbar "mIz" into mIz
put the thumbposition of scrollbar "Efield" into Efield
put the thumbposition of scrollbar "Bfield" into Bfield
if bfield <> 0 then
put scaleFac*sqrt(2*mIz*Efield/(Bfield*Bfield)) into r1
put mIz, Efield, Bfield, 2*mIz*Efield/(Bfield*Bfield)
end if

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Sep 24, 2008 2:05 am

Hi Alistair,

I guess that the value of scaleFac changes. Is it posible that the startValue and endValue of the scrollbars change?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Wed Sep 24, 2008 2:24 am

Hi Mark

No the startValues and endValues and the scalefactor don't change. When I display the values of the variables they are correct. It seems to be the calculation itself that is producing a varying result.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Sep 24, 2008 2:36 am

Hi Alistair,

What are the values and what are the results?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Wed Sep 24, 2008 2:50 am

Hi Mark

As an example, if Miz is set to 100 and EField is set to 1, then repeatedly setting Bfield to 2 (setting the slider to another value, then setting it to 2) produces the following results: 2.636, 2.670, 2.705, 2.740. The correct value is 2.72.

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Wed Sep 24, 2008 8:15 am

Hi Alistair,

I also experienced some strangeness with scrollbars in the past, they were returning fractions althougg I did not set the numberformat of anything else.

The range was from -4 to 4.

So I ended with somthing like:

Code: Select all

...
put ROUND(the thumbpos of sb "whatever scrollbar") into sb_value
...
And my calculations were correct and happy ever after :-)

Hope that helps.


Best from germany

Klaus

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Wed Sep 24, 2008 8:45 am

Hi Klaus

Adding Round did solve the problem for integer values but the problem is that the range is meant to go from 0 and therefore fractional values such as 0.3 and intermediate values,say, between 1 and 2 are valid.

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Wed Sep 24, 2008 9:12 am

Hi Alistair,

you can define the number of fractions that ROUND should return with a second parameter to that function, see the docs for ROUND:

put round(100/3,1) -> 33.3
put round(100/3,2) -> 33.33
etc...


Best

Klaus

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Thu Sep 25, 2008 8:58 am

Hi Klaus,

Thanks for that suggestion. That fixed the problem. I should have thought to check the Dictionary when you first suggested using the Round function.

Thanks again

Alistair

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Sep 25, 2008 9:16 am

Hi Alistair,

One more idea: add the following brief script to your slider object and make sure to set the showValue of the slider to false or set the pageInc to 0, to hide the tics (you'll need to experiment with what works best for you).

Code: Select all

on mouseUp
   set the thumbPos of me to round(the thumbPos of me)
end mouseUp
If you use this script while the tics of the slider are visible, you will see that the tics are purely cosmetic and also confusing.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Fri Sep 26, 2008 5:11 am

Hi Mark

Thanks for that suggestion. I am using the slider to set a value so in this case displaying the value is important.

Regards

Alistair

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Sep 26, 2008 8:31 am

Alistair, just to make sure that I'm clear, you can display the value without displaying the tics.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Fri Sep 26, 2008 9:34 am

Yes, thanks, Mark. I'm displaying the value but not the tics.

Alistair

Post Reply