Followers 0

# FX1S decimal help

## 4 posts in this topic

Hi There This is my first time using data registry and even going over the manuals is don't seem to be sinking. So here is my problem i have 3 variables (data input) and i want to count the calculated result. Lest start i have 2 variables i want to divided from each over 11.4 and 2.5 answer 4.56 I then have an encoder (variable 3 as this can always change on pules per revolution) 100 I then times the result with variable 3 to get 456. From this i have managed to get what i want but it seems a very long away around 70 steps!!! or so.. also i can only use an encoder that is divided by 100. From the attachment you can see that i got my result D16 just wondering is there another way of doing it? also i get lost when it start to go into 32 bit. your help would be apprishatded Regards Daniel

##### Share on other sites
I see you're jumping through some hoops to deal with integer division. There are some tricks to simplify the code a little. The main point is to try to do multiplication before division, as much as possible. You might have to switch to double math (32-bit). For the following example to work, you'll need the variables to be 32-bit. If they're coming from an HMI, just set them up that way. Otherwise you can just manually reset (RST works on registers...) the upper half. [DMUL D0 D2 D6] --- Multiplies 100 (encoder) times 1140 (variable 1) to get 114000 [DDIV D6 D4 D16] --- Divides 114000 by 250 (variable 2) to get 456 (result) Don't forget that the result of a DMUL or a DDIV actually controls four registers. Now, this works so cleanly because the numbers work out that way, so you still might need to do some rounding. For example if the encoder value is 112, the true answer is 510.72, but the code will return 510. However, this is pretty simple, because the DIV instruction actually returns two numbers. The quotient (510) shows up in D16 (32-bit) and the remainder (180) shows up in D18 (32-bit). So just add a rung to do a little rounding. ---[>= D18 K125]--------------------[DADD D16 K1 D16] --- Compares remainder with 125 (half of 250), and adds one (rounds up) if necessary
1 person likes this

##### Share on other sites
Thanks JRoss I will have a look at this and adjust as well as change the HMI settings. Regards Daniel

##### Share on other sites
JRoss Thanks again for helping me. I have implemented the coding and it has made it much clearer. I see what you mean about getting another decimal but considering i have already scaled up the decimal from 11.40 to 1140 getting and answer with a decimal if fine as i don't need that finer of accuracy just two decimal place is fine with in the answer it self plus using a encoder of 100 or more if close to the mark. Perhaps if i was using something with 20 ppr or less then i might add it in. Thanks again Regards Daniel