<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Am I crazy? A-B [REAL] give wrong result?</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/52c022e8b6/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/52c022e8b6/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/52c022e8b6/</id><updated>2020-02-27T21:41:26.257000Z</updated><subtitle>Recent posts to Am I crazy? A-B [REAL] give wrong result?</subtitle><entry><title>Am I crazy? A-B [REAL] give wrong result?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/52c022e8b6/?limit=25#cdc9" rel="alternate"/><published>2020-02-27T21:41:26.257000Z</published><updated>2020-02-27T21:41:26.257000Z</updated><author><name>shooter</name><uri>https://forge.codesys.com/u/shooter/</uri></author><id>https://forge.codesys.comf613661862e22533bb62f5f3269b28732f73e22d</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;REAL is not exact, so you can see a small deviation.&lt;br/&gt;
the same can be done on a simple calculator, just add 1 and after 1 million times the result will not be exact.&lt;br/&gt;
It is indeed to do with the way the mantisse works, as it is not endless resolution.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Am I crazy? A-B [REAL] give wrong result?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/52c022e8b6/?limit=25#b081" rel="alternate"/><published>2020-02-27T21:41:22.081000Z</published><updated>2020-02-27T21:41:22.081000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com47a0b8ac0b64508e711615ee939199efbf0a76d0</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;You are hitting accuracy errors.  Keep in mind that 0.15 is a repeating number in base 2, so depending on internal implementation, you will have an error when you define the value because of truncation.  Also, I am pretty sure you will see more accuracy problems with 50-50.15 (negative, fractional number) than with 50 + 50.15.  This page (&lt;a class="" href="http://ccm.net/contents/62-representation-of-real-numbers-and-integers" rel="nofollow"&gt;http://ccm.net/contents/62-representati ... d-integers&lt;/a&gt;) gives an explanation of how real numbers are stored as a 32 bit number.&lt;/p&gt;
&lt;p&gt;This calculator (&lt;a class="" href="https://baseconvert.com/" rel="nofollow"&gt;https://baseconvert.com/&lt;/a&gt;) shows:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1500000&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0010&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1500015&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0010&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0111&lt;/span&gt; &lt;span class="mi"&gt;1111&lt;/span&gt; &lt;span class="mi"&gt;1001&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;1101&lt;/span&gt; &lt;span class="mi"&gt;1001&lt;/span&gt; &lt;span class="mi"&gt;1101&lt;/span&gt; &lt;span class="mi"&gt;0111&lt;/span&gt; &lt;span class="mi"&gt;0111&lt;/span&gt; &lt;span class="mi"&gt;0111&lt;/span&gt; &lt;span class="mi"&gt;0100&lt;/span&gt; &lt;span class="mi"&gt;1010&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Bit deviation starts at bit 20 (23 bits would be absolute max performance, based on 23 bit mantissa)&lt;/p&gt;
&lt;p&gt;Now, if we look at your 50-50.15:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="mi"&gt;0010&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="mi"&gt;0010&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0010&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;50.00 will be:    1.10010000000000000000000 e^(127+5) s=0, m=10010000000000000000000, e=10000100&lt;br/&gt;
50.15 will be:    1.10010001001100110011001 e^(127+5) s=0, m=10010001001100110011001, e=10000100&lt;/p&gt;
&lt;p&gt;Ultimately, you are subtracting a truncated repeating number, so error will appear sooner.&lt;/p&gt;
&lt;p&gt;I created a project with CoDeSys 3.5.6.66 that has an error already with 50.15 (as expected) - see the screen shot.  I also used 50.5 instead, and received a "perfect" answer (because 0.5 is 0.1 in base 2 notation).  See screen shots.&lt;/p&gt;
&lt;p&gt;Are you sure your CoDeSys 3 test was still using 32 bit REALs?  With LREAL, you will see -0.15 in the online debug mode, but not in the variable header - you will see truncation error of infinite repeating 0.15 in base 2:&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: errors.png" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3017"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: clean.png" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3018"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: lreal.png" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3019"/&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Am I crazy? A-B [REAL] give wrong result?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/52c022e8b6/?limit=25#6fd1" rel="alternate"/><published>2020-02-27T21:41:20.613000Z</published><updated>2020-02-27T21:41:20.613000Z</updated><author><name>hki75</name><uri>https://forge.codesys.com/u/hki75/</uri></author><id>https://forge.codesys.combb7642c330b7d3848600be331e2ebe33bdbb6339</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;mmm....I don't think is the REAL data type limit...by doing the same calculation on codesys 3 is ok. Or by trying to adding (50+50.15=100.15 , not 100.1500015..)the result is correct. I think is something related to the fraction and exponent management ...&lt;br/&gt;
No one had the same trouble?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Am I crazy? A-B [REAL] give wrong result?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/52c022e8b6/?limit=25#2eda" rel="alternate"/><published>2020-02-27T21:41:16.472000Z</published><updated>2020-02-27T21:41:16.472000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com8a333bc3757be226999f6722bc9bc381f1e75937</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;Accuracy limits of REAL type.  If you need higher accuracy, then you need to use LREAL, if it is supported on your platform.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Am I crazy? A-B [REAL] give wrong result?</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/52c022e8b6/?limit=25#6951" rel="alternate"/><published>2020-02-27T21:41:14.955000Z</published><updated>2020-02-27T21:41:14.955000Z</updated><author><name>hki75</name><uri>https://forge.codesys.com/u/hki75/</uri></author><id>https://forge.codesys.com6dd46fc42b826e7f84fcac23e108190ace29594b</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;a simple code:&lt;br/&gt;
PROGRAM PLC_PRG&lt;br/&gt;
VAR&lt;br/&gt;
A: REAL:=50.0;&lt;br/&gt;
B: REAL:=50.15;&lt;br/&gt;
C:REAL;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;C:=A-B;&lt;/p&gt;
&lt;p&gt;C value is -0.15000&lt;strong&gt;15&lt;/strong&gt;   ? Why?&lt;/p&gt;
&lt;p&gt;I'm using 2.3.9.53&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: 14" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3008"/&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>