Division in python script returns 2 decimal places rounded

I know this is going to be a stupid question, but I can’t seem to find a solution.
I have an equation in a Python script, it’s very simple and looks like this:
myBatchPercent = 1.0 / numberOfBatches

where numberOfBatches is 13.
I get 0.08 as the result.
I write the value to a float8 tag and write it to a log, and it is 0.08.
It should be something more like 0.07692307692307692307692307692308.
I’d like to get up to 5 or so decimal places.
This value will be used to find a percentage of total order size for a single batch.
I need a bit more precision to the number.
I’ve tried using the decimal module but only got errors.

Am I missing something simple here? :frowning:

Thanks,
Mike

Can you show how you are looking at it. Sources, and what you mean by write it to a log.

By default from what I’ve seen, the Ignition Tag Browser window only shows 2 decimal places for floats. If you open up the tag (Click the +) to show the Value, AlarmActiveCount,…FormatString,… You will see that the default format string is “#,##0.##” This can be changed in the tag editor Metadata entry, maybe that will help you out.

Bill

2 Likes

Test your script in the Designer’s Script Console (Tools–>Script Console):

numberOfBatches = 13
myBatchPercent = 1.0 / numberOfBatches
print myBatchPercent

Interactive Interpreter will print the result you expect:

0.0769230769231
>>>

As BillS notes, check the format string of the tag you are writing to. To show five decimal places, you’ll want a format string something like “#,##0.00000” (# is for optional digits, zeros will always be displayed).

1, I write the result to a memory tag using the system.tag.write function. That tag value displays 0.08 in the designer tag list.
2. I create a logger log using the system.util.getLogger function. I then put a message in the log such as log.info('My percent is: ’ + str(myBatchPercent)). The resulting log entry (on the Gateway Status > Diagnostics > Logs page) contains an entry stating “My percent is: 0.08”.

I will try the tag format, but that should not affect the variable value that is being written to the log (as noted above), I wouldn’t think.

I will also try this in the console, but if it prints as shown there but continues showing as 0.08 in both the variable and the tag, what is the next step?

Thanks.

Can we get your exact code that’s resulting in 0.08? I suspect there’s some other rounding taking place elsewhere that you’re not expecting.

It’s just like I posted above:

myBatchPercent = 1.0 / numberOfBatches

numberOfBatches is an integer value.

If you replace PrecisionTest with the path to your tag, how does your script console output compare to the script console here?

1 Like

OK, I’m back able to do some testing today and this is what I’m seeing.
When I change the tag’s default format string to “#,##0.00000” I get this:

The logic that populates that tag is:

	#if this is the first of the three campaigns, call the calcNumBatches to get 
	#batchData[0] = Total Component Quantity for Order
	#batchData[1] = Number of batches
	#& calclate the component qty percentage multiplier
	if campNum == 1:
		batches = shared.Northwind_Interface.calcNumBatches(tagPartAssignedPO, maxBatch, K)
		orderQtyTot = batches[0]
		log.info("Component Qty Total: " + str(orderQtyTot))
		numBat = batches[1]
		result = system.tag.write(tagPartNWDehy + '/batchNumTot', numBat)
		#The batch percentage of order is used to calculate the amount of each component
		#per batch. The value on campNum 1 is 1/total batches for order
		#This %age can then be use to muptiply against the Req'd Qty for a componet (total order qty)
		#to get the batch quantity for that component.
		batchPercentOfOrder = 1.0 / numBat
		log.info('Batch % of Order calculates to ' + str(batchPercentOfOrder) + '%')

Where numBat = the total number of batches, in this case 13. This value is written to the batchNumTotal tag shown above.
The logic is producing the “percentage” (actually the ratio) value of 0.08000.

When I use the Script Console to print the tag value I get the 0.08 value.

I wrote a short script to first get the tag value of the ratio that was previously calculated by the Shared script.
It read back and printed as above (0.08)
This script then calculates the ratio using the batchesNumTotal tag value (13) by dividing that tag value into 1 (1.0 / batchesNumTotal). This produces a “valid” value of 0.0769230769231
I then do the same with a constant value of 13, producing the same 0.0769230769231 value. I write that value to the tag, batchPercentOfOrder.
I then read that value back and get 0.08.


It looks like the tag is getting updated with the “valid” value of 0.0769… but not quick enough for the followup read get the updated value. If I put a while loop in that simply adds one to a counter, the counter the while loop is examining, I can get the updated value when I set the while loop to run when the counter is less than 10,000,000.

So it looks like the tag is not the issue.
That leaves the Shared script, but the logic calculating the ratio value there is the same as the logic being used in the Script Console. It is simply 1.0 / number of batches.

Ideas?

BTW, here is the tag value as calculated by the Script Console script:

Never mind!!!

I’m an id10T!

I had inadvertently deleted the logic that calculate the number of batches.
When I rewrote it I forgot to call the math.ceil() method on the calculated value to get the next higher integer value.
With this corrected my tag is populating as expected.

Thanks for all the help and I apologize for my stupidity! :frowning: