Math Question

I wish to save as a session variable the average of two saved session.getVariables for two different set of session.getVariables and then calculate the average of the two set of session.getVariables plus a third session.getVariable.

For example,
session.getVariable("A")
session.getVariable("B")
out.write the average of AB (AB is the average of A and B)

session.getVariable("C")
session.getVariable("D")
out.write the average of CD (CD is the average of C and D)

session.getVariable("E")
out.write the average of A B, C D, and E

Here's the weird part; AB, CD, and or E will not always be available. For example, AB will not be available so I need to calculate the average of CD and E or E will not be available so I need to calculate the average of AB and CD.

Thanks again!!!

Algorithms and Java's picky casting

Adrianne,

I see two things you'll want to get acquainted with in order to crack this nut. The first is learning the different options you have when using if-then statements. The following two links are pretty advanced uses of conditional statements and should give you an idea of what can be done.

http://community.screen-scraper.com/script_repository/Parse_Full_Name
http://community.screen-scraper.com/script_repository/Fix_Phone

The next little nugget you're going to want to learn when performing calculations in screen-scraper when using Interpretive Java is how to properly cast your variables so Java doesn't think you're trying to add an orange named "3" with an actual number 2. This is going to seem a little convoluted and excessive but I'm sure the folks at Sun had a darn good reason to make the casting of their data types so rigid.

This is an example of how to take two screen-scraper variables from different sources, bring them together and have them produce a unique variable all their own.


// Add two variables

// Set them as local variables
numA = session.getVariable("numA");
numB = dataRecord.get("numB");

// Turn them into Integers
numA = Integer.parseInt(numA);
numB = Integer.parseInt(numB);

// Make them an int
numA = numA.intValue();
numA = numA.intValue();

// We still alive little buddies?
session.log("numA**" + numA + "**");
session.log("numB**" + numB + "**");

// Now they're ready to be treated and numbers
numC = (numA + numB);

// Make the product a screen-scraper variable
session.setVariable("numC",numC);

session.log("numC**" + session.getVariable("numC") + "**");

// Here it is consolidated down a bit.
// ** Remember a true Jedi warrior will only consolidate his code once
// he has proven that it works **

numA = Integer.parseInt(session.getVariable("numA")).intValue();
numB = Integer.parseInt(dataRecord.get("numB")).intValue();

session.log("numA**" + numA + "**");
session.log("numB**" + numB + "**");

numC = (numA + numB);
session.setVariable("numC",numC);

session.log("numC**" + session.getVariable("numC") + "**");

Getting closer...

Hi Scott,

I completed the first phase but have run into trouble when the session.getVariable is not available. For example, VALUE_A is not always available which causes an error. I’m also trying to assign a value of 0 if not found and a value of 1 for found so I can calculate the average of VALUE_A, VALUE_B, and VALUE_C. So if VALUE_A is not found (0) and VALUE_B is found (1) and VALUE_C is found I can add 0 + 1 + 1 to know that the total of VALUE_A, VALUE_B, and VALUE_C should be divided by 2 (0 + 1 + 1).

So far I have pieced together the following for VALUE_A portion of the equation:

