Skip to content

Instantly share code, notes, and snippets.

@lpenguin lpenguin/float_data.py

Last active Oct 15, 2017
Embed
What would you like to do?
math-exp-cpp
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)
#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
You can’t perform that action at this time.