{"id":499,"date":"2013-06-18T12:21:41","date_gmt":"2013-06-18T19:21:41","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=499"},"modified":"2013-07-26T14:50:16","modified_gmt":"2013-07-26T21:50:16","slug":"ctci-ch-5-2","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=499","title":{"rendered":"CTCI: Ch. 5-2"},"content":{"rendered":"<p>\n\t<strong>Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with less than 32 characters, print &ldquo;ERROR.&rdquo;<\/strong><br \/>\n\t&nbsp;\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<pre class=\"brush:cpp;\">\r\n\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nstring printFloatingBinary(double num)\r\n{\r\n    if(num &gt;=1 || num &lt;= 0)\r\n        return &quot;ERROR. (RANGE ERROR)&quot;;\r\n    \r\n    string floatingBinary = &quot;&quot;;\r\n    floatingBinary.append(&quot;0.&quot;);\r\n    while(num &gt;= 0)\r\n    {\r\n        if(floatingBinary.length() &gt;= 32)\r\n        {\r\n            floatingBinary = &quot;ERROR. (&quot; + floatingBinary + &quot;)&quot;;\r\n            break;\r\n        }\r\n        int intPart;\r\n        double floatingPart;\r\n        \r\n        intPart = num * 2;\r\n        floatingPart = (num * 2) - intPart;\r\n        floatingBinary += (char)intPart + &#39;0&#39;;\r\n        \r\n        num = floatingPart;\r\n        \r\n    }\r\n    return floatingBinary;\r\n}\r\n\r\nint main(int argc, const char * argv[])\r\n{\r\n    \r\n    cout&lt;&lt;printFloatingBinary(0.12566)&lt;&lt;endl;\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with less than 32 characters, print &ldquo;ERROR.&rdquo; &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[23,6],"tags":[],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-chapter-5-bit-manipulation","category-cracking-the-coding-interview"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=499"}],"version-history":[{"count":6,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":520,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions\/520"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}