//The variable “VALUE_A” is the result of attempting to match for the token “VALUE_A”
//Check to see if token “VALUE_A” is found
if (session.getVariable(“VALUE_A”) != null)
VALUE_A = 0
{
//Assign a value
NumA = session.getVariable(“VALUE_A”);
if (NumA == null)
NumA = 0
if (NumA > null)
NumA = 1
//Log Results
session.log(“VALUE_A was: “ + NumA);

// Set them as local variables
// Turn them into Integers
// Make them an int
VALUE_A = Integer.parseInt(session.getVariable("VALUE_A ")).intValue();
// We still alive little buddies?
session.log("VALUE_A **" + VALUE_A + "**");

Any suggestions or code is always appreciated.

Take it slow...

Adrianne,

I'm sure you've felt like you're taking very small bites in the process of eating this elephant; however, I would suggest that you take even smaller bites. I would suggest that you proceed to the next line of logic in your code when only when you have the current line working.

Using your script as an example...

Get the first line working for when you pass it an actual value or no value at all.

if (session.getVariable(“VALUE_A”) != null)
VALUE_A = 0
{

After it's working, move on to the next. You'll want to make judicious use of session.log to confirm your variables are doing what you expect.

Another helpful tool is session.breakpoint(). I recommend creating a script with nothing but a call to that method. Then, I recommend calling that script before running the current scrapeable file you're working on. Calling it before a scrapeable file runs help you control just when all the action happens that you're interested in monitoring.

And then a personal request. Please include the actual error message you receive and don't hesitate to enclose your code samples within.


// Here goes my code
if (mycode == cool)
me = cool;

By following my recommendation above if you get stuck on any one line, then it will make it much easier for myself and others to help troubleshoot.

Thanks,
Scott

Step 1

I'm receiving a "1" whether value VALUE_A is extracted or not. I have tried to replace the " " with null and BLANK but receive the same "1" value. The code is as follows:

if (session.getVariable("VALUE_A") == " " ) NumA = 0;
if (session.getVariable("VALUE_A") != " " ) NumA = 1;
//Log Results
session.log("The value is: " + NumA);

The logs are as follows:

Property Detail: Extracting data for pattern "Value_A"
Property Detail: The following data elements were found:
Value_A--DataRecord 0:
VALUE_A=$358,500
Storing this value in a session variable.
Processing script: "Write data to a file"
Writing data to a file.
UNFIXED: VALUE_A = $358,500
FIXED VALUE_A = 358500
The value is: 1

OR

Property Detail: Extracting data for pattern "Value_A"
Property Detail: The pattern did not find any matches.
Processing script: "Write data to a file"
Writing data to a file.
UNFIXED: VALUE_A =
FIXED VALUE_A =
The value is: 1

Thanks in advance for your help!!!

null vs. empty string

Adrian,

In your example, you're evaluating on a space.

" " = space
"" = empty string

I believe you're wanting to check if it's null or not. It's important to distinguish between an empty string and a null value. A null value means that the object does not exist, where an empty string means there is an object but that object contains no data.

So, here are the three conditions I'd recommend using in order to evaluate any of the above.


if (session.getVariable("VALUE_A") == "" ) NumA = "empty";
if (session.getVariable("VALUE_A") != "" ) NumA = "not empty";
if (session.getVariable("VALUE_A") == null) NumA = "is null";
if (session.getVariable("VALUE_A") != null) NumA = "is not null";
//Log Results
session.log("The value is: " + NumA);

-Scott

Stuck on Step 1...

I did try the "" before you suggested it but it did not work than.

I copied and pasted your suggested code and it produced the same "The value is: is not null" whether VALUE_A produced a value or not. What do you think?

== vs. .equals()

I thought I would include a few links, just because they pertain to the same issue.

It's a little involved to say why you would be interested in these links, but when the four conditions above don't work, you'll likely want to try .equals().

http://www.ccnyddm.com/JavaBook/EqualsEtc.htm
http://www.javapractices.com/topic/TopicAction.do?Id=17

-Scott

"is not null"

Adrian,

That sounds like it's working. Session variables are objects. Once you set a session variable in a scraping session, even if that session variable has no value or is an empty string, the object still exists until you explicitly call session.setVariable("theNameOfYourVariable",null).

So, if you are passing your script a session variable, whether or not that session variable contains any data, when your script says, "is not null", it is correct.

Let me change up the messaging in my example to hopefully be more clear.


session.log("The value of the 'VALUE_A' session variable is surrounded on both sides by two stars");
session.log( "**" + session.getVariable("VALUE_A") + "**");

// First perform evaluation on session variable, "VALUE_A"
// to verify whether the object exists

if (session.getVariable("VALUE_A") == null) resultsOfEvaluationOnObject = "object does not exist or is null";
if (session.getVariable("VALUE_A") != null) resultsOfEvaluationOnObject = "object does exist";

if (session.getVariable("VALUE_A") == "" ) resultsOfEvaluationOnObjectValue = "value is an empty string";
if (session.getVariable("VALUE_A") != "" ) resultsOfEvaluationOnObjectValue = "value is not an empty string";

//Log Results
session.log(">>> Results of evaluation on object: " + resultsOfEvaluationOnObject);
session.log(">> Results of evaluation on value of object: " + resultsOfEvaluationOnObjectValue);

When replying, please be sure to provide examples (session logs, your code) of what you experience in addition to the summary of your experience. And, please, when including logs or code, surround your logs or code with opening and closing HTML <code> tags in your message so the formatting is preserved.

-Scott

Trying to find a new direction...

I now understand that once you set a session variable in a scraping session, even if that session variable has no value or is an empty string, the object still exists but I think I might be going in the wrong direction. I see ther error in my thinking as I'm not so much interested in whether the variable exists as much as whether the variable is greater that 0, so that I can assign a value for calculating the average. In my original post I said AB, CD, and or E will not always be available. For example, AB will not be available so I need to calculate the average of CD and E or E will not be available so I need to calculate the average of AB and CD. I figured I could assign a 0 if session variable has no value or is an empty string and a 1 if session variable has a value.

I understand that he relational operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent (!=). Equivalence and nonequivalence work with all primitives, but the other comparisons won’t work with type boolean. Because boolean values can only be true or false, “greater than” and “less than” doesn’t make sense.

Is there a direction you could point me in to resolve my delema? Thanks!

Two possible approaches

If there are relatively few number of items you want to find the average of, try the following...

int totalValues = 0;
int totalItems = 0;

if (session.getVariable("A") != null && session.getVariable("A") != "")
{
session.log("A: " + session.getVariable("A") + "**");
totalValues = (totalValues + Integer.parseInt(session.getVariable("A")).intValue());
session.log("totalValues: " + totalValues);
totalItems++;
session.log("totalItems: " + totalItems);
}
if (session.getVariable("B") != null && session.getVariable("B") != "")
{
session.log("B: " + session.getVariable("B") + "**");
totalValues = (totalValues + Integer.parseInt(session.getVariable("B")).intValue());
session.log("totalValues: " + totalValues);
totalItems++;
session.log("totalItems: " + totalItems);
}
if (session.getVariable("C") != null && session.getVariable("C") != "")
{
session.log("C: " + session.getVariable("C") + "**");
totalValues = (totalValues + Integer.parseInt(session.getVariable("C")).intValue());
session.log("totalValues: " + totalValues);
totalItems++;
session.log("totalItems: " + totalItems);
}

session.log("Sum of all values for this dataRecord: " + totalValues);
session.log("Average of the sum of all values: " + (totalValues / totalItems));

If there are many items you want to find the average of, consider this approach.

session.setVariable Conflict

Please disregard my previous post because I tried to include the "Calculate the Average" script with the "Write data to a file" script which caused some type of conflict. When I created a separate script to calculate the average of VALUE_A and VALUE_B it worked fine. Thanks!!!

I'm having a problem with a session.setVariable. It appears there is a conflict when I apply the session.setVariable VALUE_CD_AVERAGE to the calculation average of VALUE_A, VALUE_B, and VALUE_CD_AVERAGE. I'm calculating the average VALUE_C and VALUE_D and saving it as a VALUE_CD_AVERAGE. This parts works fine, the code is as follows:


//Calculate the average of VALUE_CD
dataSet = new DataSet();

myDataRecord = new DataRecord();

if (session.getVariable("VALUE_C") != null && session.getVariable("VALUE_C") != "")
{
myDataRecord.put("VALUE_C",session.getVariable("VALUE_C"));
}
if (session.getVariable("VALUE_D") != null && session.getVariable("VALUE_D") != "")
{
myDataRecord.put("VALUE_D",session.getVariable("VALUE_D"));
}

dataSet.addDataRecord( myDataRecord );

session.log("how many fields in myDataRecord? " + myDataRecord.size());

int totalValues = 0;

for (int i=0; i {
dr = dataSet.getDataRecord(i);

enumeration = dr.keys();

while (enumeration.hasMoreElements())
{
key = enumeration.nextElement();
value = dr.get(key);

session.log("key:value **" + key + ":" + value + "**");
totalValues += Integer.parseInt(value).intValue();
}

session.log("Sum of all values for VALUE_CD: " + totalValues);
session.setVariable("VALUE_CD_AVERAGE",(totalValues / dr.size()));
session.log("VALUE_CD_AVERAGE**" + session.getVariable("VALUE_CD_AVERAGE") + "**");
}

Then I apply the VALUE_CD_AVERAGE to the average of VALUE_A and VALUE_B, the script is as follows:


//Calculate the average of VALUE_A, VALUE_B, VALUE_CD
dataSet = new DataSet();

myDataRecord = new DataRecord();

if (session.getVariable("VALUE_A") != null && session.getVariable("VALUE_A") != "")
{
myDataRecord.put("VALUE_A",session.getVariable("VALUE_A"));
}
if (session.getVariable("VALUE_B") != null && session.getVariable("VALUE_B") != "")
{
myDataRecord.put("VALUE_B",session.getVariable("VALUE_B"));
}
if (session.getVariable("VALUE_CD_AVERAGE") != null && session.getVariable("VALUE_CD_AVERAGE") != "")
{
myDataRecord.put("VALUE_CD_AVERAGE",session.getVariable("VALUE_CD_AVERAGE"));
}

dataSet.addDataRecord( myDataRecord );

session.log("how many fields in myDataRecord? " + myDataRecord.size());

int totalValues = 0;

for (int i=0; i {
dr = dataSet.getDataRecord(i);

enumeration = dr.keys();

while (enumeration.hasMoreElements())
{
key = enumeration.nextElement();
value = dr.get(key);

session.log("key:value **" + key + ":" + value + "**");
totalValues += Integer.parseInt(value).intValue();
}

session.log("Sum of all values for this dataRecord: " + totalValues);
session.log("Average of the sum of all values: " + (totalValues / dr.size()));
}

It appears the session.getVariable("VALUE_CD_AVERAGE") conflicts with "Static method parseInt( java.lang.Integer )" of the script because I receive the following error in the log:

The error message was: Error in method invocation: Static method parseInt( java.lang.Integer ) not found in class'java.lang.Integer' : at Line: 69.

Line 69 is:

totalValues += Integer.parseInt(value).intValue();

What do you think the conflict is? If there is additional information you need please let me know. Thanks!!!

A bit more of the log, please

Adrian,

The issue is unrelated to session.setVariable. The problem is occurring on line 69, as you point out.

On line 68 you're writing to the log the values being used in the equation on line 69. Could you post those? Please be sure to include enough data to understand their meaning in the larger context of the script.

Try this on a hunch. Change the following line from your first script.

session.setVariable("VALUE_CD_AVERAGE",(totalValues / dr.size()));

to

session.setVariable("VALUE_CD_AVERAGE",Double(totalValues / dr.size()).intValue());

If this works the reason would be that screen-scraper retains the datatype of the variable at the time you assign it to a session variable. When you're creating the VALUE_CD_AVERAGE session variable in your first script, I'm guessing it's of type Double. My suggestion will convert it from a Double to an int.

I didn't test this, so my apologies if it blows up on you.

-Scott

Command not found: Double

The log is:

how many fields in myDataRecord? 3
key:value **VALUE_A:279319**
key:value **VALUE_B:628568**

Notice that it recognizes that there is three data records but does display the value of VALUE_CD_AVERAGE.

When I run it without VALUE_CD_AVERAGE it will produce the following results:

how many fields in myDataRecord? 2
key:value **VALUE_A:279319**
key:value **VALUE_B:628568**
Sum of all values for A and B: 907887
VALUE_A and VALUE_B average**453,944**

I tried your suggestion of:


session.setVariable("VALUE_CD_AVERAGE",Double(totalValues / dr.size()).intValue());

which produced the following results:

how many fields in myDataRecord? 2
key:value **VALUE_C:563256**
key:value **VALUE_D:479810**
Sum of all values for VALUE_CD: 1043066
An error occurred while processing the script: Average
The error message was: Command not found: Double : at Line: 49.
530 Authentication required
Processing script: "Breakpoint"

Line 49 is this portion of the script:


for (int i=0; i

Please note the log included "530 Authentication required" which I have not seen before.

I hope I included enough information.

Double.valueOf

Adrian.

Sorry, you need to put ".valueOf" after "Double".

session.setVariable("VALUE_CD_AVERAGE",Double.valueOf(totalValues / dr.size()).intValue());

I'm not sure why you're getting the 530 error. Chances are it's unrelated to the current issue. Sometimes you may see a 530 response if the server thinks you're screen-scraping and eventually will block your IP outright.

-Scott

Magic Tricks...

I tried your suggestion of:


session.setVariable("VALUE_CD_AVERAGE",Double.valueOf(totalValues / dr.size()).intValue());

and now it does recognize "VALUE_CD_AVERAGE" for the average of VALUE_A, VALUE_B, and VALUE_CD_AVERAGE but it does not recognize VALUE_A. The log is as follows:

how many fields in myDataRecord? 3
key:value **VALUE_B:628568**
key:value **VALUE_CD_AVERAGE:521533**

Notice that it recognizes that there is three data records but does display the value of VALUE_A_AVERAGE.

When I run it without VALUE_CD_AVERAGE it will produce the following results:

how many fields in myDataRecord? 2
key:value **VALUE_A:279319**
key:value **VALUE_B:628568**
Sum of all values for A and B: 907887
VALUE_A and VALUE_B average**453,944**

I hope I included enough information.

When the looping is over...

Adrian,

I see a flaw in my example script that I believe is the cause of this latest issue. I should have put the logging of the sum and average of the values outside of the for loop. You were correct to assign your VALUE_CD_AVERAGE session variable where I was logging those values, it's just that I should have been logging after the loop and not within the loop.

Please see the revised script here and make the related changes to your script, as well.

Sorry about that.

-Scott

Isolating the error

Hi Scott,

In order to isolate the error I wrote the value of VALUE_A, VALUE_B and VALUE_CD_AVERAGE to the log the line above where the error occurs in the script. I received the following error in the log.

An error occurred while processing the script: Average
The error message was: BeanShell script error: Sourced file: unknown error: argument type mismatch : at Line: 34 : in file: : session .log ( session .getVariable ( "VALUE_CD_AVERAGE" ) ) BSF info: null at line: 0 column: columnNo

Thanks!!!

Adrian, Alone, this doesn't

Adrian,

Alone, this doesn't tell me much. Could you include the entire log? To ensure the formating is retained, please use the tags like we've talked about.

-Scott

Sorry...

Hi Scott,

I originally posted the reply below but it did not show-up in the forum. I had to enclosed it with the code markers to save it to the forum:

In order to isolate the error I wrote the value of VALUE_A, VALUE_B and VALUE_CD_AVERAGE to the log the line above where the error occurs in the script. I received the following error in the log.


An error occurred while processing the script: Average
The error message was: BeanShell script error: Sourced file: unknown error: argument type mismatch : at Line: 34 : in file: : session .log ( session .getVariable ( "VALUE_CD_AVERAGE" ) ) BSF info: null at line: 0 column: columnNo

Thanks!!!

Additional Information

Hi Scott,

For some reason, I'm not getting the BeanShell error but I'm still getting the same "Error in method invocation: Static method parseInt( java.lang.Integer ) not found in class'java.lang.Integer'".

In order to isolate the problem I created separate scripts for removal of non-numerical elements for numerical values, the average of Value_C and the Value_D and the average of Value_A, Value_B, and Value_CD_Average.

The log to remove non-numerical elements for numerical values is as follows:

Processing script: "Remove Non-Numerical Elements"
UNFIXED: Value_A = 416000
FIXED Value_A = 416000
UNFIXED: Value_C = 434,148
FIXED Value_C = 434148
UNFIXED: Value_D = 509,652
FIXED Value_D = 509652
UNFIXED: Value_B = $235,640
FIXED Value_B = 235640

The log to average Value_C and the Value_D is as follows:

Processing script: "Average CD"
how many fields in myDataRecord? 2
key:value ** Value_D:509652**
key:value ** Value_C:434148**
Sum of all values for CD: 943800
Value_CD_Average**471900**

As you suggested I logged the value of Value_A, Value_B and Value_CD_Average above the line where the error occurs in the script. I logged the value Value_A, Value_B and Value_CD_Average several times in the script and every time the values appeared to be correct. I also tried to log the value of Value_A, Value_B and Value_CD_Average after the error but it did not produce a value. The log to average of Value_A, Value_B, and Value_CD_Average is as follows:

Processing script: "Average"
Value_A: 416000
Value_CD_Average: 471900
Value_B: 235640
Value_A: 416000
Value_CD_Average: 471900
Value_B: 235640
how many fields in myDataRecord? 3
Value_A: 416000
Value_CD_Average: 471900
Value_B: 235640
key:value ** Value_B:235640**
key:value ** Value_CD_Average:471900**
An error occurred while processing the script: Average
The error message was: Error in method invocation: Static method parseInt( java.lang.Integer ) not found in class'java.lang.Integer' : at Line: 56.

Line 56 is:


totalValues += Integer.parseInt(value).intValue();

Please note that it did not produce the “key.value” for Value_A.

I hope I provided enough information.

Confused by your logging

Adrian,

Sorry for the delay this time. The sequence in which your data gets logged is confusing. I'm including a sample scrape I've set up below and the log that went with it.

Note my use of the "removeNonNumerics" function that is within the same script. I don't recommend putting that functionality in a different script.

Script:

// Function for removing anything non-numeric
removeNonNumerics(value)
{
if (value != null)
{
value = value.replaceAll("\\D", "");
value = value.trim();
}
return ( (value==null || value.equals( " " ))? "" : value );
}

// Create your own dataSet to store the incoming session variables.
dataSet = new DataSet();

myDataRecord = new DataRecord();

// Add the session variables to your new dataSet
if (session.getVariable("Value_CD_Average") != null && session.getVariable("Value_CD_Average") != "")
{
myDataRecord.put("Value_CD_Average",session.getVariable("Value_CD_Average"));
}
if (session.getVariable("Value_XY_Average") != null && session.getVariable("Value_XY_Average") != "")
{
myDataRecord.put("Value_XY_Average",session.getVariable("Value_XY_Average"));
}

// Add the DataRecord you just created to the current dataSet
// (if one already exists)
dataSet.addDataRecord( myDataRecord );

session.log("how many fields in myDataRecord? " + myDataRecord.size());

session.log("how many DataRecords in dataSet? " + dataSet.getNumDataRecords());

// Set to 0 prior to starting loop
int totalValues = 0;

// Loop through all rows in your dataSet
for (int i=0; i {
// Set the current row to a local variable
dr = dataSet.getDataRecord(i);

// Create an enumeration from the values in the current DataRecord
enumeration = dr.keys();

// Loop through enumeration
while (enumeration.hasMoreElements())
{
// Assign the key to a local var
key = enumeration.nextElement();
// Assign the value to a local var
value = dr.get(key);
session.log("value1**" + value + "**");
// Remove any non-numerics
value = removeNonNumerics(value);
session.log("value2**" + value + "**");

session.log("key:value **" + key + ":" + value + "**");
// Add the current value to the previous total
totalValues += Integer.parseInt(value).intValue();
}
}
session.log("Sum of all values for this dataRecord: " + totalValues);
session.log("Average of the sum of all values: " + (totalValues / dr.size()));

Log:

temp: Processing scripts before all pattern applications.
temp: Extracting data for pattern "Averages"
temp: The following data elements were found:
Averages--DataRecord 0:
Value_CD_Average=727,276
Storing this value in a session variable.
Value_XY_Average=619,531
Storing this value in a session variable.
temp: Processing scripts after a pattern application.
temp: Processing scripts after all pattern applications.
Processing script: "temp9"
how many fields in myDataRecord? 2
how many DataRecords in dataSet? 1
value1**727,276**
value2**727276**
key:value **Value_CD_Average:727276**
value1**619,531**
value2**619531**
key:value **Value_XY_Average:619531**
Sum of all values for this dataRecord: 1346807
Average of the sum of all values: 673403
Processing scripts after scraping session has ended.
Scraping session "temp" finished.

Any suggestions?...

Of the two approaches you offered I'm currently using the latter, but I received the same error on the first approach. When I receive no value for VALUE_A I received the following in the log:

how many fields in myDataRecord? 3
key:value **VALUE_B:401655**
key:value **VALUE_C:308412**
key:value **VALUE_A:**
An error occurred while writing the data to a file: For input string: ""

I don't know if this helps but I received the same error message when I used the following code:


VALUE_A = Integer.parseInt(session.getVariable("VALUE_A")).intValue();

One more problem; the value for VALUE_C was 00 which created an incorrect average. The log is as follows:

UNFIXED: VALUE_A = null
UNFIXED: VALUE_B = $316,012
FIXED VALUE_B = 316012
UNFIXED: VALUE_C = $00
FIXED VALUE_C = 00
how many fields in myDataRecord? 2
key:value **VALUE_B:316012**
key:value **VALUE_C:00**
Sum of all values for this dataRecord: 316012
Average of the sum of all values: 158006

Thanks again!!!

just on a little tangent. Be

just on a little tangent. Be careful when averaging integers. I got bitten by this recently. Integer division is just that, it returns an integer even if the result is a fraction.


int a = 1
int b = 2;
session.log("avg of a and b: " + ((a+b)/2));

this will return :
avg of a and b: 1

If the two division terms are integers it will return an integer by rounding of the result. To get a float output at least one of the division terms must be a float. i.e.:


float a = 1;
int b = 2;
session.log("avg of a and b: " + ((a+b)/2));

returns:
avg of a and b: 1.5

a neat way around this if you really want a and b to be int is to make sure the divisor is a float. Just add a decimal point:


int a = 1;
int b = 2;
session.log("avg of a and b: " + ((a+b)/2.0));

returns:
avg of a and b: 1.5

Thanks for the tip!

shadders,

Thanks for the heads up and examples to go demonstrate.

-Scott