none
[UWA] Math.Log RRS feed

  • Frage

  • Hello,

    so maybe I do not understand Math, but could someone please explain why e.g. (int)Math.Log(8, 2) = 2 for Windows universal ?

    Edit: Better sample:

             for (int i = 1; i < 65; i *= 2)
             {
                var value = Math.Log(i, 2);
                Debug.WriteLine($"Convert.ToInt32(Log2(x)) = {Convert.ToInt32(value)} {(Convert.ToInt32(value) != (int)value ? "!=" : "=")} {(int)value} = (int)Log2(x) <=> Log2({i}) = {DoubleConverter.ToExactString(value)}");
             }
    
    
    
    
    Convert.ToInt32(Log2(x)) = 0 = 0 = (int)Log2(x) <=> Log2(1) = 0
    Convert.ToInt32(Log2(x)) = 1 = 1 = (int)Log2(x) <=> Log2(2) = 1
    Convert.ToInt32(Log2(x)) = 2 = 2 = (int)Log2(x) <=> Log2(4) = 2
    Convert.ToInt32(Log2(x)) = 3 != 2 = (int)Log2(x) <=> Log2(8) = 2.999999999999999555910790149937383830547332763671875
    Convert.ToInt32(Log2(x)) = 4 = 4 = (int)Log2(x) <=> Log2(16) = 4
    Convert.ToInt32(Log2(x)) = 5 = 5 = (int)Log2(x) <=> Log2(32) = 5
    Convert.ToInt32(Log2(x)) = 6 != 5 = (int)Log2(x) <=> Log2(64) = 5.99999999999999911182158029987476766109466552734375
    You need http://jonskeet.uk/csharp/DoubleConverter.cs from http://csharpindepth.com/Articles/General/FloatingPoint.aspx

    Thanks.






    • Bearbeitet cubedit Freitag, 22. April 2016 18:38
    Freitag, 22. April 2016 13:10

Antworten

Alle Antworten