0

In Java I run:

System.out.println(Math.log(249.0/251.0));

Output: -0.008000042667076265

In C# I run: <- fixed

Math.Log (x/y); \\where x, y are almost assuredly 249.0 and 251.0 respectively

Output: -0.175281838 (printed out later in the program)

Google claims:

Log(249.0/251.0)

Output: -0.00347437439

And MacOS claims about the same thing (the first difference between google and Snow Leopard is at about 10^-8, which is negligible.

Is there any reason that these results should all vary so widely or am I missing something very obvious? (I did check that java and C# both use base e). Even mildly different values of e don't seem to account for such a big difference. Any suggestions?

EDIT:

Verifying on Wolfram Alpha seems to suggest that Java is right (or that Wolfram Alpha uses Java Math for logarithms...) and that my C# program doesn't have the right input, but I am disinclined to believe this because taking (e^(google result) - 249/251) gives me an error of 0.0044 which is pretty big in my opinion, suggesting that there is a different problem at hand...

Ritwik Bose
  • 5,889
  • 8
  • 32
  • 43
  • I assume the C# example should read Math.Log(x / y)? Otherwise you get the logarithm of x with a base of y (instead of the logarithm of x/y with a base of e). – Ronald Wildenberg Jul 02 '10 at 05:40
  • 1
    Google returns a base 10 log so it should be 10^(google result), not e^(google result). Look for errors in your thinking, not in other's code ;) – Guillaume Jul 02 '10 at 06:31
  • For google I wouldn't dare to think they made an error. The C# code on the other hand... Any code that isn't properly indented is suspect. – Ritwik Bose Jul 04 '10 at 04:36

2 Answers2

7

You're looking at logarithms with different bases:

  • Java's System.out.println(Math.log(249.0/251.0)); is a natural log (base e)
  • C#'s Math.Log (x,y); gives the log of x with base specified by y
  • Google's Log(249.0/251.0) gives the log base 10

Though I don't get the result you do from C# (Math.Log( 249.0, 251.0) == 0.998552147171426).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • my apologies <- my C# code was miscopied (I had really complicated variable names and input translation on that line, though it does potentially (and certainly partially) answer my question: Google and Java are different bases) so the question is now Java vs C# – Ritwik Bose Jul 02 '10 at 05:44
  • I get the same result in C# as in Java with `Math.Log(249.0/251.0)` - at least to 16th decimal place. – Michael Burr Jul 02 '10 at 05:50
  • Hmmm interesting. I will spend another five hours poring over the C# code, in that case. Would someone be able to assure me that, say, an AMD processor, would return the same as an Intel processor? – Ritwik Bose Jul 02 '10 at 05:52
  • Instead of poring over the C# code, you might be able to get your answer much more readily stepping through and/or setting appropriate breakpoints in the debugger to ensure the `x` and `y` values are correct (as well as the result) when passed to `Math.Log()` and that that result isn't being modified at some point before being displayed. – Michael Burr Jul 02 '10 at 05:57
  • unfortunately I can't seem to run .Net on my Mac, which makes me sad. – Ritwik Bose Jul 02 '10 at 06:08
  • 1
    @Mechko, basically you can count on the same program generating the same output regardless of processor (unless you do exotic stuff with SSE3 etc). The "single step in debugger" advice is probably the best you can get right now. – Thorbjørn Ravn Andersen Jul 02 '10 at 06:08
  • @Thor Thanks for confirming the architecture stuff. – Ritwik Bose Jul 04 '10 at 04:35
  • @ThorbjørnRavnAndersen In .net you can't count on that. The JITer is free to make optimizations that affect the result, and it does that pretty much all the time, when using x87. – CodesInChaos Apr 12 '12 at 20:42
  • @CodeInChaos are you saying that the results may differ based on which cpu you run your program on? – Thorbjørn Ravn Andersen Apr 12 '12 at 23:47
  • @ThorbjørnRavnAndersen The results may differ depending on CPU, the .net version, the optimization level and probably the mood of the JITter. http://stackoverflow.com/questions/6683059/are-floating-point-numbers-consistent-in-c-can-they-be – CodesInChaos Apr 13 '12 at 00:27
3

You have a mistake somewhere in your C# program between where the log is calculated and where it is printed out. Math.Log gives the correct answer:

class P
{
  static void Main()
  {
      System.Console.WriteLine(System.Math.Log(249.0/251.0));
  }
}

prints out -0.00800004266707626

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067