<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-17668659</id><updated>2011-04-21T14:07:29.684-07:00</updated><title type='text'>The Joy Of Programming</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-17668659.post-117214181701631634</id><published>2007-02-22T02:56:00.000-08:00</published><updated>2007-02-22T02:56:57.016-08:00</updated><title type='text'>The Joy Of Programming</title><content type='html'>&lt;a href="http://joyofprogramming.blogspot.com/"&gt;The Joy Of Programming&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In Feb issue of Linux for You (&lt;a href="http://www.linuxforu.com/index.php"&gt;http://www.linuxforu.com/index.php&lt;/a&gt;) magazine Joy Of Programming column, I&lt;br /&gt;mentioned a floating point problem:&lt;br /&gt;"Since the floating point arithmetic is not accurate, the operations can result in loss of precision and rounding errors.&lt;br /&gt;In the loop, when the value of f reaches 16777216.0, adding one doesn’t increment the value of f by one, but gets&lt;br /&gt;rounded off.&lt;br /&gt;&lt;br /&gt;You can verify it with the following code segment:&lt;br /&gt;&lt;br /&gt;float f = 16777216.0;&lt;br /&gt;assert(f == (f + 1));&lt;br /&gt;&lt;br /&gt;This doesn’t fail with an assertion failure and works fine!&lt;br /&gt;&lt;br /&gt;"An astute reader observed that, if we change f + 1 to f + 1.0, it does fail!&lt;br /&gt;The reason is that 1.0 is a double value. f + 1.0 becomes a double expression, which results in the&lt;br /&gt;value 16777217.0, and hence the assertion fails.&lt;br /&gt;&lt;br /&gt;Note that the code here assumes that the compiler is in C89 mode. In K&amp;R mode, all floating&lt;br /&gt;point operations are done in double, so the assertion will fail!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-117214181701631634?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/117214181701631634/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=117214181701631634' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/117214181701631634'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/117214181701631634'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2007/02/joy-of-programming.html' title='The Joy Of Programming'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-117214167061022024</id><published>2007-02-22T02:53:00.000-08:00</published><updated>2007-02-22T02:54:30.626-08:00</updated><title type='text'></title><content type='html'>What can simple software problems do? It can cause even a rocket mission to get aborted, as happened with Ariane5 accident:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://sunnyday.mit.edu/accidents/Ariane5accidentreport.html"&gt;http://sunnyday.mit.edu/accidents/Ariane5accidentreport.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;"The internal SRI software exception was caused during execution of a data conversion from 64-bit floating point to 16-bit signed integer value. The floating point number which was converted had a value greater than what could be represented by a 16-bit signed integer. This resulted in an Operand Error. The data conversion instructions (in Ada code) were not protected from causing an Operand Error, although other conversions of comparable variables in the same place in the code were protected.&lt;br /&gt;&lt;br /&gt;The error occurred in a part of the software that only performs alignment of the strap-down inertial platform. This software module computes meaningful results only before lift-off. As soon as the launcher lifts off, this function serves no purpose."&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-117214167061022024?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/117214167061022024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=117214167061022024' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/117214167061022024'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/117214167061022024'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2007/02/what-can-simple-software-problems-do.html' title=''/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-114138696465596835</id><published>2006-03-03T03:56:00.000-08:00</published><updated>2006-03-03T03:56:04.690-08:00</updated><title type='text'>The Conceptual Simplicity of Pascal</title><content type='html'>Conceptual simplicity is something very important in the modern, complex, maddening world . &lt;br /&gt;I thought about this when I just got a chance to take a look at the pascal sources:&lt;br /&gt;&lt;br /&gt;&lt;a title="http://invent.ucsd.edu/technology/cases/1995-prior/SD1991-807.htm" href="http://invent.ucsd.edu/technology/cases/1995-prior/SD1991-807.htm" target="_blank"&gt;http://invent.ucsd.edu/technology/cases/1995-prior/SD1991-807.htm&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Its so compact and clear considering to the contributions and advancements it made in&lt;br /&gt;programming languages/compilers!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-114138696465596835?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/114138696465596835/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=114138696465596835' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/114138696465596835'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/114138696465596835'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2006/03/conceptual-simplicity-of-pascal.html' title='The Conceptual Simplicity of Pascal'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-113774192356394390</id><published>2006-01-19T23:25:00.000-08:00</published><updated>2006-01-19T23:25:23.613-08:00</updated><title type='text'>Decimal Arithmetic</title><content type='html'>Lack of support for decimal arithmetic is a painpoint for commercial software implementations.Put simply, the existing ieee 754 standard for floating points isn't much suitable for representing realworld decimal values (in other words, using binary floats with base 2 is not preferable, a float representation with base 10 is better).&lt;br /&gt;&lt;br /&gt;Languages currently have not-so-good alternatives. C# supports the decimal type as a primitive type (borrowed from SQL). This type occupies 128 bytes and provides higher precision. It can be valuable in monetary calculations where it is necessary to represent exact values. For similar functionality, C++ provides the long double type (but don’t be mislead- implementations provide its supportthro following IEEE extended precision fp standard). Java provides the java.math.BigDecimal class- that’s much better; but since no H/W support is there, and all values calculated, the application will perform terribly slow if BigDecimal is used extensively. There are a few proposalswith IEEE for decimal arithmetic, and if its approved, things will be much better.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-113774192356394390?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/113774192356394390/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=113774192356394390' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113774192356394390'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113774192356394390'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2006/01/decimal-arithmetic.html' title='Decimal Arithmetic'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-113716587900266061</id><published>2006-01-13T07:24:00.000-08:00</published><updated>2006-01-13T07:24:39.036-08:00</updated><title type='text'>new meaning of static in C99</title><content type='html'>C99 provides a new meaning for static, consider: &lt;br /&gt;&lt;br /&gt;void bar (int b[static 100]);&lt;br /&gt;&lt;br /&gt;This is to ensure that bar will take an array argument which is promised to have size 100 or more. This is an optimization hint for the compiler (those that do vectorization).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-113716587900266061?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/113716587900266061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=113716587900266061' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113716587900266061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113716587900266061'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2006/01/new-meaning-of-static-in-c99.html' title='new meaning of static in C99'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-113402344793191313</id><published>2005-12-07T22:30:00.000-08:00</published><updated>2005-12-07T22:30:47.953-08:00</updated><title type='text'>Standard C++ TR1</title><content type='html'>The Standard C++ library has got a new TR1 (Technical Report 1). There are major additions to standard library with this (though TR1 is not part of the standard yet). Major components added include shared_ptr (and its relative weak_ptr), unordered associative containers, c99 compatibility features, tuples etc. Here is a small Q &amp; A on questions you might have on TR1. &lt;br /&gt;&lt;br /&gt;Q: A weak_ptr is for avoiding the shared_ptr circular reference. How can we ensure that the resource isn't freed when accessing a resource through a weak_ptr. &lt;br /&gt;A: When we use a weak_ptr to access a resource when use_count might become zero, we can use "lock" function. &lt;br /&gt;weak_ptr::expired() is the function that can be used to check if the memory resource has got freed or not. &lt;br /&gt;&lt;br /&gt;Q: How to get a legacy C array out of a tr1::array? &lt;br /&gt;A: Use array::data() function to get the equivalent C-style array from a tr1::array.  &lt;br /&gt;&lt;br /&gt;Q: Since using tr1::array is recommended over using legacy C style array, will it result in more type_info objects added to the object file and that the object file will become bigger? &lt;br /&gt;A: type_info objects are per-class datastructures, so its unlikely that it will result in any significant change. Moreover, tr1::array is not polymorphic, so a compiler need not put type_info objects in the object file. &lt;br /&gt;&lt;br /&gt;Q: What are the compiler changes that will be needed to accommodate TR1? &lt;br /&gt;A: The C99 changes compatibility addition to TR1 for math libraries is one. If there are any other changes needed are not clear yet, but its unlikely that any changes will be needed. &lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-113402344793191313?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/113402344793191313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=113402344793191313' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113402344793191313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113402344793191313'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/12/standard-c-tr1.html' title='Standard C++ TR1'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-113273622790637011</id><published>2005-11-23T00:57:00.000-08:00</published><updated>2005-11-23T00:57:07.926-08:00</updated><title type='text'>The Power of C++ Library</title><content type='html'>Sometimes, I am just amazed by the power of the C++ standard library. Check the following simple &lt;br /&gt;code: &lt;br /&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;string&gt;&lt;br /&gt;#include &lt;cstdio&gt;&lt;br /&gt;#include &lt;sstream&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    ostringstream out(ios_base::in | ios_base::out);&lt;br /&gt;    ostream mycout(cin.rdbuf());&lt;br /&gt;    out &lt;&lt; mycout.rdbuf();&lt;br /&gt;    printf("%s\n", out.str().c_str());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;It reads the input, copies the output to the temporary ostream object. Then it puts whatever in the ostream to a string stream&lt;br /&gt;and then, uses printf to get the underlying C string and put it in stdout. Another variation of this program is this: &lt;br /&gt;&lt;br /&gt;#include &lt;iostream&gt; &lt;br /&gt;using namespace std; &lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    cout &lt;&lt; cin.rdbuf();  &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;This copies the standard input to stdout. The performance is also very good, comparable to the similar program written&lt;br /&gt;with low-level C routines! &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-113273622790637011?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/113273622790637011/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=113273622790637011' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113273622790637011'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113273622790637011'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/11/power-of-c-library.html' title='The Power of C++ Library'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-113013166948284102</id><published>2005-10-23T22:27:00.000-07:00</published><updated>2005-10-23T22:27:49.496-07:00</updated><title type='text'>Programming language ratings</title><content type='html'>I (like many others) don't believe in rating programming languages, but it is still intructive/interesting to see whats happening in the programming language community. Recently, I saw one such rating in http://www.tiobe.com/tpci.htm.  &lt;br /&gt;&lt;br /&gt;Interesting to see C rated much higher than C++.  &lt;br /&gt;&lt;br /&gt;"October Headline: ColdFusion enters Top 20 &lt;br /&gt;&lt;br /&gt;The TIOBE Programming Community index gives an indication of the popularity of programming languages. The index is updated once a month. The ratings are based on the world-wide availability of skilled engineers, courses and third party vendors. The popular search engines Google, MSN, and Yahoo! are used to calculate the ratings. Observe that the TPC index is not about the best programming language or the language in which most lines of code have been written.&lt;br /&gt;&lt;br /&gt;The index can be used to check whether your programming skills are still up to date or to make a strategic decision about what programming language should be adopted when starting to build a new software system...." &lt;br /&gt;&lt;br /&gt;Position (Position) Programming Language Ratings (Ratings) Status &lt;br /&gt;&lt;br /&gt;1  			  Java 			21.871% +4.82%   A &lt;br /&gt;2  			  C 			18.773% +0.60%   A &lt;br /&gt;3  			  C++ 			11.820% -3.75%   A &lt;br /&gt;. &lt;br /&gt;7  		               C#  			3.462%  +1.82%   A &lt;br /&gt;.&lt;br /&gt;14  			  COBOL 			0.859%  +0.15%   A &lt;br /&gt;15  			  Lisp/Scheme 		0.665%  +0.23%   A- &lt;br /&gt;16  			  Fortran 			0.640%  +0.28%   B &lt;br /&gt;17  			  Ada 			0.590%  +0.23%   B &lt;br /&gt;18  			  Pascal 			0.567%  +0.10%   B &lt;br /&gt;. &lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-113013166948284102?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/113013166948284102/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=113013166948284102' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113013166948284102'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/113013166948284102'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/10/programming-language-ratings.html' title='Programming language ratings'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-112971399242576807</id><published>2005-10-19T02:26:00.000-07:00</published><updated>2005-10-19T02:26:34.693-07:00</updated><title type='text'>Floating Point Fractional Constants in C99</title><content type='html'>Today, when looking into the C99 standard for clarifying a doubt I had, I encountered a &lt;br /&gt;strange construct in C99 hexadecimal constants where "P" is allowed to be present. Later, &lt;br /&gt;with exprimentation, I found that it is for supporting floating point "fractional" constants.  &lt;br /&gt;&lt;br /&gt;For example,   0x1P1 means 1 * 2 power 1 =&gt; 2 &lt;br /&gt;	       0x1P-1 means 1 * 2 power -1 =&gt; 0.5 &lt;br /&gt;&lt;br /&gt;Note that this construct is only allowed for hex-constants and the power is 2 not 10 or 16.&lt;br /&gt;With this, just like the e/E notation for exponents, fractional values can be conveniently expressed&lt;br /&gt;with P format. For example to represent a fraction, say, EPSILON, I can use P notation: &lt;br /&gt;&lt;br /&gt;#define EPSILON 0X1P-23F &lt;br /&gt;&lt;br /&gt;instead of: &lt;br /&gt;&lt;br /&gt;#define EPSILON 1.19209e-07 &lt;br /&gt;&lt;br /&gt;But I don't know what P means (may be "Power of"?).  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-112971399242576807?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/112971399242576807/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=112971399242576807' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112971399242576807'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112971399242576807'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/10/floating-point-fractional-constants-in.html' title='Floating Point Fractional Constants in C99'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-112922219475480441</id><published>2005-10-13T09:49:00.001-07:00</published><updated>2005-10-13T09:49:54.756-07:00</updated><title type='text'>Thunks in C++ </title><content type='html'>&lt;strong&gt;Thunks in C++ &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Advanced C++ programmers might know about "thunks". Thunks are compiler generated very small functions that typically do some pointer or address adjutment. Why is it needed? Consider&lt;br /&gt;&lt;br /&gt;delete [] arr;&lt;br /&gt;&lt;br /&gt;where arr is points to an array of 10 elements from dynamically allocated memory. When you do delete, how does the size of array obtained and correct number of destructors are called? This can be implented by a compiler by providing thunks: a small function that will retrieve the number of elements in a dynamically allocated array from memory and give it to the delete so that it will call the destructors that many number of times. Usually, thunks are used in vtables where a this pointer manipulation for doing a virtual function call for complex cases in multiple virtual inheritance.&lt;br /&gt;&lt;br /&gt;This is all fine, but I came across an intersting fact that "thunks" were originally introduced in compiler implementations during Algol days. Algol has two argument passing mechanisms: pass by value and pass by name. Pass by name is problematic, and works just like macros in C. With pass by value, the name is bound at runtime, an a replacement is done. That sounds like magic, how can it be done? &lt;br /&gt;P. Z. Ingerman is believed to have invented thunks in 1961 as a mechanism of implementing pass by name to bind actual parameters to formal definitions. Pass by name didn't live the test of time and later programming languages didn't use it, but the term "thunk" stuck. Interesting indeed!&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-112922219475480441?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/112922219475480441/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=112922219475480441' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112922219475480441'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112922219475480441'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/10/thunks-in-c_13.html' title='Thunks in C++ '/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-112922219415810366</id><published>2005-10-13T09:49:00.000-07:00</published><updated>2005-10-13T09:49:54.180-07:00</updated><title type='text'>Thunks in C++ </title><content type='html'>&lt;strong&gt;Thunks in C++ &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Advanced C++ programmers might know about "thunks". Thunks are compiler generated very small functions that typically do some pointer or address adjutment. Why is it needed? Consider&lt;br /&gt;&lt;br /&gt;delete [] arr;&lt;br /&gt;&lt;br /&gt;where arr is points to an array of 10 elements from dynamically allocated memory. When you do delete, how does the size of array obtained and correct number of destructors are called? This can be implented by a compiler by providing thunks: a small function that will retrieve the number of elements in a dynamically allocated array from memory and give it to the delete so that it will call the destructors that many number of times. Usually, thunks are used in vtables where a this pointer manipulation for doing a virtual function call for complex cases in multiple virtual inheritance.&lt;br /&gt;&lt;br /&gt;This is all fine, but I came across an intersting fact that "thunks" were originally introduced in compiler implementations during Algol days. Algol has two argument passing mechanisms: pass by value and pass by name. Pass by name is problematic, and works just like macros in C. With pass by value, the name is bound at runtime, an a replacement is done. That sounds like magic, how can it be done? &lt;br /&gt;P. Z. Ingerman is believed to have invented thunks in 1961 as a mechanism of implementing pass by name to bind actual parameters to formal definitions. Pass by name didn't live the test of time and later programming languages didn't use it, but the term "thunk" stuck. Interesting indeed!&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-112922219415810366?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/112922219415810366/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=112922219415810366' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112922219415810366'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112922219415810366'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/10/thunks-in-c.html' title='Thunks in C++ '/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-17668659.post-112892352014354810</id><published>2005-10-09T22:48:00.000-07:00</published><updated>2005-10-09T22:52:00.146-07:00</updated><title type='text'>The Joy of Programming</title><content type='html'>&lt;span style="font-size:180%;color:#ff6666;"&gt;Brahmagupta's Riddle &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I recently came across ancient Indian mathematician Brahmagupta's quote "A person who can, within a year, solve x^2 - 92y^2 = 1 is a mathematician." (x square - 92 multiplied by y square = 1).&lt;br /&gt;&lt;br /&gt;Since I am no mathematician but a programmer, I tried writing a program to find out the values for x and y.&lt;br /&gt;&lt;br /&gt;x^2 - 92y^2 = 1&lt;br /&gt;=&gt; x^2 = 1 + 92y^2&lt;br /&gt;=&gt; x = rootof (1 + 92y^2)&lt;br /&gt;&lt;br /&gt;So, my program can vary an integer value y and do floating point computation for the formula rootof (1 + 92y^2), and check if the result is an integer.&lt;br /&gt;&lt;br /&gt;But later, I thought, its better to keep it as integer and compute it using two for loops to avoid floating point computation:&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;limits.h&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int x, y;&lt;br /&gt;    for(x = 1; x &lt;= SHRT_MAX; x++) {&lt;br /&gt;         for(y = 1; y &lt;= SHRT_MAX; y++) {&lt;br /&gt;               if ( (x * x) == (1 + 92 * y * y)) {&lt;br /&gt;                     printf("x = %d, y = %d", x, y);&lt;br /&gt;                     break;&lt;br /&gt;                 }&lt;br /&gt;          }&lt;br /&gt;     }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;And for my surprise, it found the answer in a few seconds: it prints x = 1151, y = 120&lt;br /&gt;1151 * 1151 == 1 + 92 * 120 * 120&lt;br /&gt;=&gt; 1324801 == 1 + 92 * 14400&lt;br /&gt;=&gt; 1324801 == 1 + 1324800&lt;br /&gt;=&gt; 1324801 == 1324801&lt;br /&gt;&lt;br /&gt;Well, even now I don't understand the complexity of the original problem. Can somebody who is good in mathematics explain it to me?&lt;br /&gt;&lt;br /&gt;I was also wondering how this program can be made more efficient.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/17668659-112892352014354810?l=joyofprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://joyofprogramming.blogspot.com/feeds/112892352014354810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=17668659&amp;postID=112892352014354810' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112892352014354810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/17668659/posts/default/112892352014354810'/><link rel='alternate' type='text/html' href='http://joyofprogramming.blogspot.com/2005/10/joy-of-programming.html' title='The Joy of Programming'/><author><name>Ganesh</name><uri>http://www.blogger.com/profile/04995399629209382886</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
