<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to preprocessor constants in structured text</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/38a96df125/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/38a96df125/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/38a96df125/</id><updated>2020-02-27T21:28:40.767000Z</updated><subtitle>Recent posts to preprocessor constants in structured text</subtitle><entry><title>preprocessor constants in structured text</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/38a96df125/?limit=25#8e28" rel="alternate"/><published>2020-02-27T21:28:40.767000Z</published><updated>2020-02-27T21:28:40.767000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.comfb54c6a5dfb8aa5dabba1b97d83f62041a13b08e</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;There is not an equivalent to the define of C language. The closest you can come is to define the conversion as a constant first (if you can) and then define your 16 bit constants as a REAL x conversion. &lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;VAR&lt;/span&gt; &lt;span class="n"&gt;CONSTANT&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;TRIP_TEMP&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;REAL_TO_INT&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;111&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But I would suggest just defining the real and making the conversion during code execution using a function.  Unless you are running a low power embedded system, you wouldn't notice a problem.  And even in an embedded system, you probably wouldn't notice anything as most have floating point chips anyway.&lt;/p&gt;
&lt;p&gt;In my machine programs, I mostly convert all my variables to reals first!  Fieldbus values - convert to reals every PLC scan...  Then I can limit my overflow/underflow errors to a small area.  Plus it's much easier to debug when I see DriveTemp = 57.3 (C) instead of 573.&lt;/p&gt;
&lt;p&gt;Famous quote: "The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming." Donald Knuth&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>preprocessor constants in structured text</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/38a96df125/?limit=25#39f7" rel="alternate"/><published>2020-02-27T21:28:39.219000Z</published><updated>2020-02-27T21:28:39.219000Z</updated><author><name>rdb9879</name><uri>https://forge.codesys.com/u/rdb9879/</uri></author><id>https://forge.codesys.come9fafa6a164ecd8dcca272bdda4a1deaa62488d2</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Greetings,&lt;/p&gt;
&lt;p&gt;I have some analog sensor threshold constants defined in my code. Even though the sensor value is delivered as a 16-bit integer, I normally like to define the thresholds using REAL data types, with SI units (example: 112.5, units of °F). This makes the code easier to read and modify.&lt;/p&gt;
&lt;p&gt;However, in the code, I need the threshold converted to the 16-bit integer for the algorithm to use.&lt;/p&gt;
&lt;p&gt;In C code, this was a pretty simple thing to do, as I could use a #define macro to perform the conversion, and as long as my coding style ensured that the value was "const/read-only", it would optimize and implicitly convert it to the 16-bit int (no need to perform operations with REAL data types at run-time, or even have a REAL data type stored in memory).&lt;/p&gt;
&lt;p&gt;Is there something similar to this in structured text?&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>