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

Calculations: So close, yet so far away.

dlsiegel
Registered: Dec 1 2009
Posts: 9
Answered

I am an intermediate user with very limited experience with scripts (I've used the "How to do (not so simple) form calculations" tutorial to learn how to get the calculations I need). I'm creating an extensive form in Acrobat 9.1.2 Pro with several calculations based on references from other cells also containing simple calculations.

I've got my custom calculation scripts correct, and they return correct values. The problem is that I have a select few calculated fields that return values with repeating decimals, when the correct value should not be such.

For example, a simple calculation ("TotalHabitatIndex"/"TotalAcres", where each is the sum of values) that should equal 0.14 is displayed as 0.1399999999999. The actual numbers being calculated are 1.4/10.

Another field with a subtraction calcuation of other calculated fields does the same thing. For example, when subtracting the values 1.1-0.19 it returns 0.9100000000000001.

What is throwing me off is that this is only occuring in one or two places, and only for certain values. In other words, the same field will correctly display 0.19 when the values being calculated are 1.9/10. The second field correctly displays 0.96 when the values being calculated are 1.15-0.19.

All fields are set to text format.

I would appreciate any ideas.

My Product Information:
Acrobat Pro 9.1.2, Windows
dlsiegel
Registered: Dec 1 2009
Posts: 9
Well, I think I was able to solve my own problem by trying a few different options I found on this forum.

I formated the calculated fields to Number with two decimal places. In the past, this was giving me errors because of the zero and null divisor fields (which may be empty until the user fills them), but I didn't realize this was what caused the problem.

I found a script in another post to correct that problem, and the Number format now seems to work fine without errors, and I no longer have my decimal issue.

The script I used for calculating FieldA/FieldB, where FieldB is blank prior to user interaction:

// clear field value
event.value = '';
// perform division only if divisor is not zero or null
if(this.getField("FieldB").value != 0)
event.value=this.getField("FieldA").value/this.getField("FieldB").value

I still don't understand why the calculations were doing what they did, though. That kind of annoys me, but at least I found a bandaid for the problem.
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
It's a general problem of division involving floating point variables in programming.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
What is the result of dividing by zero?

You are lucky your computer does not lock up!

The strange decimals you see are a result of how decimal number are converted to binary numbers. Whole numbers are fine, but decimals become irregular binary numbers.

George Kaiser

try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
Division by 0 is Infinity (and by -0 is -Infinity) ....

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

dlsiegel
Registered: Dec 1 2009
Posts: 9
I understand division by zero is undefined, but that was not what was initially causing the problem, since it would simply display NaN (Not a Number) when the offending fields were blank - and this was acceptable. However, the decimal problem led me to try switching the format to Number (with two decimal places), and that is when the division by zero became an issue.

Thanks for the insight about why the decimals went all wonky on me. I don't know what "floating point variables in programming" are, but I'm pretty sure I could find out!

Thanks, again!