<story><title>Intel Underestimates Error Bounds by 1.3 Quintillion</title><url>http://randomascii.wordpress.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/</url></story><parent_chain><item><author>sillysaurus3</author><text>Is it even valid for a compiler to transform sin(constant) into a hardcoded constant embedded into the executable?&lt;p&gt;I guess since sin(x) is computed by an FPU in hardware, then it&amp;#x27;s inherently &amp;quot;dynamically linked.&amp;quot;  But in that case, why is it valid for a compiler to transform sin(constant) into a hardcoded result?  Is there some standard somewhere which says that the implementation of sin&amp;#x2F;cos&amp;#x2F;etc can&amp;#x27;t ever change, and therefore it&amp;#x27;s valid for the compiler to do that?  Or do compilers just make an assumption that the implementation of sin(x) won&amp;#x27;t ever change?</text></item><item><author>Guvante</author><text>&amp;gt; One scenario I can imagine an app breaking is if someone executed fsin(x), pasted the result into their code, and then tested some other float against it using the == operator.&lt;p&gt;While this sounds grandious, thanks to aggressive inlining this could be the case even if you wrote in your logic:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    sin(x) == sin(constant)
&lt;/code&gt;&lt;/pre&gt;
Or even the following depending on your choice of delta.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    abs(sin(x) - sin(constant) &amp;lt; delta&lt;/code&gt;&lt;/pre&gt;</text></item><item><author>sillysaurus3</author><text>It&amp;#x27;s not possible to implement a more precise fsin() without breaking apps?&lt;p&gt;One scenario I can imagine an app breaking is if someone executed fsin(x), pasted the result into their code, and then tested some other float against it using the == operator.&lt;p&gt;I suppose a more common breakage scenario would be if someone did something like floor(x + fsin(x)), since that&amp;#x27;d very slightly change where the floor &amp;quot;triggers&amp;quot; with respect to the input.  But how could that cause an app to break?  Every scenario I can think of comes back to someone using == to test floats, which everyone knows is a no-no for dynamic results, such as the output of a function like fsin().&lt;p&gt;I guess a programmer wouldn&amp;#x27;t really expect the result of &amp;quot;x + fsin(x)&amp;quot; to ever change for a certain value of x, so maybe they executed it with that certain value, stored the result, and did an equality comparison later, which would always work unless the implementation of fsin() changed, and the thought process went something like, &amp;quot;This should be reliable because realistically the implementation of fsin() won&amp;#x27;t change.&amp;quot;&lt;p&gt;Can anyone think of some obvious way that making fsin() more accurate could cause serious breakage in an app that isn&amp;#x27;t testing floats with the equality operator?</text></item><item><author>CurtHagenlocher</author><text>Of possible interest, I researched a related issue once and here&amp;#x27;s what I found:&lt;p&gt;The particular approximation used by the FPU instructions fsin and fcos is valid in the domain [-π &amp;#x2F;4, π &amp;#x2F;4]. Inputs outside this domain are reduced by taking the remainder when dividing by π &amp;#x2F;4 and then using trigonometric identities to fix up values in the other three quadrants (ie sin(x) = sin(π &amp;#x2F;2 – x) for x in [π &amp;#x2F;4, π &amp;#x2F;2].&lt;p&gt;The Intel FPU is “off” because it uses an approximation for π &amp;#x2F;4 when performing the reduction. In the mid 90s, some engineers at AMD figured out how to do a more precise reduction and implemented this reduction in the K5 FPU. AMD went back to being bit-for-bit compatible with the x87 behavior in the very next generation of their CPUs, presumably because it either broke or appeared to break too many applications.</text></item></parent_chain><comment><author>zAy0LfpBZLC8mAC</author><text>Your mistake is in assuming that the language C and its standard library are somehow defined in terms of some hardware, and that there somehow is a mapping between C functions and operators and CPU instructions - there isn&amp;#x27;t. The standard specifies for what input values an operation is defined and which conditions have to be met by the result, nowhere does it say how an implementation is supposed to compute the result, that&amp;#x27;s for the compiler to decide - as far as the standard is concerned, the compiler may compile all arithmetic to only ANDs and NOTs, though most practical compilers tend to try and choose instructions that give the best performance for a given computation, or just compute the result at compile time where possible.</text></comment>
<story><title>Intel Underestimates Error Bounds by 1.3 Quintillion</title><url>http://randomascii.wordpress.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/</url></story><parent_chain><item><author>sillysaurus3</author><text>Is it even valid for a compiler to transform sin(constant) into a hardcoded constant embedded into the executable?&lt;p&gt;I guess since sin(x) is computed by an FPU in hardware, then it&amp;#x27;s inherently &amp;quot;dynamically linked.&amp;quot;  But in that case, why is it valid for a compiler to transform sin(constant) into a hardcoded result?  Is there some standard somewhere which says that the implementation of sin&amp;#x2F;cos&amp;#x2F;etc can&amp;#x27;t ever change, and therefore it&amp;#x27;s valid for the compiler to do that?  Or do compilers just make an assumption that the implementation of sin(x) won&amp;#x27;t ever change?</text></item><item><author>Guvante</author><text>&amp;gt; One scenario I can imagine an app breaking is if someone executed fsin(x), pasted the result into their code, and then tested some other float against it using the == operator.&lt;p&gt;While this sounds grandious, thanks to aggressive inlining this could be the case even if you wrote in your logic:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    sin(x) == sin(constant)
&lt;/code&gt;&lt;/pre&gt;
Or even the following depending on your choice of delta.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    abs(sin(x) - sin(constant) &amp;lt; delta&lt;/code&gt;&lt;/pre&gt;</text></item><item><author>sillysaurus3</author><text>It&amp;#x27;s not possible to implement a more precise fsin() without breaking apps?&lt;p&gt;One scenario I can imagine an app breaking is if someone executed fsin(x), pasted the result into their code, and then tested some other float against it using the == operator.&lt;p&gt;I suppose a more common breakage scenario would be if someone did something like floor(x + fsin(x)), since that&amp;#x27;d very slightly change where the floor &amp;quot;triggers&amp;quot; with respect to the input.  But how could that cause an app to break?  Every scenario I can think of comes back to someone using == to test floats, which everyone knows is a no-no for dynamic results, such as the output of a function like fsin().&lt;p&gt;I guess a programmer wouldn&amp;#x27;t really expect the result of &amp;quot;x + fsin(x)&amp;quot; to ever change for a certain value of x, so maybe they executed it with that certain value, stored the result, and did an equality comparison later, which would always work unless the implementation of fsin() changed, and the thought process went something like, &amp;quot;This should be reliable because realistically the implementation of fsin() won&amp;#x27;t change.&amp;quot;&lt;p&gt;Can anyone think of some obvious way that making fsin() more accurate could cause serious breakage in an app that isn&amp;#x27;t testing floats with the equality operator?</text></item><item><author>CurtHagenlocher</author><text>Of possible interest, I researched a related issue once and here&amp;#x27;s what I found:&lt;p&gt;The particular approximation used by the FPU instructions fsin and fcos is valid in the domain [-π &amp;#x2F;4, π &amp;#x2F;4]. Inputs outside this domain are reduced by taking the remainder when dividing by π &amp;#x2F;4 and then using trigonometric identities to fix up values in the other three quadrants (ie sin(x) = sin(π &amp;#x2F;2 – x) for x in [π &amp;#x2F;4, π &amp;#x2F;2].&lt;p&gt;The Intel FPU is “off” because it uses an approximation for π &amp;#x2F;4 when performing the reduction. In the mid 90s, some engineers at AMD figured out how to do a more precise reduction and implemented this reduction in the K5 FPU. AMD went back to being bit-for-bit compatible with the x87 behavior in the very next generation of their CPUs, presumably because it either broke or appeared to break too many applications.</text></item></parent_chain><comment><author>DannyBee</author><text>&amp;quot;but if it&amp;#x27;s dynamic, is it even valid for a compiler to transform sin(constant) into a hardcoded constant embedded into the executable?&amp;quot;&lt;p&gt;Yes.&lt;p&gt;I honestly don&amp;#x27;t remember all the rules here, but it does in fact do so, legally.</text></comment>