Long Integer division needs remainder in RSLogix

SysIntegrator

Member
Join Date
Jul 2012
Location
Pittsburgh, PA
Posts
46
Hello,

I've got a tricky math problem. I have a double integer that I need to divide by 10,000, and separate it into quotient and remainder. I have successfully done this with a regular integer by setting the destination as the Math Word S:13, then S:14 gives me my remainder, but if you use a float value or a Long integer in the DIV function you will only get get a rounded value in S:13.

I can think of a few ways to do this, but most involve some really cumbersome math. Anybody have a solution that's clean and simple?

If you're wondering what its for, I have a commanded position value in counts for a drive motor (Long integer) that needs to be converted to revolutions and counts so I can write it to a drive.

And as usual, help is always appreciated!
 
Hello,

I've got a tricky math problem. I have a double integer that I need to divide by 10,000, and separate it into quotient and remainder. I have successfully done this with a regular integer by setting the destination as the Math Word S:13, then S:14 gives me my remainder, but if you use a float value or a Long integer in the DIV function you will only get get a rounded value in S:13.

I can think of a few ways to do this, but most involve some really cumbersome math. Anybody have a solution that's clean and simple?

If you're wondering what its for, I have a commanded position value in counts for a drive motor (Long integer) that needs to be converted to revolutions and counts so I can write it to a drive.

And as usual, help is always appreciated!

Can you not just use a regular DIV for the rounded quotient and a MOD for the remainder?
Then do a comparison, if remainder >= (divisor/2) then the quotient would have been rounded up, therefore you can just subtract one from quotient.

EDIT: Example:
Code:
DIV  L9:0 10000 N7:0
MOD L9:0 10000 N7:1
GEQ N7:1 5000 SUB N7:0 1 N7:2
LES N7:1 5000 MOV N7:0 N7:2
 
Last edited:
It sounds like he's using RSLogix 500, which has no modulus instruction.

If you processor supports it, you could do a double divide (DDV):
When rung conditions are true, this output instruction divides the contents of the math register (S:13 and S:14), containing 32 bits of data, by the source (16 bits of data) and stores the result in the destination and the math register. The value stored in the destination is rounded. The value stored in the math register consists of the unrounded quotient (placed in the most significant word) and the remainder (placed in the least significant word).
If you are using a 5/02, 5/03, 5/04, 5/05 or MicroLogix 1000 processor, you can use indexed addresses for the destination parameter. If you are using a 5/03 OS302, a 5/04 OS401, or a 5/05 processor, you can use indirect addresses for the source or destination parameters.
If a value greater than +32,767 is returned, a minor error flag is set, and the value 32,767 is placed in the destination. If the remainder is 0.5 or greater, the destination is rounded up.
 
I looked at using the DDV function, but surprisingly it is not available on the Micrologix 1400 B either.

The only way I can think of doing this is using the DIV function with the long integer to get a rounded value, then multiplying that rounded value by 10,000 again, then subtract this value by the long integer. Where you get into trouble doing this is signs, because you will have to do comparisons and additional rungs to tell if you've rounded up or down, and then you will also need to add even more rungs and comparisons in the event you have a negative or positive value.

Its probably not as bad as it sounds but it just seems like there must be a cleaner way to do it.
 
Or you could DIV to a float, then MOV that float to INT. If INT > Float, then subtract one from the INT. Then you could subtract the INT from the float to get a float remainder. MUL this float by 10000 to get an INT remainder.
Not exactly a "clean" solution either, but if you're wanting to do modulus math with a uLogix it's not gonna be pretty no matter how you do it. :eek:
 
You can compensate for rounding when moving a positive float. Instead of using a MOV instruction use a SUB instruction to subtract .5 and make the destination an integer. This gives you the unrounded result. Its simpler than the multistep process of computing an integer and float result and then compaing them.
 
Last edited:
You can compensate for rounding when moving a positive float. Instead of using a MOV instruction use a SUB instruction to subtract .5 and make the destination an integer. This gives you the unrounded result. Its simpler than the multistep process of computing an integer and float result and then compaing them.

Nice solution, works beautifully for any PLC that uses "normal" rounding methods. But would you not run into problems with the CLX family of PLC's which use the "Banker's Round" algorithm?

For example, in the OP's case say his Drive Counts = 830000
DIV Drive_Counts 10000 = 83.0 exactly.
If you were to SUB 83.0 0.5 Some_DINT, then Some_DINT would round down to 82.0

I guess it's a moot point, though, since CLX processors, and probably any PLC sophisticated enough to use a non-typical rounding method, are also going to have a MOD instruction...

Anyway, I will definitely use your method in the future.

Thanks,
Dustin
 
Last edited:

Similar Topics

I have Allen Bradley plcs, I have had Circuit breakers and other automation equipment in the past. There's no solid buyers local. How much do you...
Replies
2
Views
205
I have a question. I work in a very large plant and this one (of many hundreds of control cabinets) contains one 5580 (1756-L83E), two 1756-L73...
Replies
6
Views
209
Hi to everybody. I need to read the first 12 characters of the message that a barcode reader sends to the ascii card (1734-rs232 ascii) and I...
Replies
8
Views
729
Hello all. This is a very lonnnnnnng shot but worth a try. I have an OMS Group Impact100 metering machine. At this customer it blows foam into 3d...
Replies
0
Views
188
Anybody have a recent experience becoming a UL508A panel shop? If so, how long did it take from initial contact to being able to apply stickers...
Replies
3
Views
1,238
Back
Top Bottom