Skip to content

Instantly share code, notes, and snippets.

@pgsin pgsin/math.exp.test.md
Last active Oct 16, 2017

Embed
What would you like to do?
Implementations of Math.Exp() and Math.Log() differ between OSes

I used Mono C# compiler version 5.2.0.0

mcs Program.cs

of this code

using System;

namespace Test {
    public class Program {
        public static void Main() {
            byte[] input = { 14, 243, 143, 0, 124, 41, 85, 64 };
            double inputD = BitConverter.ToDouble(input, 0);
            double outputD = Math.Exp(inputD);
            byte[] output = BitConverter.GetBytes(outputD);

            Console.WriteLine("Math.Exp(" + inputD + "\thex: " + BitConverter.ToString(input).Replace("-", " ") + ")\t=\t" +
                              outputD + "\thex: " + BitConverter.ToString(output).Replace("-", " "));

            input = new byte[] { 198, 77, 75, 30, 56, 151, 18, 65 };
            inputD = BitConverter.ToDouble(input, 0);
            outputD = Math.Log(inputD);
            output = BitConverter.GetBytes(outputD);

            Console.WriteLine("Math.Log(" + inputD + "\thex: " + BitConverter.ToString(input).Replace("-", " ") + ")\t=\t" +
                              outputD + "\thex: " + BitConverter.ToString(output).Replace("-", " "));
        }
    }
}

Windows 10.0.15063, net 4.6.1:

Math.Exp(84.6481934934384       hex: 0E F3 8F 00 7C 29 55 40)   =       5.7842004815199E+36     hex: 9A 64 2E 68 FC 67 91 47
Math.Log(304590.029584136       hex: C6 4D 4B 1E 38 97 12 41)   =       12.6267219860911        hex: 14 E4 43 B4 E1 40 29 40

Windows 10.0.15063, mono 5.2.0:

Math.Exp(84.6481934934384       hex: 0E F3 8F 00 7C 29 55 40)   =       5.7842004815199E+36     hex: 9A 64 2E 68 FC 67 91 47
Math.Log(304590.029584136       hex: C6 4D 4B 1E 38 97 12 41)   =       12.6267219860911        hex: 14 E4 43 B4 E1 40 29 40

Ubuntu 16.04, mono 5.2.0.224:

Math.Exp(84.6481934934384       hex: 0E F3 8F 00 7C 29 55 40)   =       5.7842004815199E+36     hex: 99 64 2E 68 FC 67 91 47
Math.Log(304590.029584136       hex: C6 4D 4B 1E 38 97 12 41)   =       12.6267219860911        hex: 15 E4 43 B4 E1 40 29 40

ArchLinux, mono 5.0.0:

Math.Exp(84.6481934934384       hex: 0E F3 8F 00 7C 29 55 40)   =       5.7842004815199E+36     hex: 99 64 2E 68 FC 67 91 47
Math.Log(304590.029584136       hex: C6 4D 4B 1E 38 97 12 41)   =       12.6267219860911        hex: 15 E4 43 B4 E1 40 29 40

Gentoo 2.4.1, Mono 5.2.0.196

Math.Exp(84.6481934934384       hex: 0E F3 8F 00 7C 29 55 40)   =       5.7842004815199E+36     hex: 99 64 2E 68 FC 67 91 47
Math.Log(304590.029584136       hex: C6 4D 4B 1E 38 97 12 41)   =       12.6267219860911        hex: 15 E4 43 B4 E1 40 29 40

import struct
import math

def bytes_to_double(data):
    return struct.unpack('d', data)[0]

def double_to_bytes(data):
    return struct.pack('d', data)

def bytes_to_hex_string(data):
    return ' '.join('{:02x}'.format(x).upper() for x in data)

inputBytes = bytes([14, 243, 143, 0, 124, 41, 85, 64])
inputD = bytes_to_double(inputBytes)
outputD = math.exp(inputD)
outputBytes = double_to_bytes(outputD)
print("Math.Exp(" + str(inputD) + "\thex: " + bytes_to_hex_string(inputBytes) \
      + ")\t=\t" \
      + str(outputD) + "\thex: " + bytes_to_hex_string(outputBytes))

inputBytes = bytes([198, 77, 75, 30, 56, 151, 18, 65])
inputD = bytes_to_double(inputBytes)
outputD = math.log(inputD)
outputBytes = double_to_bytes(outputD)

print("Math.Log(" + str(inputD) + "\thex: " + bytes_to_hex_string(inputBytes) \
      + ")\t=\t" \
      + str(outputD) + "\thex: " + bytes_to_hex_string(outputBytes))

Windows 10.0.15063, python 3.6.1:

Math.Exp(84.6481934934384       hex: 0E F3 8F 00 7C 29 55 40)   =       5.7842004815199E+36     hex: 9A 64 2E 68 FC 67 91 47
Math.Log(304590.029584136       hex: C6 4D 4B 1E 38 97 12 41)   =       12.6267219860911        hex: 14 E4 43 B4 E1 40 29 40

Ubuntu 16.04, python 3.5.2:

Math.Exp(84.64819349343836      hex: 0E F3 8F 00 7C 29 55 40)   =       5.784200481519901e+36   hex: 99 64 2E 68 FC 67 91 47
Math.Log(304590.0295841362      hex: C6 4D 4B 1E 38 97 12 41)   =       12.626721986091107      hex: 15 E4 43 B4 E1 40 29 40

#include <iostream>
#include <sstream>
#include <iomanip>
#include <math.h> 
#include <string>

using namespace std;

typedef unsigned char uchar;

string doubleToHexString(double d){
    uchar * bytes = (uchar*) &d;
    stringstream out;
    for (int i = 0; i < 8; ++i){
        out << hex << setfill('0') << setw(2) << uppercase << (int) bytes[i] << " ";
    }
    return out.str();
}

int main() {
    unsigned char input[] = { 14, 243, 143, 0, 124, 41, 85, 64 };
    double* inputD = (double*)input;
    double outputD = exp(*inputD);
    cout<<"std.exp(" << *inputD << "\thex: " << doubleToHexString(*inputD) <<"\t=\t" \
    <<outputD<<"\thex: "<<doubleToHexString(outputD)<<" ) "<<endl;

    unsigned char input1[] = { 198, 77, 75, 30, 56, 151, 18, 65 };
    inputD = (double*)input1;
    outputD = log(*inputD);
    cout<<"std.log(" << *inputD << "\thex: " << doubleToHexString(*inputD) <<"\t=\t" \
    <<outputD<<"\thex: "<<doubleToHexString(outputD)<<" ) "<<endl;
}

Windows 10.0.15063, g++ 6.4.0:

std.exp(84.6482 hex: 0E F3 8F 00 7C 29 55 40    =       5.7842e+36      hex: 9A 64 2E 68 FC 67 91 47  )
std.log(304590  hex: C6 4D 4B 1E 38 97 12 41    =       12.6267         hex: 14 E4 43 B4 E1 40 29 40  )

Ubuntu 16.04, g++ 5.4.0 20160609:

std.exp(84.6482 hex: 0E F3 8F 00 7C 29 55 40    =       5.7842e+36      hex: 99 64 2E 68 FC 67 91 47  )
std.log(304590  hex: C6 4D 4B 1E 38 97 12 41    =       12.6267         hex: 15 E4 43 B4 E1 40 29 40  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.