{"id":478,"date":"2008-05-31T16:00:22","date_gmt":"2008-05-31T23:00:22","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=478"},"modified":"2011-08-07T19:43:59","modified_gmt":"2011-08-08T02:43:59","slug":"compile-time-symbolic-computations","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/compile-time-symbolic-computations\/","title":{"rendered":"Compile-time symbolic computations"},"content":{"rendered":"<p>A compiler ought to be able to perform compuations at compile time. Unlike the generated code, these computations don&#8217;t have to be blazingly fast (since they only happen at compile time), and don&#8217;t have to conform to any particular machine&#8217;s architecture (since the language should be the same for different architectures anyway) so some nice things can be done.<\/p>\n<p>Arbirary-precision integer arithmetic is a nice easy one and one that is done by many scripting languages already. This allows you to write expressions such as:<\/p>\n<pre lang=\"c\">const int a = 1000000000000\/1000;<\/pre>\n<p>and have a initialized to one billion even on 32-bit machines.<\/p>\n<p>Rational arithmetic is also easy and useful. This allows one to write:<\/p>\n<pre lang=\"c\">const int a = (1000\/3)*3;<\/pre>\n<p>and have a initialized to exactly <code>1000<\/code> even on machines lacking any sort of floating point facility.<\/p>\n<p>Then there are closed forms such as:<\/p>\n<pre lang=\"c\">const int a = sqrt(1000)^2;  \/\/ a==1000, not 961<\/pre>\n<p>One could even make a compiler understand <code>pi<\/code> as a symbolic constant of type &#8220;<code>Real<\/code>&#8221; and evaluate it to the appropriate number of significant figures for the expression and type that is being initialized. So:<\/p>\n<pre lang=\"c\">const int a = truncate_cast<int>(pi*10000);<\/pre>\n<p>would initialize <code>a<\/code> to <code>31416<\/code>.<\/p>\n<p>Once these things are in place, a compiler can perform quite sophisticated mathematical transformations to allow programmers to write what they really mean and still obtain optimal code. Even to the level of Maple\/Mathematica if the compiler writers are sophisticated enough.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A compiler ought to be able to perform compuations at compile time. Unlike the generated code, these computations don&#8217;t have to be blazingly fast (since they only happen at compile time), and don&#8217;t have to conform to any particular machine&#8217;s architecture (since the language should be the same for different architectures anyway) so some nice [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-language"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":1,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":1317,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions\/1317"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}