Jump to content

Recommended Posts

I am trying to move the decimal point of a counter value by dividing by 1000.  I think this is a binary issue because when the result reaches 32 it goes to -32 and decrements. This is  back and forth from negative to positive.  How do I work around this? I added a picture of my block to see.

divide.jpg

Link to comment
Share on other sites

An MI is a signed value from 32767 to -32768.  When you divide by 1000, I would expect to see what you are seeing due to word wrap to a negative value when the value gets larger than 32767 (accessing the sign bit/MSB).  As said by Flex727, you will probably need to utilize one of the double word formats ML, DW etc. to allow values large enough to not roll over the word.  You could also cascade counters by making one counter count to 1000 and counting the number  times it gets there with a second counter.

Link to comment
Share on other sites

You can also try converting the value from the counter to a Float before dividing by 1000 and placing the result in a float as below.  MI 3 can be substituted for your counter value.  MF1 will be = (MI3/1000)

float.png.227bab3729fa1b47b530971d1bdc219f.png

 

 

logic.png

Edited by bbird
wont' let me delete incorrect logic image
  • Upvote 1
Link to comment
Share on other sites

  • MVP 2023

Hi Visco and all,

I'm not completely sure what you're trying to do here, Visco, as your input being an MI is a bit self-regulating for possible amounts.  Doesn't my screenshot below do it easily?  But if you put 1000 into MI0, your result here will be 1, without any dec point.  So this raises my question of what exactly are you trying to do with the result?  Is it simply maths, displaying etc?

cheers,

Aus

Ausfloat.jpg

Link to comment
Share on other sites

On 5/6/2017 at 5:01 AM, Ausman said:

Hi Visco and all,

I'm not completely sure what you're trying to do here, Visco, as your input being an MI is a bit self-regulating for possible amounts.  Doesn't my screenshot below do it easily?  But if you put 1000 into MI0, your result here will be 1, without any dec point.  So this raises my question of what exactly are you trying to do with the result?  Is it simply maths, displaying etc?

cheers,

Aus

Ausfloat.jpg

I am trying to display 1000's of an inch. like 123.375"

Link to comment
Share on other sites

  • MVP 2023

Exactly, JohnR.

Visco, remember my comment about sometimes things can be simple, sometimes complex.  A major part of programming is pinning down exactly what you need to achieve, and then finding the easiest way to do it.  I often put all my known methods into the washing machine called the brain, and ages later I still end up with something not even considered in the first place!  It is a constant learning curve.

cheers,

Aus

Link to comment
Share on other sites

On 5/5/2017 at 4:43 PM, bbird said:

You can also try converting the value from the counter to a Float before dividing by 1000 and placing the result in a float as below.  MI 3 can be substituted for your counter value.  MF1 will be = (MI3/1000)

float.png.227bab3729fa1b47b530971d1bdc219f.png

 

 

logic.png

BBird, That worked. Thank you!! Much appreciated

Link to comment
Share on other sites

Thanks,

I think Ausman's version will do the same thing more efficiently with one less block, as the conversion is done automatically in the floating point divide block.  Lots of ways to accomplish the same result.  Glad it's working.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...