Sign in to follow this  
Followers 0
waterboy

effect of branching

7 posts in this topic

I have noticed in my CLX program (v20) that if I take the number 2.5 and MUL by .01 I get 0.02499999998 Why dont I get 0.025 ?

Share this post


Link to post
Share on other sites
in simplest terms: storing floating point numbers in "bits" leads to all sorts of issues like that ... I'm short on time right now, but someone else will come along soon and explain the nuts-and-bolts involved – but in the meantime here's a little experiment to try: MOV the number 123456789 from a DINT into a REAL ... notice the change in value ... now move the new number from the REAL into another DINT ... notice that the value didn't change this time ... now try to type the value 123456789 directly into a REAL location ... the point is that the "floating point" format is seldom as "precise" as most programmers like to think that it is ... in other words, there's often a certain amount of "round off" error involved when storing many specific "floating point" values ... this often leads to all sorts of wacky ideas when programmers convert their PLC's "integer" type values into a "floating point" format in a futile attempt to achieve "greater precision" for their calculations and control schemes ... Edited by Ron Beaufort

Share this post


Link to post
Share on other sites
It may be worthy to note this is not just an issue with PLC's but a fundamental issue with the IEEE 754 Floating Point standard. There is a short explanation here: Wikipedia Floating Point Details and workarounds are in this nice little write up: What Every Computer Scientist Should Know About Floating-Point Arithmetic. Edited by IO_Rack

Share this post


Link to post
Share on other sites
Here is a link to a handy tool. It helps illustrate for me (and solve some interesting problems with) floating point. But what you are seeing is similar to trying to express exactly using decimals the value 1/3. Without using the little 'repeat endlessly' bar you can't. You just get closer and closer. The decimal digits are just sub multiples of ten. There are many values they can't express exactly. In floating point all the bits are just sub multiples of two. It's the same concept, just problems in different areas. http://www.h-schmidt.net/FloatConverter/IEEE754.html

Share this post


Link to post
Share on other sites
OK then, I now understand the issue. Thanks for the links.

Share this post


Link to post
Share on other sites
Just so you know, it is the number .1 that is giving you the problem. Any number that is 1/10, 1/100th, 1/1000th, etc, or any multiple that is not equal to 1/2, cannot be represented exactly, eg .2, .3, .4. 6. , etc. However, 1/2, 1/4, 1/8, 1/16, etc. and any multiples of them can be represented exactly. This is because each bit in the mantissa represents 1/(2^n) where n is the bit position in the mantissa. Edited by TConnolly

Share this post


Link to post
Share on other sites
I need to edit the subject line. it was a mistake and not related to the question. sysop?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0