<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: MOD operator in Libraries &amp; objects</title>
    <link>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4221#M41667</link>
    <description>&lt;BLOCKQUOTE&gt;Geoffroy wrote:&lt;BR /&gt;Here is the result I get when calculating &lt;BR /&gt;
&lt;BR /&gt;
A=120 cm&lt;BR /&gt;
B=40 cm&lt;BR /&gt;
&lt;BR /&gt;
A mod B = 0.4 !!!!
&lt;/BLOCKQUOTE&gt;

If you want to use the MOD:&lt;BR /&gt;
eps=0.0001 ! tolerance&lt;BR /&gt;
rmd=A MOD B&lt;BR /&gt;
IF ABS(rmd)&amp;lt;eps OR ABS(B-rmd)&amp;lt;eps THEN ...&lt;BR /&gt;
&lt;BR /&gt;
other vatiant:&lt;BR /&gt;
IF ABS(A-B*INT(A/B+0.5))&amp;lt;eps THEN ...&lt;BR /&gt;
&lt;BR /&gt;
PS: On A mod B = 0.4 !!!!&lt;BR /&gt;
Lets for example, 0.4 in a PC processor is 0.40000000001&lt;BR /&gt;
then 1.2 MOD 0.40000000001=3.999999999&lt;BR /&gt;
Rounds by PRINT is 0.4&lt;BR /&gt;
&lt;BR /&gt;
Oleg</description>
    <pubDate>Mon, 08 Dec 2003 18:23:40 GMT</pubDate>
    <dc:creator>Oleg</dc:creator>
    <dc:date>2003-12-08T18:23:40Z</dc:date>
    <item>
      <title>MOD operator</title>
      <link>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4218#M41664</link>
      <description>&lt;DIV class="actalk-migrated-content"&gt;&lt;T&gt;Here is the result I get when calculating &lt;BR /&gt;
&lt;BR /&gt;
A=120 cm&lt;BR /&gt;
B=40 cm&lt;BR /&gt;
&lt;BR /&gt;
A mod B = 0.4 !!!!&lt;BR /&gt;
&lt;BR /&gt;
When using PRINT A, I get 1.2, meaning that AC use the values in meter, thus&lt;BR /&gt;
1.2 mod 0.4 = 0.4&lt;BR /&gt;
&lt;BR /&gt;
But, in my point of view the rest of the divion is zero ! &lt;BR /&gt;
&lt;BR /&gt;
Is this normal ? I just want a function that will tell me if a multiple of B can fit exactly in A...&lt;BR /&gt;
&lt;BR /&gt;
My only solution at the present time is to use A*1000 mod B*1000 (so I have a precision of 1 millimeter), but I think this might be a bug...&lt;/T&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 08 Dec 2003 15:48:29 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4218#M41664</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-12-08T15:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: MOD operator</title>
      <link>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4219#M41665</link>
      <description>As the multiplication trick isn't correct, I decided to use the definition of the MOD function :&lt;BR /&gt;
X MOD Y = X - Y * INT (X/Y)&lt;BR /&gt;
&lt;BR /&gt;
When applied to X=1.2 and Y=0.4, the result is :&lt;BR /&gt;
-2.22045e-0.16... I know this is very small and wouldn't be a problem on site &lt;IMG src="https://community.graphisoft.com/legacyfs/online/emojis/icon_wink.gif" style="display : inline;" /&gt;, but I thought the result should have been 0&lt;BR /&gt;&lt;IMG src="http://community.graphisoft.com/t5/image/serverpage/image-id/68778i46BDB7E83596A10E/image-size/large?v=v2&amp;amp;px=999" border="0" alt="Capture03·12·08-16.56.42.jpg" title="Capture03·12·08-16.56.42.jpg" /&gt;</description>
      <pubDate>Mon, 08 Dec 2003 16:01:47 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4219#M41665</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2003-12-08T16:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: MOD operator</title>
      <link>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4220#M41666</link>
      <description>&lt;BLOCKQUOTE&gt;Geoffroy wrote:&lt;BR /&gt;but I thought the result should have been 0&lt;/BLOCKQUOTE&gt;

