Last active
August 13, 2024 13:39
-
-
Save LightningStalker/18d5467f1575d2870417d618f0daf20c to your computer and use it in GitHub Desktop.
Examine binary representations of floating point numbers on your machine
This file contains 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
/* Compile with gcc -Wall -o floperation floperation.c -lquadmath*/ | |
/* Examine binary representations of floating point numbers | |
on your machine | |
Project Crew 8/10/2024 */ | |
//#include <immintrin.h> | |
#include <quadmath.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define FLOAT_TYPE double | |
#define DESC "double" | |
#define INT_TYPE char | |
void | |
int2bin (INT_TYPE myint[], size_t sizeofp, char retstr[]) | |
{ | |
int c; // counter | |
for (c = 7; c > -1; c--) | |
{ | |
//printf("%i", c); | |
retstr[c] = (myint[0] & 1) ? *"1" : *"0"; // test each bit | |
myint[0] >>= 1; // shift in next bit | |
} | |
//puts(""); | |
return; | |
} | |
void | |
trouble (void) | |
{ | |
puts("I had trouble printing that."); | |
exit (EXIT_FAILURE); | |
} | |
int | |
main (int argc, char **argv) | |
{ | |
size_t floatsz = sizeof(FLOAT_TYPE); // size in bytes on current machine | |
char nbytes = floatsz; | |
char strout[] = { [0 ... 130] = 0 }; // initialize output string to 0 | |
char buf[80] = { [0 ... 79] = 0 }; | |
int c, d; | |
if(argc == 2) | |
{ // keep going... | |
union flotion | |
{ | |
INT_TYPE i[nbytes]; | |
FLOAT_TYPE f; // fp variable holding _the number_ | |
}flote; | |
flote.f = strtod(argv[1], NULL); | |
/*FLOAT_TYPE floteval = strtoflt128(argv[1], NULL); // fp variable holding _the number_ | |
FLOAT_TYPE *floteptr = &floteval; // initial pointer | |
uint8_t (*flote) [nbytes] = (uint8_t (*)[])floteptr; // convert it to unsigned int | |
INT_TYPE (*floteh)[2] = (INT_TYPE (*)[])*flote; // buffer to hold working copy of data | |
*/ | |
floatsz *= 8; // get number of bits | |
nbytes--; // account for 0th element in array | |
/*quadmath_snprintf (buf, sizeof buf, "%Qf", | |
*floteptr); | |
*/ | |
d = sprintf(buf, "%f 0x", flote.f); | |
if (d < 2) trouble(); | |
for (c = nbytes; c > 0; c--) | |
{ | |
if (sprintf(&buf[d], "%hhX", flote.i[c]) < 2) trouble(); | |
d += 2; | |
} | |
printf ("%s (%zi-bit " DESC ")\n\n", | |
buf, floatsz); | |
floatsz--; // decrement to account for 0 bit | |
// *flote = 38; // debug | |
for (c = nbytes; c > 0; c--) | |
{ | |
//printf("%i", c); | |
int2bin(&flote.i[c], floatsz, strout); | |
if (printf("%s", strout) < 2) trouble(); // final result of binary conversion | |
} | |
puts(""); | |
// printf("%zi", sizeof(unsigned long long int)); | |
// how we doing? | |
return(0); | |
}else | |
{ | |
puts ("Did you know something about floating point numbers?"); // boiler plate | |
puts ("open floperation.c in a text editor, change FLOAT_TYPE and recompile"); // lorem ipsum | |
puts ("..\n"); | |
puts ("floperation displays floating point numbers supported on your system"); | |
puts ("Usage: floperation XX.XXXXXX where X is some number"); | |
puts ("Example: floperation 53.33333333333333"); | |
puts ("Output depends on the type of float being displayed"); | |
return (1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment