{"id":5103,"date":"2013-02-28T07:00:01","date_gmt":"2013-02-28T07:00:01","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/02\/28\/what-does-1j-mean-a-strange-corner-case-of-the-printing-of-special-values\/"},"modified":"2013-02-28T07:00:01","modified_gmt":"2013-02-28T07:00:01","slug":"what-does-1j-mean-a-strange-corner-case-of-the-printing-of-special-values","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130228-01\/?p=5103","title":{"rendered":"What does 1#J mean? A strange corner case of the printing of special values"},"content":{"rendered":"<p><P>\nAs a puzzle, commenter nobugz\nasks,\n&#8220;<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/02\/21\/10395734.aspx#10397012\">What kind of infinity is <CODE>1.#J<\/CODE><\/A>?&#8221;\n<\/P>\n<PRE>\ndouble z = 0;\nprintf(&#8220;%.2f&#8221;, 1\/z);\n<\/PRE>\n<P>\nNow, the division by zero results in IEEE positive infinity,\nwould would normally be printed as\n<CODE>1#INF<\/CODE>.\nBut the catch here is that the print format says\n&#8220;Display at most two places after the decimal point.&#8221;\nBut where is the decimal point in infinity?\n<\/P>\n<P>\nThe Visual C runtime library arbitrarily decided that\nall of the exceptional values have one digit before\nthe decimal (namely, the <CODE>&#8220;1&#8221;<\/CODE>).\nActually, it turns out that this puzzle might be an\nanswer to Random832&#8217;s question,\n&#8220;<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/02\/21\/10395734.aspx#10396209\">What&#8217;s\nthe 1 for<\/A>?&#8221;\nMaybe the 1 is there so that there is a digit at all.\n<\/P>\n<P>\nOkay, so now you have one digit before the decimal\n(the <CODE>&#8220;1&#8221;<\/CODE>),\nand now you need to show at most two places after the decimal.\nBut <CODE>&#8220;#INF&#8221;<\/CODE> is too long to fit into two characters.\nThe C runtime then says,\n&#8220;Well, then I&#8217;d better round it off to two places, then.&#8221;\n<\/P>\n<P>\nThe first character is <CODE>&#8220;#&#8221;<\/CODE>.\nThe second character is <CODE>&#8220;I&#8221;<\/CODE>.\nNow we need to round it.\nThat&#8217;s done by inspecting the third character,\nwhich is <CODE>&#8220;N&#8221;<\/CODE>.\nWe all learned in grade school that you round up if the next\ndigit is 5 or greater.\nAnd it so happens that the code point for\n<CODE>&#8220;N&#8221;<\/CODE> is numerically higher than the code point for\n<CODE>&#8220;5&#8221;<\/CODE>,\nso the value is rounded up by incrementing the previous digit.\nIncrementing <CODE>&#8220;I&#8221;<\/CODE> gives you\n<CODE>&#8220;J&#8221;<\/CODE>.\n<\/P>\n<P>\nThat&#8217;s why printing IEEE positive infinity to two places\ngives you the strange-looking <CODE>&#8220;1#J&#8221;<\/CODE>.\nThe <CODE>J<\/CODE> is an <CODE>I<\/CODE> that got rounded up.\n<\/P>\n<P>\nI doubt this behavior was intended;\nit&#8217;s just a consequence of taking a rounding algorithm intended\nfor digits and applying it to non-digits.\n<\/P>\n<P>\nOf course, in phonetics, rounding an <I>i<\/I>\n<A HREF=\"http:\/\/en.wikipedia.org\/wiki\/Close_front_rounded_vowel\">\nproduces a <I>&uuml;<\/I><\/A>.\nImagine the nerdiness of rounding\n<CODE>&#8220;1#INF&#8221;<\/CODE> to two places and producing\n<CODE>&#8220;1#&Uuml;&#8221;<\/CODE>.\nThat would have been awesome.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a puzzle, commenter nobugz asks, &#8220;What kind of infinity is 1.#J?&#8221; double z = 0; printf(&#8220;%.2f&#8221;, 1\/z); Now, the division by zero results in IEEE positive infinity, would would normally be printed as 1#INF. But the catch here is that the print format says &#8220;Display at most two places after the decimal point.&#8221; But [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-5103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>As a puzzle, commenter nobugz asks, &#8220;What kind of infinity is 1.#J?&#8221; double z = 0; printf(&#8220;%.2f&#8221;, 1\/z); Now, the division by zero results in IEEE positive infinity, would would normally be printed as 1#INF. But the catch here is that the print format says &#8220;Display at most two places after the decimal point.&#8221; But [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=5103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5103\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=5103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}