Last active
October 14, 2018 09:40
-
-
Save yuq-1s/a883af5d136ab71b34132dbf7df38b81 to your computer and use it in GitHub Desktop.
Playing with floats: bits to floats and floats to bits.
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 "showbits.h" | |
#include <iostream> | |
#include <algorithm> | |
#include <string> | |
#include <locale> | |
#include <cassert> | |
typedef union { | |
int64_t i; | |
double f; | |
} i2f64_t; | |
typedef union { | |
int32_t i; | |
float f; | |
} i2f32_t; | |
void trim(std::string& s) { | |
s.erase(std::remove_if( | |
begin(s), end(s), | |
[l = std::locale{}](auto ch) { return std::isspace(ch, l); } | |
), end(s)); | |
} | |
template <class T> | |
T mystoi(std::string s) { | |
if (s[0] == '1') { | |
s[0] = '0'; | |
T num = std::stoi(s, nullptr, 2); | |
return num - (1 << (sizeof(T)*8-1)); | |
} else { | |
return std::stoi(s, nullptr, 2); | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if (argc < 2) { | |
printf("usage: %s, -d <double_number> or -f <float_number>\n", argv[0]); | |
exit(1); | |
} | |
if (strcmp(argv[1], "-d") == 0) { | |
for (int i = 2; i < argc; ++i) { | |
std::string input = argv[i]; | |
trim(input); | |
assert(input.size() == 64); | |
i2f64_t number = {.i = mystoi<int64_t>(input)}; | |
printBits(sizeof(int64_t), &number); | |
printf("%lf\n", number.f); | |
} | |
} else if (strcmp(argv[1], "-f") == 0) { | |
for (int i = 2; i < argc; ++i) { | |
std::string input = argv[i]; | |
trim(input); | |
assert(input.size() == 32); | |
i2f32_t number = {.i = mystoi<int32_t>(input)}; | |
printBits(sizeof(int32_t), &number); | |
printf("%f\n", number.f); | |
} | |
} else { | |
printf("usage: %s, -d <double_number> or -f <float_number>\n", argv[0]); | |
exit(1); | |
} | |
} |
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 <stdio.h> | |
#include <float.h> | |
#include <string.h> | |
#include <limits.h> | |
#include <stdlib.h> | |
#include "showbits.h" | |
int main(int argc, char* argv[]) | |
{ | |
if (argc < 2) { | |
printf("usage: %s, -d <double_number> or -f <float_number>\n", argv[0]); | |
exit(1); | |
} | |
if (strcmp(argv[1], "-d") == 0) { | |
if (argc == 2) { | |
printf("DBL_MIN: %lf\n", DBL_MIN); | |
double dblmin = DBL_MIN; | |
printBits(sizeof(float), &dblmin); | |
printf("DBL_MAX: %lf\n", DBL_MAX); | |
double dblmax = DBL_MAX; | |
printBits(sizeof(float), &dblmax); | |
} else { | |
for (int i = 2; i < argc; ++i) { | |
double input = strtod(argv[i], NULL); | |
printBits(sizeof(double), &input); | |
} | |
} | |
} else if (strcmp(argv[1], "-f") == 0) { | |
if (argc == 2) { | |
printf("FLT_MIN: %f\n", FLT_MIN); | |
float fltmin = FLT_MIN; | |
printBits(sizeof(float), &fltmin); | |
printf("FLT_MAX: %f\n", FLT_MAX); | |
float fltmax = FLT_MAX; | |
printBits(sizeof(float), &fltmax); | |
} else { | |
for (int i = 2; i < argc; ++i) { | |
float input = atof(argv[i]); | |
printBits(sizeof(float), &input); | |
} | |
} | |
} else { | |
printf("usage: %s, -d <double_number> or -f <float_number>\n", argv[0]); | |
exit(1); | |
} | |
} |
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
#!/usr/bin/env python3 | |
import sys | |
bitstring = input() | |
del_pos = int(sys.argv[1]) | |
while bitstring: | |
print(bitstring[:1]+' '+bitstring[1:del_pos]+' '+bitstring[del_pos:]) | |
bitstring = input() |
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 <stdio.h> | |
#include <float.h> | |
#include <string.h> | |
#include <limits.h> | |
#include <stdlib.h> | |
#include "showbits.h" | |
void printBits(size_t const size, void const * const ptr) | |
{ | |
unsigned char *b = (unsigned char*) ptr; | |
unsigned char byte; | |
int i, j; | |
for (i=size-1;i>=0;i--) | |
{ | |
for (j=7;j>=0;j--) | |
{ | |
byte = (b[i] >> j) & 1; | |
printf("%u", byte); | |
} | |
/*printf(" ");*/ | |
} | |
puts(""); | |
} | |
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
#ifndef _SHOWBITS_H | |
#define _SHOWBITS_H | |
#include <stdio.h> | |
#include <float.h> | |
#include <string.h> | |
#include <limits.h> | |
#include <stdlib.h> | |
void printBits(size_t const size, void const * const ptr); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment