
adccommunitymod (AutomationDirect) asked a question.
Rounding to 0 or 5 HELP !!!!
Created Date: June 28,2010
Created By: eric14779
**** This post has been imported from our legacy forum. Information in this post may be outdated and links contained in the post may no longer work.****
I want to round a Real number to the nearest .0005 (half thousandths) example 125.2567 ----- > > > 125.2570 125.2502 ----- > > > 125.2505 this is for a position readout from an encoder I have a DL250-1 CPU Thanks in advance
Created Date: June 28,2010
Created by: bfitz
What's the range of the readout?
Created Date: June 28,2010
Created by: bcarlton
From your second example it looks like you are rounding UP to the next half thousandth instead of the NEAREST half thousandth. Is that correct?
If nearest the second example would have rounded to 125.2500
Created Date: June 29,2010
Created by: bcarlton
Start with your real in the accumulator - 2 examples
(123.4567) (123.4568)
MULR R10000 (1234567) (1234568)
ADDR R2.5 (1234569.5) (1234570.5)
DIVR R5.0 (246913.9) (246914.1)
RTOB (246913) (246914)
MULB K5 (1234565) (1234570)
BTOR (1234565.0) (1234570.0)
DIVR R10000 (123.4565) (123.4570)
Is this something like what you were looking for?
Created Date: June 29,2010
Created by: bcarlton
By the way - yes I realize that the steps can be shortened to:
MULR R2000
ADDR R.5
RTOB
BTOR
DIVR R2000
but I adapted it from a normal rounding function. That's the way I thought it through.
By the way - quiz time - what does the RTOB/BTOR pair do?
Created Date: June 29,2010
Created by: eric14779
Bcarlton,
Thanks for the replies...... This project is the first time I have had to do any math.... So I am a greenhorn in that regard to say the least....
I will impliment your code.... Thank You....
and I beileve that RTOB is Real to Binary conversion and BTOR is binary to Real.....
Eric
Created Date: June 29,2010
Created by: eric14779
Bcarlton,
I guess it would have been helpful if I mentioned that my Real number to round is negative as well as positive. I see that your code strips off the negative sign...
is there a way to maintain a negative number during the rounding ??
Thanks
eric
Created Date: June 29,2010
Created by: Do-more PE
Add a rung after you do Bernie's code that is:
STR B2001.15 *Original Vmemory location of the Floating Point (V2000)
OUT B3001.15 *Location where you stored the result of Bernie's code
What we are doing is checking bit 15 of the high word original floating point location to see if it is set. If it ism then it is negative and we set the same bit in the result of the calculation.
And Bernie beat me to it again. :)
Created Date: June 29,2010
Created by: bcarlton
STR B2001.1 *Original Vmemory location of the Floating Point (V2000)
OUT B3001.1 *Location where you stored the result of Bernie's code
Wouldn't that be B2001.15 ?
I didn't suggest this as I wasn't sure if there was some kind of two's complement thing for negative numbers. If figured the "0 - number " would do it correctly regardless.
Created Date: June 29,2010
Created by: eric14779
I was wondering about looking at the sign bit of the Vword....
I am LDR V1410 doing the rounding and OUTD back to V1410
according to Appendix H page 5 the sign bit is at bit 31 So that would be at V1411.15 for me
SO....... can I add in the STR V1411.15 before the LDR V1410 and then an OUT B1411.15 to the end ??? using say C51 to hold the bit status...
Created Date: June 29,2010
Created by: eric14779
What I described seems to work.... The only thing is that the rounding operations screws up my scale by many decimal places...
Is it the MULR R 2000.... ???? why 2000 and not 1000 ??? and what does the ADDR R0.5 do ??