Hi Geoffroy,&lt;BR /&gt;
&lt;BR /&gt;
You've run into numerical discretization error that results from computers using binary arithmetic.  Decimal fractions cannot be represented precisely in a fixed number of binary digits (bits) - and therefore some truncation occurs.  This numerical error is magnified when arithmetic is performed, particularly subtraction which can leave nothing but the error remaining.  The branch of Computer Science/Mathematics called 'Numerical Analysis' deals with these issues and more.&lt;BR /&gt;
&lt;BR /&gt;
There is no problem if you are using MOD with integer (whole) values.&lt;BR /&gt;
&lt;BR /&gt;
Attached is a screenshot of an Excel spreadsheet and the results of computing the same expression as you used in five different ways.&lt;BR /&gt;
&lt;BR /&gt;
Row 1 uses the Excel MOD operator - and the result looks strange, as did yours.  Decimal fractions generate infinitely repeating binary fractions - and so when a number is stored in a fixed number of bits, part of the number is lost.  You're seeing that those missing bits result in an error of:&lt;BR /&gt;
0.0000000000000001&lt;BR /&gt;
which is pretty tiny after all .. but is certainly not zero.  I talked about this on GDLTalk a while back - and which is probably where this conversation belongs, except that I cannot post screenshots there.  One needs to allow for this numerical error with some kind of factor, epsilon, and realize that the result will be within +/- epsilon of the true result.  In this case, anything that is between  + or - 1E-15 could safely be said to be zero.  As you say, for construction purposes, we could set epsilon to be much larger and still call it zero.&lt;BR /&gt;
&lt;BR /&gt;
Row 2 miraculously shows zero using just your forumula.  The following three rows explore this further.&lt;BR /&gt;
&lt;BR /&gt;
Row 3 uses the function smod defined in VisualBasic in the module at the left and uses single precision arithmetic - which is apparently the default precision of Excel since this row and row 2 both give 0.  Now, single precision should exhibit the same kind of numerical error - but at 1E-8 or so, so I think we just had good luck here.&lt;BR /&gt;
&lt;BR /&gt;
Row 4 shows the next VB function - same formula but with double precision values and arithmetic.  Pretty odd, yes?  It gives the 0.4 value that you got from ArchiCAD - which suggests to me that the ArchiCAD implementation of the MOD function is incorrect.  The reason is easy to see.  INT(X/Y) evaluated to 2 instead of 3 - thus giving the result of 0.4.  Well, how can this be?  Simply because INT truncates (chops) the fraction from a number leaving the whole part.  If the result of the division was 2.999999999999999 then the result of the INT would indeed be 2.  Why all the 9's?  Same deal as above.&lt;BR /&gt;
&lt;BR /&gt;
Row 5 shows one solution to implementing the MOD function - and the result matches exactly line 1, which is the Excel built-in MOD function.  Here, I added an epsilon factor of 1E-15 to the result of the division to balance the numerical error.  In some case, I might get 3.000000000000001 or slightly more, but for the MOD function this doesn't matter, since INT will chop it all off.&lt;BR /&gt;
&lt;BR /&gt;
Whew.  &lt;IMG src="https://community.graphisoft.com/legacyfs/online/emojis/icon_rolleyes.gif" style="display : inline;" /&gt;   Looks like you did find a bug to report to GS, though!&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Karl&lt;BR /&gt;&lt;IMG src="https://community.graphisoft.com/t5/image/serverpage/image-id/16378i635BD3514F3FFCC8/image-size/large?v=v2&amp;amp;px=999" border="0" alt="mod.png" title="mod.png" /&gt;</description>
      <pubDate>Mon, 08 Dec 2003 18:00:33 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4220#M41666</guid>
      <dc:creator>Karl Ottenstein</dc:creator>
      <dc:date>2003-12-08T18:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: MOD operator</title>
      <link>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4221#M41667</link>
      <description>&lt;BLOCKQUOTE&gt;Geoffroy wrote:&lt;BR /&gt;Here is the result I get when calculating &lt;BR /&gt;
&lt;BR /&gt;
A=120 cm&lt;BR /&gt;
B=40 cm&lt;BR /&gt;
&lt;BR /&gt;
A mod B = 0.4 !!!!
&lt;/BLOCKQUOTE&gt;

If you want to use the MOD:&lt;BR /&gt;
eps=0.0001 ! tolerance&lt;BR /&gt;
rmd=A MOD B&lt;BR /&gt;
IF ABS(rmd)&amp;lt;eps OR ABS(B-rmd)&amp;lt;eps THEN ...&lt;BR /&gt;
&lt;BR /&gt;
other vatiant:&lt;BR /&gt;
IF ABS(A-B*INT(A/B+0.5))&amp;lt;eps THEN ...&lt;BR /&gt;
&lt;BR /&gt;
PS: On A mod B = 0.4 !!!!&lt;BR /&gt;
Lets for example, 0.4 in a PC processor is 0.40000000001&lt;BR /&gt;
then 1.2 MOD 0.40000000001=3.999999999&lt;BR /&gt;
Rounds by PRINT is 0.4&lt;BR /&gt;
&lt;BR /&gt;
Oleg</description>
      <pubDate>Mon, 08 Dec 2003 18:23:40 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4221#M41667</guid>
      <dc:creator>Oleg</dc:creator>
      <dc:date>2003-12-08T18:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: MOD operator</title>
      <link>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4222#M41668</link>
      <description>&lt;BLOCKQUOTE&gt;Geoffroy wrote:&lt;BR /&gt;When using PRINT A, I get 1.2, meaning that AC use the values in meter, thus &lt;BR /&gt;
1.2 mod 0.4 = 0.4 &lt;/BLOCKQUOTE&gt;

I'm still fairly new to GDL, but many of the people helping me with GDL that have background with other scripting languages always ask about a PRINT so it will show the value. Are you scripting straight through Archicad to do this or is it 3rd party program.</description>
      <pubDate>Mon, 08 Dec 2003 20:25:45 GMT</pubDate>
      <guid>https://community.graphisoft.com/t5/Libraries-objects/MOD-operator/m-p/4222#M41668</guid>
      <dc:creator>Red</dc:creator>
      <dc:date>2003-12-08T20:25:45Z</dc:date>
    </item>
  </channel>
</rss>

