4

Somehow this way to calculate a string returns 8 instead of an exception or something else... every other division by 0 produces the same result. Why doesn't it throw an error and why is the result always 8?

System.Console.WriteLine(new DataTable().Compute("2/0", null));
TheMadGuy
  • 97
  • 6
  • 4
    "8" in this instance is trying to show you an infinity symbol rotated though 90 degrees. Weird, indeed, but that's what it's doing. – Matthew Watson May 28 '18 at 11:26
  • see this https://stackoverflow.com/questions/40907417/why-is-infinity-printed-as-8-in-the-windows-10-console – Lucifer May 28 '18 at 11:28
  • The problem here is the code page used by your console (application), which maps the infinity symbol, ∞, to the digit 8. – Lasse V. Karlsen May 28 '18 at 11:30
  • Actually I just realised that the duplicate question doesn't say why it doesn't throw an error. That is a duplicate of [this question](https://stackoverflow.com/questions/48929855/why-doesnt-dividing-by-zero-with-doubles-throw-an-exception) - essentially, the IEEE spec for floating point calculations stipulates that `Double.PositiveInfinity` or `Double.NegativeInfinity` is returned instead. – Matthew Watson May 28 '18 at 11:31
  • @MatthewWatson You could have [added it](https://stackoverflow.com/questions/originals/50565276/edit) to the duplicate list. – Patrick Hofman May 28 '18 at 11:32
  • 1
    @PatrickHofman Thanks - I see you've done it for me. :) – Matthew Watson May 28 '18 at 11:33
  • For me, it shows just "Infinity". – L_J May 28 '18 at 11:34
  • @MatthewWatson Was right. Pretty weird yeah, but thanks – TheMadGuy May 28 '18 at 11:34
  • @L_J By default it shows `8` for me - but if I add `Console.OutputEncoding = System.Text.Encoding.UTF8;` before printing, it comes out as `∞` - however, in neither case does it print "Infinity" when I try it... – Matthew Watson May 28 '18 at 11:37
  • Ok so this is dependent on culture - see https://dotnetfiddle.net/QYhXMu (not written by me!) – Matthew Watson May 28 '18 at 11:39

0 Answers0