Last active
October 15, 2017 13:54
-
-
Save lpenguin/9bb5f0a677b7a91bc2d023902bad5b50 to your computer and use it in GitHub Desktop.
math-exp-cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) for x in data) | |
inputBytes = bytes([14, 243, 143, 0, 124, 41, 85, 64]) | |
inputD = bytes_to_double(inputBytes) | |
print(bytes_to_hex_string(inputBytes)) | |
print(inputD) | |
outputD = math.exp(inputD) | |
outputBytes = double_to_bytes(outputD) | |
print(bytes_to_hex_string(outputBytes)) | |
print(outputD) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) << (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<<"exp(" << *inputD << "\thex: " << doubleToHexString(*inputD) <<"\t=\t" \ | |
<<outputD<<"\thex: "<<doubleToHexString(outputD)<<" ) "<<endl; | |
outputD = sqrt(*inputD); | |
cout<<"sqrt(" << *inputD << "\thex: " << doubleToHexString(*inputD) <<"\t=\t" \ | |
<<outputD<<"\thex: "<<doubleToHexString(outputD)<<" ) "<<endl; | |
outputD = *inputD * *inputD; | |
cout<<"mul(" << *inputD << "\thex: " << doubleToHexString(*inputD) <<"\t=\t" \ | |
<<outputD<<"\thex: "<<doubleToHexString(outputD)<<" ) "<<endl; | |
} | |
// windows 7, mingw-64 g++: | |
// exp(84.6482 hex: 0e f3 8f 00 7c 29 55 40 ) = 5.7842e+36 hex: 9a 64 2e 68 fc 67 91 47 ) | |
// Apple LLVM version 8.0.0 (clang-800.0.42.1): | |
// exp(84.6482 hex: 0e f3 8f 00 7c 29 55 40 = 5.7842e+36 hex: 99 64 2e 68 fc 67 91 47 ) | |
// g++-7 (Homebrew GCC 7.2.0) 7.2.0: | |
// exp(84.6482 hex: 0e f3 8f 00 7c 29 55 40 = 5.7842e+36 hex: 99 64 2e 68 fc 67 91 47 ) | |
// windows-server, mingw-64 g++: | |
// exp(84.6482 hex: 0e f3 8f 00 7c 29 55 40 ) = 5.7842e+36 hex: 9a 64 2e 68 fc 67 91 47 ) | |
// windows-server, mingw-32 g++: | |
// exp(84.6482 hex: 0e f3 8f 00 7c 29 55 40 ) = 5.7842e+36 hex: 99 64 2e 68 fc 67 91 47 ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment