<p>Most mathematicians agree that:</p>

<blockquote>
  <p>e<sup>πi</sup> + 1 = 0</p>
</blockquote>

<p>However, most floating point implementations disagree. How well can we settle this dispute?</p>

<p>I'm keen to hear about different languages and implementations, and various methods to make the result as close to zero as possible. Be creative!</p>

[{'answer_id': 1054, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 3, 'selected': False, 'text': "<p>Here's a short list of implementations and languages I've tried. It's sorted by closeness to zero:</p>\n\n<ul>\n<li>Scheme: <code>(+ 1 (make-polar 1 (atan 0 -1)))</code>\n\n<ul>\n<li>⇒ <code>0.0+1.2246063538223773e-16i</code> (Chez Scheme, MIT Scheme)</li>\n<li>⇒ <code>0.0+1.22460635382238e-16i</code> (Guile)</li>\n<li>⇒ <code>0.0+1.22464679914735e-16i</code> (Chicken with <code>numbers</code> egg)</li>\n<li>⇒ <code>0.0+1.2246467991473532e-16i</code> (MzScheme, SISC, Gauche, Gambit)</li>\n<li>⇒ <code>0.0+1.2246467991473533e-16i</code> (SCM)</li>\n</ul></li>\n<li>Common Lisp: <code>(1+ (exp (complex 0 pi)))</code>\n\n<ul>\n<li>⇒ <code>#C(0.0L0 -5.0165576136843360246L-20)</code> (CLISP)</li>\n<li>⇒ <code>#C(0.0d0 1.2246063538223773d-16)</code> (CMUCL)</li>\n<li>⇒ <code>#C(0.0d0 1.2246467991473532d-16)</code> (SBCL)</li>\n</ul></li>\n<li>Perl: <code>use Math::Complex; Math::Complex-&gt;emake(1, pi) + 1</code>\n\n<ul>\n<li>⇒ <code>1.22464679914735e-16i</code></li>\n</ul></li>\n<li>Python: <code>from cmath import exp, pi; exp(complex(0, pi)) + 1</code>\n\n<ul>\n<li>⇒ <code>1.2246467991473532e-16j</code> (CPython)</li>\n</ul></li>\n<li>Ruby: <code>require 'complex'; Complex::polar(1, Math::PI) + 1</code>\n\n<ul>\n<li>⇒ <code>Complex(0.0, 1.22464679914735e-16)</code> (MRI)</li>\n<li>⇒ <code>Complex(0.0, 1.2246467991473532e-16)</code> (JRuby)</li>\n</ul></li>\n<li>R: <code>complex(argument = pi) + 1</code>\n\n<ul>\n<li>⇒ <code>0+1.224606353822377e-16i</code></li>\n</ul></li>\n</ul>\n"}, {'answer_id': 1057, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 3, 'selected': False, 'text': '<p>Is it possible to settle this dispute?</p>\n\n<p>My first thought is to look to a symbolic language, like <a href="http://en.wikipedia.org/wiki/Maple_(software)" rel="noreferrer">Maple</a>. I don\'t think that counts as floating point though.</p>\n\n<p>In fact, how does one represent <em>i</em> (or <em>j</em> for the engineers) in a conventional programming language?</p>\n\n<p>Perhaps a better example is sin(π) = 0? (Or have I missed the point again?)</p>\n'}, {'answer_id': 25528, 'author': 'Dana the Sane', 'author_id': 2567, 'author_profile': 'https://Stackoverflow.com/users/2567', 'pm_score': 3, 'selected': False, 'text': "<p>I agree with Ryan, you would need to move to another number representation system. The solution is outside the realm of floating point math because you need pi to represented as an infinitely long decimal so any limited precision scheme just isn't going to work (at least not without employing some kind of fudge-factor to make up the lost precision).</p>\n"}, {'answer_id': 26026, 'author': 'dmckee --- ex-moderator kitten', 'author_id': 2509, 'author_profile': 'https://Stackoverflow.com/users/2509', 'pm_score': 2, 'selected': False, 'text': '<p>@Ryan Fox <blockquote>In fact, how does one represent i (or j for the engineers) in a conventional programming language?</blockquote></p>\n\n<p>Native complex data types are far from unknown. Fortran had it by the mid-sixties, and the OP exhibits a variety of other languages that support them in hist followup. </p>\n\n<p>And complex numbers can be added to other languages as libraries (with operator overloading they even look just like native types in the code).</p>\n\n<p>But unless you provide a special case for this problem, the "non-agreement" is just an expression of imprecise machine arithmetic, no? It\'s like complaining that</p>\n\n<pre><code>float r = 2/3;\nfloat s = 3*r;\nfloat t = s - 2;\n</code></pre>\n\n<p>ends with (t != 0) (At least if you use an dumb enough compiler)...</p>\n'}, {'answer_id': 26038, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 3, 'selected': False, 'text': "<p>Your question seems a little odd to me, as you seem to be suggesting that the Floating Point math is implemented by the language.  That's generally not true, as the FP math is done using a floating point processor in hardware.  But software or hardware, floating point will always be inaccurate.  That's just how floats work.</p>\n\n<p>If you need better precision you need to use a different number representation.  Just like if you're doing integer math on numbers that don't fit in an int or long.  Some languages have libraries for that built in (I know java has BigInteger and BigDecimal), but you'd have to explicitly use those libraries instead of native types, and the performance would be (sometimes significantly) worse than if you used floats.</p>\n"}, {'answer_id': 26055, 'author': 'Chris', 'author_id': 2134, 'author_profile': 'https://Stackoverflow.com/users/2134', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>In fact, how does one represent i (or j for the engineers) in a conventional programming language?</p>\n</blockquote>\n\n<p>In a language that doesn\'t have a native representation, it is usually added using OOP to create a <code>Complex</code> class to represent <code>i</code> and <code>j</code>, with operator overloading to properly deal with operations involving other <code>Complex</code> numbers and or other number primitives native to the language.</p>\n\n<p>Eg: <a href="http://www.math.ksu.edu/~bennett/jomacg/c.html" rel="nofollow noreferrer">Complex.java</a>, <a href="http://msdn.microsoft.com/en-us/library/0352zzhd%28VS.80%29.aspx" rel="nofollow noreferrer">C++ &lt; complex ></a></p>\n'}, {'answer_id': 394225, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 5, 'selected': True, 'text': "<p>It's not that most floating point implementations disagree, it's just that they cannot get the accuracy necessary to get a 100% answer. And the correct answer is that they can't.</p>\n\n<p>PI is an infinite series of digits that nobody has been able to denote by anything other than  a symbolic representation, and e^X is the same, and thus the only way to get to 100% accuracy is to go symbolic.</p>\n"}, {'answer_id': 394290, 'author': 'Brent.Longborough', 'author_id': 9634, 'author_profile': 'https://Stackoverflow.com/users/9634', 'pm_score': 2, 'selected': False, 'text': '<p>Numerical Analysis teaches us that you can\'t rely on the precise value of small differences between large numbers. </p>\n\n<p>This doesn\'t just affect the equation in question here, but can bring instability to everything from solving a near-singular set of simultaneous equations, through finding the zeros of polynomials, to evaluating log(~1) or exp(~0) (I have even seen special functions for evaluating log(x+1) and (exp(x)-1) to get round this).</p>\n\n<p>I would encourage you not to think in terms of zeroing the difference -- you can\'t -- but rather in doing the associated calculations in such a way as to ensure the minimum error.</p>\n\n<p>I\'m sorry, it\'s 43 years since I had this drummed into me at uni, and even if I could remember the references, I\'m sure there\'s better stuff around now. I suggest <strong><em><a href="http://en.wikipedia.org/wiki/Numerical_analysis#The_generation_and_propagation_of_errors" rel="nofollow noreferrer">this</a></em></strong> as a starting point.</p>\n\n<hr>\n\n<p>If that sounds a bit patronising, I apologise. My "Numerical Analysis 101" was part of my Chemistry course, as there wasn\'t much CS in those days. I don\'t really have a feel for the place/importance numerical analysis has in a modern CS course. </p>\n'}, {'answer_id': 1773104, 'author': 'Mark Essel', 'author_id': 51700, 'author_profile': 'https://Stackoverflow.com/users/51700', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s a limitation of our current floating point computational architectures. Floating point arithmetic is only an approximation of numeric poles like e or pi (or anything beyond the precision your bits allow). I really enjoy these numbers because they defy classification, and appear to have greater entropy(?) than even primes, which are a canonical series. A ratio defy\'s numerical representation, sometimes simple things like that can blow a person\'s mind (I love it).</p>\n\n<p>Luckily entire languages and libraries can be dedicated to precision trigonometric functions by using notational concepts (similar to those described by <a href="https://stackoverflow.com/users/267/lasse-v-karlsen">Lasse V. Karlsen</a> ). </p>\n\n<p>Consider a library/language that describes concepts like e and pi in a form that a machine can understand. Does a machine have any notion of what a perfect circle is? Probably not, but we can create an object - circle that satisfies all the known features we attribute to it (constant radius, relationship of radius to circumference is 2*pi*r = C). An object like pi is only described by the aforementioned ratio. r &amp; C can be numeric objects described by whatever precision you want to give them. e can be defined "as the e is the unique real number such that the value of the derivative (slope of the tangent line) of the function f(x) = ex at the point x = 0 is exactly 1" from <a href="http://en.wikipedia.org/wiki/E_(mathematical_constant)" rel="nofollow noreferrer">wikipedia</a>.</p>\n\n<p>Fun question.</p>\n'}, {'answer_id': 43816298, 'author': 'developer_hatch', 'author_id': 7128034, 'author_profile': 'https://Stackoverflow.com/users/7128034', 'pm_score': 2, 'selected': False, 'text': '<p>I had looooong coffee chats with my best pal talking about Irrational numbers and the diference between other numbers. Well, both of us agree in this different point of view:</p>\n\n<p>Irrational numbers are relations, as functions, in a way, what way? Well, think about "if you want a perfect circle, give me a perfect pi", but circles are diferent to the other figures (4 sides, 5, 6... 100, 200) but... How many more sides do you have, more like a circle it look like. If you followed me so far, connecting all this ideas here is the pi formula:\n<a href="https://i.stack.imgur.com/uM2PX.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uM2PX.gif" alt="enter image description here"></a></p>\n\n<p>So, pi is a function, but one that never ends! because of the ∞ parameter, but I like to think that you can have "instance" of pi, if you change the ∞ parameter for a very big Int, you will have a very big pi instance.</p>\n\n<p>Same with e, give me a huge parameter, I will give you a huge e.</p>\n\n<p>Putting all the ideas together:</p>\n\n<p>As we have memory limitations, the language and libs provide to us huge instance of irrational numbers, in this case, pi and e, as final result, you will have long aproach to get 0, like the examples provided by @Chris Jester-Young</p>\n'}]
['https://Stackoverflow.com/questions/1053', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13/']