These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

mixing FormCalc and Javascript calculation formulas

rpeterson
Registered: Oct 7 2008
Posts: 72
Answered

Hi all,
I have four fields: Avg, SD, RelSD, and Recovery. For simplicity sake, I have truncated the field names.

For Avg, the calculation is Avg(#1, #2, #3).
For SD, the calculation is sqrt((((#1 - Avg) * (#1 - Avg)) + ((#2 - Avg) * (#2 - Avg)) + ((#3 - Avg) * (#3 - Avg))) / 2).
For RelSD, the calculation is (SD / Avg)*100.
For Recovery the calculation is (SD / Assigned)*100.

The calculations for Avg and SD are in FormCalc. They work fine. But the calculations for RelSD and Recovery don't work at all. If I select FormCalc I get the Error: Arithmetic Over/Underflow, and if I select Javascript I get no error message, but I also get no results.

I am thinking that I either have not defined the fields properly for use in the last two or I haven't written them correctly or I need to tell the program to perform (i.e., hit the go button somehow). Am I on the right track or have I messed up entirely?

Can someone help me? I am very new to this and I am trying to create forms for technical laboratory use that will do the calculations needed. Any response and advice will be greatly appreciated.

rpeterson

rpeterson

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Whenever you divide, you should first check that you're not attempting to divide by zero, which is probably what's happening.

George
rpeterson
Registered: Oct 7 2008
Posts: 72
George or anyone else,
Ok, how to I fix this? Should I set the calculations' hierarchy? I have seen references to that but I am not sure how it is actually done. Help?

Thanks, rpeterson

rpeterson

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You might want to use "Sqrt()" for the square root function in FormCalc. This is not a documented function in LiveCycle Designer, but it follows the conversion of JavaScripts "Math" object's methods and there has been a post about these undocumented functions.

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You would just have to code it correctly in the field's calculate event. For example:

// JavaScript if (Avg != 0) {// Perform whatever calculation you want here, where Avg is the divisor } else {// Set field to blank (or whatever else you would want) here }

This is incomplete, but it should give you the general idea. Maybe someone else could help with FormCalc. Any calculation scripts should trigger whenever any other field value changes.

George
rpeterson
Registered: Oct 7 2008
Posts: 72
George,
Still no help. My fields are numeric if that makes any difference and those for Avg, RelSD and Recovery are all integers. Does that help to complete the picture? Am I really out in left field? If anyone wants, I can email them my file and they can take a look at it. I am at an impasse. I have printed out several reference docs, so I am not just relying on you all. Help?

rpeterson

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Have you opened Acrobat and the JavaScript debugger/console in Acrobat?

Are there any messages being displayed?

It might not be a good idea to name a field or variable with the same name as built in function.

You could also add some "messageBox()" statement to see what values are assigned to each variable and display the computational result so you will not be limited by the form's format.

George Kaiser

rpeterson
Registered: Oct 7 2008
Posts: 72
You have given me a lot to look into. I will get back to you probably tomorrow with what I came up with.

Thanks, rpeterson

p.s. by the way, what the devil is Arithmetic Over / Underflow anyway? It makes no sense to me whatsoever. If I knew what it meant I could address it. That is the only error message I am getting now. I tried the HasValue if statement and it works wonderful, except that I now get the over / underflow error. how do I get rid of it? I am going to try saving the document as a newer version and see if that helps. otherwise now that is my issue. Thanks to gkaiseril and george johnson for all the guidance.

rpeterson

rpeterson
Registered: Oct 7 2008
Posts: 72
Hey all,
I got it to work. Here's the resulting script:

Avg=Avg(#1, #2, #3)

SD=Sqrt(...

RelSD=if (HasValue(Avg)) then
SD / Avg * 100
else
"XX"
endif

Recovery=if (HasValue(Avg)) then
Avg / AssignedValue * 100
else
"XX"
endif

Thanks to all who helped me. Hope this turns out to be helpful to others in the future

rpeterson

rpeterson

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
> p.s. by the way, what the devil is Arithmetic Over / Underflow anyway?That what happens when you divide by zero. Dividing by zero in JavaScript is OK (doesn't generate an error), but apparently in FormCalc it does generate an error.

In JavaScript, when you divide by zero it results in the special numeric value of Infinity (for positive dividend), -Infinity (for a negative dividend), or NaN (not a number) if both dividend and divisor are zero (or non-numeric).

George
rpeterson
Registered: Oct 7 2008
Posts: 72
it also generates the error if we are asking for a division of zero. the HasValue for the Recovery was to deal with the zero in the numerator. i am so glad that i joined the users group. i hope i will be able to contribute more in the future.

rpeterson

rpeterson

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Just as a sanity check, when using JavaScript you have to explicitly reference the field value, i.e.,

if (Avg.rawValue != 0) {
(SD.rawValue / Avg.rawValue)*100;
} else {
// Set field to blank (or whatever else you would want) here

}

You were so Close;)

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script