round off number to use as variable

Posted on
Sun Jan 08, 2012 1:29 am
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

round off number to use as variable

Can you tell me why when this script writes the rounded off number to variable test123_, it is not rounded off?

Code: Select all
writeVar("test123_", 98.897896875578)

on writeVar(indigoSensor, val)
   tell application "IndigoServer"
      set val to my (roundThis(val, 2))
      set value of variable indigoSensor to val
   end tell
end writeVar

on roundThis(n, numDecimals)
   set multiplier to 10 ^ numDecimals
   n * multiplier
   round result
   result / multiplier
end roundThis


If I round off the number with the roundThis script first then have Indigo write the value to a variable it still won't round it off to two decimal places.

Code: Select all
set val to 99.897896875578

set val to my roundThis(val, 2)

--return val ==> returns rounded off value correctly

writeVar("test123_", val)

on writeVar(indigoSensor, val)
   tell application "IndigoServer"
      set value of variable indigoSensor to val
   end tell
end writeVar


on roundThis(n, numDecimals)
   set multiplier to 10 ^ numDecimals
   n * multiplier
   round result
   result / multiplier
end roundThis

Posted on
Sun Jan 08, 2012 9:28 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: round off number to use as variable

I'm not sure. I don't understand your 2 examples -- why is writeVar defined twice, but roundThis isn't defined in the 2nd example?

Image

Posted on
Sun Jan 08, 2012 3:37 pm
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

Re: round off number to use as variable

Matt,

OK if you run this first script, the variable "test123_" val is written to the Indigo Variable List as 99.09999999999999. If you run the second script to see what the rounded off number is, it is rounded off to 99.1. Why is Indigo changing the rounded off 99.1 to 99.09999999999999?

Code: Select all
set val to 99.09792092097

tell application "IndigoServer"
   set val to my (roundThis(val, 2))
   set value of variable "test123_" to val
end tell

on roundThis(n, numDecimals)
   set multiplier to 10 ^ numDecimals
   n * multiplier
   round result
   result / multiplier
end roundThis


Code: Select all
roundThis(99.09792092097, 2)

on roundThis(n, numDecimals)
   set multiplier to 10 ^ numDecimals
   n * multiplier
   round result
   result / multiplier
end roundThis

Posted on
Sun Jan 08, 2012 3:46 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: round off number to use as variable

Indigo isn't changing the rounding. The problem is that floating point numbers can not represent 99.1 exactly. See this article for an explanation.

The solution is to format the number as a string with a specific precision. Off the top of my head I'm not sure how to do that in AppleScript, but in python it would be:

Code: Select all
>>> valueAsStr = "%0.1f" % (99.09792092097,)
>>> valueAsStr
'99.1'

Image

Posted on
Sun Jan 08, 2012 3:52 pm
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

Re: round off number to use as variable

Matt,

I change to this (added "as string") and it is now OK.

thanks.

Code: Select all
set val to my (roundThis(val, 2)) as string

Posted on
Tue Mar 27, 2012 2:45 pm
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: round off number to use as variable

Bob... can you post your entire script? I'm trying to get some visual feedback on airfoil speaker volumes. The come in anywhere between 0 and 1... I'm hoping to be able to round to the nearest .1.

Posted on
Tue Mar 27, 2012 3:28 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: round off number to use as variable

Dewster35 wrote:
Bob... can you post your entire script? I'm trying to get some visual feedback on airfoil speaker volumes. The come in anywhere between 0 and 1... I'm hoping to be able to round to the nearest .1.

Hi. I don't know what Bob's script is like, but it's possible to use AppleScript to round to any specific decimal place. For example:
Code: Select all
(round (0.853953 * 10) rounding as taught in school) / 10

which will return
Code: Select all
0.9

There are other rounding algorithms if you don't like the statistical aversion to zero inherent to the "rounding as taught in school" method. There's also "rounding up", "rounding down", "rounding to nearest", and "rounding toward zero". Don't forget that Indigo variables can only contain values of type "text" or "string", so if you have an Indigo variable that contains this example 0.853953 value, you'll first have to convert that text into a number.
Code: Select all
set anAppleScriptVariable to (value of variable "Your_Variable_Name") as number

That will ensure that the "anAppleScriptVariable" is a number rather than a text string. In my experience, it's not necessary to convert the number back to a string when assigning it to an Indigo variable as Indigo (or perhaps the OS's AppleScript interpreter) seems to do this automatically upon assigning a number to the Indigo variable.

