<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Programming Works</title>
	<atom:link href="http://sergworks.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sergworks.wordpress.com</link>
	<description>Delphi programming and more</description>
	<lastBuildDate>Thu, 16 May 2013 00:20:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sergworks.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Programming Works</title>
		<link>http://sergworks.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sergworks.wordpress.com/osd.xml" title="The Programming Works" />
	<atom:link rel='hub' href='http://sergworks.wordpress.com/?pushpress=hub'/>
		<item>
		<title>On the operator overloading in Delphi</title>
		<link>http://sergworks.wordpress.com/2013/04/10/on-the-operator-overloading-in-delphi/</link>
		<comments>http://sergworks.wordpress.com/2013/04/10/on-the-operator-overloading-in-delphi/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 09:15:22 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[operator overloading]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=854</guid>
		<description><![CDATA[The operator overloading in Delphi records is straightforward if a record type does not contain fields which reference heap objects. To illustrate the problem which heap references arise let us consider the following (incorrect) example: The line #59 (C:= A + B) in the above program works as follows: A temporary Result record is pushed [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=854&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The operator overloading in Delphi records is straightforward if a record type does not contain fields which reference heap objects. To illustrate the problem which heap references arise let us consider the following (incorrect) example:</p>
<pre class="brush: delphi; title: ; notranslate">
program DelphiDemo;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  Adder = record
  private
    FRef: PInteger;
    function GetMemory: Integer;
    procedure SetMemory(AValue: Integer);
  public
    procedure Init(AValue: Integer = 0);
    procedure Done;
    class operator Add(const A, B: Adder): Adder;
    property Memory: Integer read GetMemory write SetMemory;
  end;

{ Adder }

class operator Adder.Add(const A, B: Adder): Adder;
begin
// !!! Memory leak
  New(Result.FRef);
  Result.Memory:= A.Memory + B.Memory;
end;

procedure Adder.Done;
begin
  Dispose(FRef);
end;

function Adder.GetMemory: Integer;
begin
  Result:= FRef^;
end;

procedure Adder.Init(AValue: Integer);
begin
  New(FRef);
  FRef^:= AValue;
end;

procedure Adder.SetMemory(AValue: Integer);
begin
  FRef^:= AValue;
end;

procedure Test;
var
  A, B, C: Adder;

begin
  A.Init(1);
  B.Init(2);
  C.Init();
  C:= A + B;
  Writeln(C.Memory);
  C.Done;
  B.Done;
  A.Done;
end;

begin
  ReportMemoryLeaksOnShutdown:= True;
  try
    Test;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.
</pre>
<p>The line #59 (<em>C:= A + B</em>) in the above program works as follows:</p>
<ul>
<li>A temporary <em>Result</em> record is pushed on stack</li>
<li>The temporary record receives the sum <em>A + B</em></li>
<li>The temporary record is assigned (by shallow copying) to the C variable</li>
<li>The temporary record is popped from stack</li>
</ul>
<p>It would work fine if <em>Adder</em> did not reference a heap data; the <em>FRef</em> of the <em>Adder</em> record field makes things complicated. You should always initialize <em>FRef</em> field for every <em>Adder</em> instance but you can&#8217;t finalize it for a temporary record that is created on line #59. The only way to solve the memory leak issue in the above code is to comment out the line #58, but it will not work for more complicated right-hand side expressions and it is not a solid approach anyway.</p>
<p>The correct solution involves using a type with automatic memory management instead of a simple pointer. Here is a solution that uses interface:</p>
<pre class="brush: delphi; title: ; notranslate">
program DelphiDemo2;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

type
  IAdder = interface
    function GetMemory: Integer;
    procedure SetMemory(AValue: Integer);
  end;

  TAdderRef = class(TInterfacedObject, IAdder)
  private
    FMemory: Integer;
    function GetMemory: Integer;
    procedure SetMemory(AValue: Integer);
  end;

  Adder = record
  private
    FRef: IAdder;
    function GetMemory: Integer;
    procedure SetMemory(AValue: Integer);
  public
    procedure Init(AValue: Integer = 0);
    procedure Done;
    class operator Add(const A, B: Adder): Adder;
    property Memory: Integer read GetMemory write SetMemory;
  end;

{ TAdderRef }

function TAdderRef.GetMemory: Integer;
begin
  Result:= FMemory;
end;

procedure TAdderRef.SetMemory(AValue: Integer);
begin
  FMemory:= AValue;
end;

{ Adder }

class operator Adder.Add(const A, B: Adder): Adder;
begin
  Result.FRef:= TAdderRef.Create;
  Result.Memory:= A.Memory + B.Memory;
end;

procedure Adder.Init(AValue: Integer);
begin
  FRef:= TAdderRef.Create;
  FRef.SetMemory(AValue);
end;

procedure Adder.Done;
begin
  FRef:= nil;
end;

function Adder.GetMemory: Integer;
begin
  Result:= FRef.GetMemory;
end;

procedure Adder.SetMemory(AValue: Integer);
begin
  FRef.SetMemory(AValue);
end;

procedure Test;
var
  A, B, C: Adder;

begin
  A.Init(1);
  B.Init(2);
//  C.Init();
  C:= A + B;
  Writeln(C.Memory);
//  C.Done;
//  B.Done;
//  A.Done;
end;

begin
  ReportMemoryLeaksOnShutdown:= True;
  try
    Test;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.
</pre>
<p>A nice side effect of the above approach is that you need not initialize or finalize the <em>FRef</em> fields manually anymore (though you can still do it). Some lines in the <em>Test</em> procedure above were commented out because they are not needed, but they can be uncommented and the code will remain correct &#8211; automatic memory management of interfaces takes care of it.</p>
<p>It is very interesting to know how the problem discussed above is solved in <em>C++</em>. The standard <em>C++</em> approach is totally different &#8211; it involves overloading the <em>assignment operator</em> (a feature which Delphi does not support; Delphi allows to overload only <em>conversion</em> &#8211; <em>Implicit</em>, <em>Explicit</em> operators) and writing a <em>copy constructor</em> (another concept absent in Delphi object model). I am planning to discuss it later.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/854/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=854&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2013/04/10/on-the-operator-overloading-in-delphi/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
		<item>
		<title>On the Pointers and References</title>
		<link>http://sergworks.wordpress.com/2013/04/03/on-the-pointers-and-references/</link>
		<comments>http://sergworks.wordpress.com/2013/04/03/on-the-pointers-and-references/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 08:44:49 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[musings]]></category>
		<category><![CDATA[Pascal]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Reference]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=814</guid>
		<description><![CDATA[A Reference is a distinct concept in C/C++ languages. The next code sample (MinGW GCC compiler was used) declares ValueRef as a reference to Value variable. The output is Though ValueRef variable is a pointer to Value internally the indirection operator is never used, and the syntax for accessing Value directly or indirectly via ValueRef [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=814&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A <em>Reference</em> is a distinct concept in C/C++ languages. The next code sample (MinGW GCC compiler was used)</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

int main()
  {
    int Value;
    int &amp;ValueRef = Value;

    Value = 2;
    std::cout &lt;&lt; &quot;Value: &quot; &lt;&lt; Value &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; &quot;ValueRef: &quot; &lt;&lt; ValueRef &lt;&lt; &quot;\n&quot;;

    ValueRef = 3;
    std::cout &lt;&lt; &quot;Value: &quot; &lt;&lt; Value &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; &quot;ValueRef: &quot; &lt;&lt; ValueRef &lt;&lt; &quot;\n&quot;;
    return 0;
  }
</pre>
<p>declares <em>ValueRef</em> as a reference to <em>Value</em> variable. The output is<br />
<a href="http://sergworks.files.wordpress.com/2013/04/ref1.png"><img src="http://sergworks.files.wordpress.com/2013/04/ref1.png?w=683" alt="_ref1"   class="aligncenter size-full wp-image-842" /></a><br />
Though <em>ValueRef</em> variable is a pointer to <em>Value</em> internally the indirection operator is never used, and the syntax for accessing <em>Value</em> directly or indirectly via <em>ValueRef</em> is the same. <em>ValueRef</em> is also called an <em>alias</em> to <em>Value</em>; the point that <em>ValueRef</em> variable is a pointer internally is just an implementation detail.</p>
<p>Another important thing about C/C++ references is that they are always initialized. The language syntax enforces that you cannot declare a wild reference.</p>
<p>Pascal does not have the same reference concept. The closest concept is a procedure parameter passed by reference:</p>
<pre class="brush: delphi; title: ; notranslate">
program ref;

{$APPTYPE CONSOLE}

var
  Value: Integer;

procedure Test(var ValueRef: Integer);
begin
  Writeln('ValueRef: ', ValueRef);
  ValueRef:= 3;
end;

begin
  Value:= 2;
  Writeln('Value: ', Value);
  Test(Value);

  Writeln('Value: ', Value);
  Test(Value);
end.
</pre>
<p>we can see that</p>
<ul>
<li><em>ValueRef</em> does not use indirection operator to access a referenced variable;</li>
<li>the language syntax enforces that <em>ValueRef</em> is always initialized.</li>
</ul>
<p>Delphi does not elaborate the reference concept, though there are many built-in types in the language that are &#8216;transparent pointers&#8217; &#8211; objects, interfaces, dynamic arrays, strings. The term <em>reference</em> can be used for example for object variables because the language syntax makes these variables indistinguishable from referenced instances. Instead the term <em>object</em> is usually used, that can mean <em>object reference</em> or <em>object instance</em>, so sometimes you think twice to understand what a particular <em>object</em> does mean.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/814/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/814/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=814&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2013/04/03/on-the-pointers-and-references/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>

		<media:content url="http://sergworks.files.wordpress.com/2013/04/ref1.png" medium="image">
			<media:title type="html">_ref1</media:title>
		</media:content>
	</item>
		<item>
		<title>Bitwise operations on big integers</title>
		<link>http://sergworks.wordpress.com/2012/12/03/bitwise-operations-on-big-integers/</link>
		<comments>http://sergworks.wordpress.com/2012/12/03/bitwise-operations-on-big-integers/#comments</comments>
		<pubDate>Mon, 03 Dec 2012 07:11:40 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[biginteger]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[free pascal]]></category>
		<category><![CDATA[multiple-precision]]></category>
		<category><![CDATA[musings]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=795</guid>
		<description><![CDATA[Standard fixed-sized negative integers are stored in two&#8217;s complement format; for arbitrary-precision big integers the two&#8217;s complement format means infinite size, so internally it is not used. Still bitwise operation on big integers are implemented as if negative big integer values are stored in two&#8217;s complement format. As a result bitwise boolean operations (and, or, [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=795&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Standard fixed-sized negative integers are stored in <a href="http://en.wikipedia.org/wiki/Two's_complement">two&#8217;s complement</a> format; for arbitrary-precision big integers the two&#8217;s complement format means infinite size, so internally it is not used. Still bitwise operation on big integers are implemented as if negative big integer values are stored in two&#8217;s complement format.</p>
<p>As a result bitwise boolean operations (<em>and</em>, <em>or</em>, <em>xor</em>) applied to big integers produce the same results as with standard fixed-sized integers:</p>
<pre class="brush: delphi; title: ; notranslate">
procedure Test1(I, J: Integer);
var
  BigI, BigJ: BigInteger;

begin
  BigI:= I;
  BigJ:= J;
  Assert(BigI and BigJ = I and J);
  Assert(BigI or BigJ = I or J);
  Assert(BigI xor BigJ = I xor J);
end;
</pre>
<p>There is a difference between standard Delphi integer types and big integers in shift operations. Shift operations on big integers preserve sign. That means any shift applied to negative big integer results in negative big integer (the same is for non-negative values):</p>
<pre class="brush: delphi; title: ; notranslate">
procedure Test2(I: BigInteger; N: Cardinal);
begin
  Assert(((I shl N) &lt; 0) = (I &lt; 0));
  Assert(((I shr N) &lt; 0) = (I &lt; 0));
end;
</pre>
<p>That is a natural consequence of the infinite-sized two&#8217;s complement negative values. Shift operations on big integers are arithmetic shifts rather than logical shifts. On the other hand <em>&#8216;shl&#8217;</em> and <em>&#8216;shr&#8217;</em> operations on the standard Delphi integer types are implemented as logical shifts and does not preserve sign:</p>
<pre class="brush: delphi; title: ; notranslate">
procedure Test3;
var
  I: Integer;

begin
  I:= -1;
  Writeln(I shr 2);   // 1073741823, because 'shr' is logical shift
  I:= 1000000000;
  Writeln(I shl 2);   // -294967296, because of 32-bit overflow
end;
</pre>
<p>PS: right now <a href="https://bitbucket.org/sergworks/tforge">TForge</a> does not support bitwise operations on <em>BigInteger</em> type, they will be implemented soon.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/795/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/795/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=795&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/12/03/bitwise-operations-on-big-integers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
		<item>
		<title>Congratulations to Marco Cantu</title>
		<link>http://sergworks.wordpress.com/2012/11/08/congratulations-to-marco-cantu/</link>
		<comments>http://sergworks.wordpress.com/2012/11/08/congratulations-to-marco-cantu/#comments</comments>
		<pubDate>Thu, 08 Nov 2012 15:33:23 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=790</guid>
		<description><![CDATA[As everyone else I wish only the best to Marco Cantu in his new appointment. November 2012 is the date Marco joined Embarcadero as Delphi Product Manager. I wish him luck and success, still I share some skepticism expressed in Delphi blogosphere. So I want to ask Delphi community:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=790&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>As everyone else I wish only the best to Marco Cantu in his new appointment. November 2012 is the date Marco joined Embarcadero as Delphi Product Manager. I wish him luck and success, still I share some skepticism expressed in Delphi blogosphere. </p>
<p>So I want to ask Delphi community:</p>
<a name="pd_a_6674857"></a>
<div class="PDS_Poll" id="PDI_container6674857" data-settings="{&quot;url&quot;:&quot;http:\/\/static.polldaddy.com\/p\/6674857.js&quot;}" style="display:inline-block;"></div>
<div id="PD_superContainer"></div>
<noscript><a href="http://polldaddy.com/poll/6674857">Take Our Poll</a></noscript>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/790/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/790/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=790&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/11/08/congratulations-to-marco-cantu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
		<item>
		<title>Be careful with Ord function in Unicode Delphi versions</title>
		<link>http://sergworks.wordpress.com/2012/11/03/be-careful-with-ord-function-in-unicode-delphi-versions/</link>
		<comments>http://sergworks.wordpress.com/2012/11/03/be-careful-with-ord-function-in-unicode-delphi-versions/#comments</comments>
		<pubDate>Sat, 03 Nov 2012 10:53:49 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[language delphi]]></category>
		<category><![CDATA[Ord]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=772</guid>
		<description><![CDATA[Here is a simple test: While evaluating the Ord function with hardcoded character parameter the compiler treats the parameter as ANSI character. In the above example Ord(‘Я’) returns 223 (Cyrillic codepage 1251) instead of 1071 (UTF16) as one could expect. As a result the assertion fails (tested on Delphi XE): After reading the comments I [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=772&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Here is a simple test:</p>
<pre class="brush: delphi; title: ; notranslate">
program OrdTest;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  try
    Writeln(Ord('Я'), '  ', Ord(Char('Я')));   // 223,  1071
    Assert(Ord('Я') = Ord(Char('Я')));         // Fails
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
</pre>
<p>While evaluating the <em>Ord</em> function with hardcoded character parameter the compiler treats the parameter as ANSI character. In the above example <em>Ord(‘Я’)</em> returns 223 (Cyrillic codepage 1251) instead of 1071 (UTF16) as one could expect. As a result the assertion fails (tested on Delphi XE):<br />
<a href="http://sergworks.files.wordpress.com/2012/11/assert.png"><img src="http://sergworks.files.wordpress.com/2012/11/assert.png?w=683" alt="assertion failed" title="assert"   class="aligncenter size-full wp-image-773" /></a></p>
<p>After reading the comments I tried another test with both Cyrillic &#8216;Я&#8217; (=223 on 1251 codepage) and German &#8216;ß&#8217; (=223 on 1252 codepage):</p>
<pre class="brush: delphi; title: ; notranslate">
program OrdTest2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  try
    Writeln(Ord('Я'), '  ', Ord(Char('Я')));
    Writeln(Ord('ß'), '  ', Ord(Char('ß')));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
</pre>
<p>if I set the compiler&#8217;s codepage to 1251 I get</p>
<p><a href="http://sergworks.files.wordpress.com/2012/11/01.png"><img src="http://sergworks.files.wordpress.com/2012/11/01.png?w=683" alt="" title="01"   class="aligncenter size-full wp-image-781" /></a></p>
<p>if I set the compiler&#8217;s codepage to 1252 I get</p>
<p><a href="http://sergworks.files.wordpress.com/2012/11/02.png"><img src="http://sergworks.files.wordpress.com/2012/11/02.png?w=683" alt="" title="02"   class="aligncenter size-full wp-image-782" /></a></p>
<p>because German &#8216;ß&#8217; has the same code (223) both in ANSI 1252 codepage and UTF16 encoding.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/772/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=772&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/11/03/be-careful-with-ord-function-in-unicode-delphi-versions/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>

		<media:content url="http://sergworks.files.wordpress.com/2012/11/assert.png" medium="image">
			<media:title type="html">assert</media:title>
		</media:content>

		<media:content url="http://sergworks.files.wordpress.com/2012/11/01.png" medium="image">
			<media:title type="html">01</media:title>
		</media:content>

		<media:content url="http://sergworks.files.wordpress.com/2012/11/02.png" medium="image">
			<media:title type="html">02</media:title>
		</media:content>
	</item>
		<item>
		<title>Benchmarking BigInteger</title>
		<link>http://sergworks.wordpress.com/2012/10/17/benchmarking-biginteger/</link>
		<comments>http://sergworks.wordpress.com/2012/10/17/benchmarking-biginteger/#comments</comments>
		<pubDate>Wed, 17 Oct 2012 17:40:14 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[biginteger]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[digits of pi]]></category>
		<category><![CDATA[language delphi]]></category>
		<category><![CDATA[multiple-precision]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=761</guid>
		<description><![CDATA[Last TForge commits contain PiBench console application that calculates decimal digits of Pi using Machin’s formula; the execution takes about 30 seconds on my laptop. The code was written to compare the execution time of subsequent TForge revisions, but it can also be used to compare TForge BigCardinal and BigInteger types with .NET 4.0 BigInteger [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=761&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Last <a href="https://bitbucket.org/sergworks/tforge">TForge</a> commits contain <em>PiBench</em> console application that calculates decimal digits of Pi using <a href="http://mathworld.wolfram.com/MachinsFormula.html">Machin’s formula</a>; the execution takes about 30 seconds on my laptop. The code was written to compare the execution time of subsequent TForge revisions, but it can also be used to compare TForge <em>BigCardinal</em> and <em>BigInteger</em> types with .NET 4.0 <em>BigInteger</em> type.</p>
<p>Here is Delphi PiBench code:</p>
<pre class="brush: delphi; title: ; notranslate">
program PiBench;

{$APPTYPE CONSOLE}

uses
  SysUtils, Diagnostics, tfNumerics;

var
  StopWatch: TStopWatch;
  PiDigits: BigCardinal;

procedure BenchMark;
var
  Factor, Num, Den: BigCardinal;
  Term: BigCardinal;
  N, M: Cardinal;

begin
  PiDigits:= 0;
  Factor:= BigCardinal.Pow(10, 10000);
  Num:= 16 * Factor;
  Den:= 5;
  N:= 1;
  repeat
    Term:= Num div (Den * (2 * N - 1));
    if Term = 0 then Break;
    if Odd(N)
      then PiDigits:= PiDigits + Term
      else PiDigits:= PiDigits - Term;
    Den:= Den * 25;
    Inc(N);
  until N = 0;
  M:= N;
  Num:= 4 * Factor;
  Den:= 239;
  N:= 1;
  repeat
    Term:= Num div (Den * (2 * N - 1));
    if Term = 0 then Break;
    if Odd(N)
      then PiDigits:= PiDigits - Term
      else PiDigits:= PiDigits + Term;
    Den:= Den * 239 * 239;
    Inc(N);
  until N = 0;
  M:= (M + N) div 2;
// M last digits may be wrong
  PiDigits:= PiDigits div BigCardinal.Pow(10, M);
end;

begin
  ReportMemoryLeaksOnShutdown:= True;
  try
    Writeln('Benchmark test started ...');
    StopWatch:= TStopWatch.StartNew;
    BenchMark;
    StopWatch.Stop;
    Writeln(PiDigits.AsString);
    PiDigits.Free;
    Writeln;
    Writeln('Elapsed ms: ', StopWatch.ElapsedMilliseconds);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
</pre>
<p>And here is equivalent C# application:</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Numerics;
using System.Diagnostics;

namespace PiBench
{
    class Program
    {
        static void Main(string[] args)
        {
            int N, M;
            BigInteger Term;
            Console.WriteLine(&quot;Benchmark test started ...&quot;);
            Stopwatch Watch = new Stopwatch();
            Watch.Start();
            BigInteger PiDigits = 0;
            BigInteger Factor = BigInteger.Pow(10, 10000);
            BigInteger Num = 16 * Factor;
            BigInteger Den = 5;
            for(N = 1; N != 0; N++){
              Term = Num / (Den * (2 * N - 1));
              if (Term == 0) break;
              if ((N &amp; 1) != 0) PiDigits = PiDigits + Term;
              else PiDigits = PiDigits - Term;
              Den = Den * 25;
            }
            M = N;
            Num = 4 * Factor;
            Den = 239;
            for(N = 1; N != 0; N++){
              Term = Num / (Den * (2 * N - 1));
              if (Term == 0) break;
              if ((N &amp; 1) != 0) PiDigits = PiDigits - Term;
              else PiDigits = PiDigits + Term;
              Den = Den * 239 * 239;
            }
            M = (M + N) / 2;
       // M last digits may be wrong
            PiDigits = PiDigits / BigInteger.Pow(10, M);
            Watch.Stop();
            Console.WriteLine(PiDigits);
            Console.WriteLine();
            Console.WriteLine(&quot;Elapsed ms: &quot; + Watch.ElapsedMilliseconds.ToString());
            Console.ReadLine();
        }
    }
}
</pre>
<p>You can run both and compare; my tests show nearly the same execution time.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/761/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=761&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/10/17/benchmarking-biginteger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
		<item>
		<title>TForge</title>
		<link>http://sergworks.wordpress.com/2012/10/09/tforge/</link>
		<comments>http://sergworks.wordpress.com/2012/10/09/tforge/#comments</comments>
		<pubDate>Tue, 09 Oct 2012 11:44:00 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arithmetic]]></category>
		<category><![CDATA[biginteger]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[free pascal]]></category>
		<category><![CDATA[integer arithmetic]]></category>
		<category><![CDATA[language delphi]]></category>
		<category><![CDATA[multiple-precision]]></category>
		<category><![CDATA[TForge]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=750</guid>
		<description><![CDATA[My interface-based arbitrary precision integer arithmetic implementation for Delphi and Free Pascal now is a project on BitBucket. Currently TForge is a single runtime package (tforge.dpk; future releases will also include a language agnostic dll); you can download the project, build the package and start hacking with BigCardinal and BigInteger types by adding tfNumerics unit [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=750&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My interface-based arbitrary precision integer arithmetic implementation for Delphi and Free Pascal now is a <a href="https://bitbucket.org/sergworks/tforge">project on BitBucket</a>.<br />
Currently <em>TForge</em> is a single runtime package (<em>tforge.dpk</em>; future releases will also include a language agnostic dll); you can download the project, build the package and start hacking with <em>BigCardinal</em> and <em>BigInteger</em> types by adding <em>tfNumerics</em> unit to <em>uses</em> clause.<br />
The code is not fully tested yet and not optimized at all, so use it carefully.</p>
<p>Here are some implementation details:</p>
<p>1. <em>BigInteger</em> and <em>BigCardinal</em> variables are initialized by assignment:</p>
<pre class="brush: delphi; title: ; notranslate">
var A: BigCardinal;
    B: BigInteger;

begin
   A:= 1;
   B:= 2;
   Writeln(A.AsString, ', ', B.AsString);
   ..
</pre>
<p>2. No need to finalize <em>BigInteger</em> and <em>BigCardinal</em> variables, the compiler does cleanup when a variable goes out of scope; still you can finalize explicitly by <em>Free</em> method. You can also reuse a ‘Freed’ variable:</p>
<pre class="brush: delphi; title: ; notranslate">
var A: BigCardinal;

   A:= 1;
   A.Free;
   A:= 10;
   Writeln(A.AsString);
</pre>
<p>3. Strings can be explicitly casted to <em>BigInteger</em> and <em>BigCardinal</em> types; you can also use <em>TryFromString</em> method to assign a string value to a <em>BigInteger</em> or <em>BigCardinal</em> variable:</p>
<pre class="brush: delphi; title: ; notranslate">
var A: BigCardinal;
    B: BigInteger;

begin
  try
   A:= 1;
   B:= BigInteger('1234567890987654321');
   if not A.TryFromString('Wrong string')
     then Writeln('Bad value');
   Writeln(A.AsString, ', ', B.AsString);
</pre>
<p>4. <em>BigInteger</em> accommodates <em>BigCardinal</em>, so <em>BigCardinal</em> is implicitly casted to <em>BigInteger</em>, and <em>BigInteger</em> is explicitly casted to <em>BigCardinal</em>:</p>
<pre class="brush: delphi; title: ; notranslate">
var A: BigCardinal;
    B: BigInteger;

begin
  A:= 1;
  B:= A; // never raises exception
  A:= BigCardinal(B); // no exception here
  B:= -1;
  A:= BigCardinal(B); // exception – negative value
  ..
</pre>
<p>5. <em>BigInteger</em> and <em>BigCardinal</em> variables can be mixed in Boolean expressions, with <em>BigCardinal</em> casted to <em>BigInteger</em>:</p>
<pre class="brush: delphi; title: ; notranslate">
var A: BigCardinal;
    B: BigInteger;

begin
    A:= 1;
    B:= -2;
    if (A &gt; B) then B:= 0;
    ..
</pre>
<p>6. <em>BigInteger</em> and <em>BigCardinal</em> variables can be mixed in arithmetic expressions, with <em>BigCardinal</em> casted to <em>BigInteger</em>:</p>
<pre class="brush: delphi; title: ; notranslate">
var A: BigCardinal;
    B: BigInteger;

begin
    A:= 10;
    B:= -42;
    Writeln( (A * B).AsString);
</pre>
<p>The project has a public bug tracker, bug reports are welcome!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/750/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/750/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=750&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/10/09/tforge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
		<item>
		<title>A word about ‘out’ parameters in Delphi</title>
		<link>http://sergworks.wordpress.com/2012/10/01/a-word-about-out-parameters-in-delphi/</link>
		<comments>http://sergworks.wordpress.com/2012/10/01/a-word-about-out-parameters-in-delphi/#comments</comments>
		<pubDate>Mon, 01 Oct 2012 10:37:08 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[musings]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=736</guid>
		<description><![CDATA[Delphi compiler has a contract – any interface variable should be either a valid reference or nil. The same also applies to other lifetime managed types – strings and dynamic arrays. An interesting consequence of the contract is how Delphi interprets ‘out’ function parameters. As the name suggests, ‘out’ means that an input value of [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=736&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Delphi compiler has a contract – any interface variable should be either a valid reference or <em>nil</em>. The same also applies to other lifetime managed types – strings and dynamic arrays. An interesting consequence of the contract is how Delphi interprets ‘<em>out</em>’ function parameters.<br />
As the name suggests, ‘<em>out</em>’ means that an input value of a parameter should be ignored inside a function; but Delphi compiler cannot ignore input value of a lifetime managed instance. It decrements the reference count of an instance when an instance is no longer needed if a reference to instance is not <em>nil</em>.<br />
As a workaround Delphi compiler applies the following trick. Suppose we have a procedure</p>
<pre class="brush: delphi; title: ; notranslate">
procedure Foo(out II: IInterface);
begin
  II:= TInterfacedObject.Create;
end;
</pre>
<p>a call of the procedure</p>
<pre class="brush: delphi; title: ; notranslate">
  Foo(II);
</pre>
<p>compiles as:</p>
<pre class="brush: delphi; title: ; notranslate">
II:= nil;
Bar(II);
</pre>
<p>where</p>
<pre class="brush: delphi; title: ; notranslate">
procedure Bar(var II: IInterface);
begin
  II:= TInterfacedObject.Create;
end;
</pre>
<p><em>Bar</em> procedure decrements the reference count of the input parameter instance; the meaning of &#8216;<em>out</em>&#8216; parameter forbids to do so. Since Delphi compiler cannot ignore input value of a lifetime manage instance the problem was solved by assigning <em>nil</em> value to a parameter before calling a procedure.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/736/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/736/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=736&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/10/01/a-word-about-out-parameters-in-delphi/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
		<item>
		<title>Big Integer redux</title>
		<link>http://sergworks.wordpress.com/2012/09/27/big-integer-redux/</link>
		<comments>http://sergworks.wordpress.com/2012/09/27/big-integer-redux/#comments</comments>
		<pubDate>Thu, 27 Sep 2012 08:33:50 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arithmetic]]></category>
		<category><![CDATA[Delphi]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=731</guid>
		<description><![CDATA[I started a project that will include Big Integer math implementation (common Delphi&#38;FPC codebase) based on interfaces without objects. Right now the project reached the stage where I am able to compile (and run!) code like that: Output: A lot still yet to be done, but it works!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=731&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I started a project that will include Big Integer math implementation (common Delphi&amp;FPC codebase) based on <a href="http://sergworks.wordpress.com/2012/04/01/interfaces-without-objects/">interfaces without objects</a>. Right now the project reached the stage where I am able to compile (and run!) code like that:</p>
<pre class="brush: delphi; title: ; notranslate">
{
  Usage example: BinomCoff 120 42
  Demonstrates how to use BigCardinal type
  see also:

http://rosettacode.org/wiki/Evaluate_binomial_coefficients#Delphi

}
program BinomCoff;

{$APPTYPE CONSOLE}

uses
  SysUtils, tfNumerics;

function BinomialCoff(N, K: Cardinal): BigCardinal;
var
  L: Cardinal;

begin
  if N &lt; K then
    Result:= 0      // Error
  else begin
    if K &gt; N - K then
      K:= N - K;    // Optimization
    Result:= 1;
    L:= 0;
    while L &lt; K do begin
      Result:= Result * (N - L);
      Inc(L);
      Result:= Result div L;
    end;
  end;
end;

var
  A: BigCardinal;
  M, N: Cardinal;

begin
  ReportMemoryLeaksOnShutdown:= True;
  try
    if ParamCount &lt;&gt; 2 then begin
      Writeln('Usage example: BinomCoff 120 42');
      ReadLn;
      Exit;
    end;
    N:= StrToInt(ParamStr(1));
    M:= StrToInt(ParamStr(2));
    A:= BinomialCoff(N, M);
    Writeln('C(', N, ', ', M, ') = ', A.AsString);
    A:= BigCardinal(nil);   // A is global var and should be freed explicitely
                            //   to prevent memory leak on shutdown
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.
</pre>
<p>Output:</p>
<p><a href="http://sergworks.files.wordpress.com/2012/09/bincoff.png"><img src="http://sergworks.files.wordpress.com/2012/09/bincoff.png?w=683" alt="" title="BinCoff"   class="aligncenter size-full wp-image-732" /></a></p>
<p>A lot still yet to be done, but it works! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/731/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/731/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=731&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/09/27/big-integer-redux/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>

		<media:content url="http://sergworks.files.wordpress.com/2012/09/bincoff.png" medium="image">
			<media:title type="html">BinCoff</media:title>
		</media:content>
	</item>
		<item>
		<title>Why StackOverflow sucks</title>
		<link>http://sergworks.wordpress.com/2012/09/26/why-stackoverflow-sucks/</link>
		<comments>http://sergworks.wordpress.com/2012/09/26/why-stackoverflow-sucks/#comments</comments>
		<pubDate>Wed, 26 Sep 2012 03:58:03 +0000</pubDate>
		<dc:creator>Serg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[StackOverflow]]></category>

		<guid isPermaLink="false">http://sergworks.wordpress.com/?p=715</guid>
		<description><![CDATA[StackOverflow is a monster Q&#38;A machine. If you have a programming question, StackOverflow is probably the best place to ask – you have a better chance to get an answer on SO than anywhere else. The paradox is that SO is not interested in users getting answers to their questions. Usually Q&#38;A sites want their [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=715&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>StackOverflow is a monster Q&amp;A machine. If you have a programming question, StackOverflow is probably the best place to ask – you have a better chance to get an answer on SO than anywhere else.<br />
The paradox is that SO is not interested in users getting answers to their questions. Usually Q&amp;A sites want their questioners to be happy, but not SO. SO wants great questions and great answers. Hence the reputation system and an army of Nazi retards moderating everything they can see.<br />
If a question is considered poor by the user with moderating privileges, it will be downvoted, closed and finally deleted. But that is not all – SO has an automatic ban system. Users providing questions &amp; answers that received low marks can be banned by robots.<br />
One of the first questions I answered on SO more that 2 years ago was:</p>
<blockquote><p>Here is a task:<br />
&#8220;3 brothers have to transfer 9 boxes from one place to another.<br />
These boxes weigh 1, 2, 4, 5, 6, 8, 9, 11, 14 kilos.<br />
Every brother takes 3 boxes.<br />
So, how to make a program that would count the most optimal way to take boxes?<br />
The difference between one brother&#8217;s carrying boxes weight and other&#8217;s has to be as small as it can be.&#8221;<br />
Can you just give me an idea or write a code with any programming language you want ( php or pascal if possible? ).<br />
Thanks.
</p></blockquote>
<p>I thought the question was interesting and after spending some time found a solution based on checking all permutations of 9 weight numbers, it appeared to be blazingly fast. I posted an answer, and my answer was accepted by the questioner.<br />
Sure that was not a great question. Also the question was not properly tagged – with ‘php’ and ‘pascal’. I guess ‘php’ tag was a mistake; the questioner got a whole army of moderating idiots attacking his question.<br />
The question received 17 downvotes. The question got the comment `Smells like homework to me`  and the comment got 14 upvotes; the presumption of innocence does not work on SO, and a guy with the editing privileges tagged the question as ‘homework’. Later on it was closed by the moderator called Bill the Lizard with the following resolution:</p>
<blockquote><p>It&#8217;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form.
</p></blockquote>
<p>Strange resolution, isn’t it? The question was answered&#8230;<br />
The strange guy Bill the Lizard did not stop after closing the question. More than 1.5 year (!) after the question was answered he returned to it and deleted both the question and my answer (my answer probably because it contradicted his resolution).<br />
If you think your post was not well accepted on SO, just think of the whole picture.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sergworks.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sergworks.wordpress.com/715/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sergworks.wordpress.com&#038;blog=10209046&#038;post=715&#038;subd=sergworks&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sergworks.wordpress.com/2012/09/26/why-stackoverflow-sucks/feed/</wfw:commentRss>
		<slash:comments>81</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/665c722f073326deabf01c902f0bab45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sergworks</media:title>
		</media:content>
	</item>
	</channel>
</rss>