Posted on
Sun Apr 01, 2012 9:48 pm
gtreece offline
Posts: 171
Joined: Sep 26, 2011

Re: round off number to use as variable

Dewster35 wrote:
Bob... can you post your entire script? I'm trying to get some visual feedback on airfoil speaker volumes. The come in anywhere between 0 and 1... I'm hoping to be able to round to the nearest .1.


I have an audio control page that includes volume up and down controls for Airfoil. I wanted increments of 10, from 0 to 100 for the volume levels, so I implemented rounding and a multiplier of 100 when keeping track of the current Airfoil volume level, so that I could always set it explicitly with any volume change:

Code: Select all
tell application "IndigoServer"
   set the value of variable "airfoilVolume" to (round ((zoneVolume + 0.1) * 100))
end tell


Based on the currently selected audio zone, I get the current speaker volume from Airfoil, increment or decrement it by .1 (or set it to 0 or 1 if I reach either end of the scale), and then round and store the number in Indigo as above. I display the value stored in Indigo on the control page.

Posted on
Mon Dec 29, 2014 2:24 am
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: round off number to use as variable

Hello All,
I am trying to round a variable "testVar" from 75.1 to 75 and when I attempt the following:
Code: Select all
tell application "IndigoServer"
   set the value of variable "testVar" to round (testVar)
end tell

I get "The variable testVar is not defined.
What's the blinding flash of the obvious that I am missing?

Thanks,
~Lou

Posted on
Mon Dec 29, 2014 4:19 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: round off number to use as variable

DU Lou wrote:
Hello All,
I am trying to round a variable "testVar" from 75.1 to 75 and when I attempt the following:
Code: Select all
tell application "IndigoServer"
   set the value of variable "testVar" to round (testVar)
end tell

I get "The variable testVar is not defined.
What's the blinding flash of the obvious that I am missing?

Thanks,
~Lou

"testVar" and testVar aren't the same thing (note the quotes) so the unquoted variable testVar isn't defined. But if you change round (testVar) to round ("testVar") you'll get an error that Applescript "can't make "testVar" into type real." The following code, however, will do the trick:

Code: Select all
tell application "IndigoServer"
   set a to value of variable "testVar"
   set the value of variable "testVar" to round (a)
end tell
Cheers,
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Dec 29, 2014 11:46 am
DU Lou offline
Posts: 279
Joined: Mar 08, 2012
Location: Florida

Re: round off number to use as variable

DaveL17 wrote:
DU Lou wrote:
Hello All,
I am trying to round a variable "testVar" from 75.1 to 75 and when I attempt the following:
Code: Select all
tell application "IndigoServer"
   set the value of variable "testVar" to round (testVar)
end tell

I get "The variable testVar is not defined.
What's the blinding flash of the obvious that I am missing?

Thanks,
~Lou

"testVar" and testVar aren't the same thing (note the quotes) so the unquoted variable testVar isn't defined. But if you change round (testVar) to round ("testVar") you'll get an error that Applescript "can't make "testVar" into type real." The following code, however, will do the trick:

Code: Select all
tell application "IndigoServer"
   set a to value of variable "testVar"
   set the value of variable "testVar" to round (a)
end tell
Cheers,
Dave


Dave,
That did the trick exactly! Thank you. I think I recall reading somewhere that it's better to use python since python uses IDs rather than names and a variable name could change while the ID will always stay the same. Could you or someone else provide the complimentary code is say the variable ID for testVar is "87654321"? As I learn a little here and there I want to be as tidy as possible :)

Thanks again,
~Lou

Posted on
Mon Dec 29, 2014 12:08 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: round off number to use as variable

Glad I can help. Indigo stores values as strings so we might need to do some converting. Here is the Python version of the same process, with some added things you might do (or not) as needed:

Code: Select all
a = indigo.variables[362195049].value # Get the value from the variable.
a = float(a) # Let's try to see if the value will float.
a = round(a) # Rounds the value.
a = int(a) # Converts the value to an integer.
a = str(a) # Converts the value back into a string.
indigo.variable.updateValue(362195049, a) # Write the updated value back to Indigo
It gets a little more complicated because Python becomes unhappy if it can't complete a step, so the code might bomb for something you're trying to do. It's also important to note that you won't need all of these steps for every conversion.

Post back if you have any questions.
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 8 guests