Skip to content

Instantly share code, notes, and snippets.

@bshambaugh
Created December 18, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bshambaugh/366f17c8ae682699f66cb930d6a11a4a to your computer and use it in GitHub Desktop.
Save bshambaugh/366f17c8ae682699f66cb930d6a11a4a to your computer and use it in GitHub Desktop.
compare to floatToIEEE754Binary.h and floatToIEEE754Binary.c in https://github.com/bshambaugh/changearray/tree/master/src
#include <stdlib.h>
// put your setup code here, to run once:
struct binArray32 {
char d[32];
};
typedef struct binArray32 Struct32Int;
typedef union {
float f;
struct
{
/* Order is important.
* Here the members of the union data structure
* use the same memory (32 bits).
* The ordering is taken
* from the LSB to the MSB. */
unsigned int sign : 1;
unsigned int exponent : 8;
unsigned int mantissa : 23;
} raw;
} myfloat32;
void printBinary(int n, int i,int array[]);
void printIEEE(myfloat32 var, int l, int m, int k, int a[], int b[], int c[], int d[]);
Struct32Int floatToIEEE754Int(float q);
/* or printBinary(int n, int i) */
void printBinary(int n, int i,int array[])
{
/* Prints the binary representation
of a number n up to i-bits. */
int k;
int j = 0;
for (k = i - 1; k >= 0; k--) {
if ((n >> k) & 1) {
array[j] = 1;
j++;
}
else {
array[j] = 0;
j++;
}
}
}
/* Function to convert real value
to IEEE foating point representation */
/* printIEEE needs to return a struct with an array of length, l, m, k */
void printIEEE(myfloat32 var, int l, int m, int k, int a[], int b[], int c[], int d[])
{
int i;
/* Prints the IEEE 754 representation
of a float value (32 bits) */
printBinary(var.raw.sign,l,a);
printBinary(var.raw.exponent,m,b);
printBinary(var.raw.mantissa,k,c);
/* I need to take all of these array elements and roll it into one array */
d[0] = a[0];
for(i = 0; i < m; i++) {
d[i+l] = b[i];
}
for(i = 0; i < k; i++) {
d[i+l+m] = c[i];
}
}
/* function float To IEEE */
Struct32Int floatToIEEE754Int(float q)
{
Struct32Int r;
int *a;
int *b;
int *c;
int *d;
int l = 1;
int m = 8;
int k = 23;
int p, i;
p = l + m + k;
a = (int *)calloc(l,sizeof(int));
b = (int *)calloc(m,sizeof(int));
c = (int *)calloc(k,sizeof(int));
d = (int *)calloc(p,sizeof(int));
myfloat32 var;
var.f = q;
printIEEE(var,l,m,k,a,b,c,d);
/* I need to set r = d */
for(i = 0; i < p; i++) {
r.d[i] = d[i];
}
free(a);
free(b);
free(c);
free(d);
/* I need to set r = d... */
return r;
}
void setup() {
Serial.begin(115200);
float q = 7.75;
int arrayTwo[32];
int i;
Struct32Int p;
p = floatToIEEE754Int(q);
for (i=0; i < 32; i++)
{
arrayTwo[i] = p.d[i];
Serial.println(arrayTwo[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
@bshambaugh
Copy link
Author

bshambaugh commented Dec 18, 2020

output:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
1
1
1
1
0
0
0

@bshambaugh
Copy link
Author

#include <stdlib.h>
// put your setup code here, to run once:
struct binArray32 {
char d[32];
};

typedef struct binArray32 Struct32Int;

typedef union {

    float f;
    struct
    {

            /* Order is important.
             * Here the members of the union data structure
             *  use the same memory (32 bits).
             * The ordering is taken
             * from the LSB to the MSB. */              
            unsigned int mantissa : 23;
            unsigned int exponent : 8;
            unsigned int sign : 1;

    } raw;

} myfloat32;

void printBinary(int n, int i,int array[]);
void printIEEE(myfloat32 var, int l, int m, int k, int a[], int b[], int c[], int d[]);
Struct32Int floatToIEEE754Int(float q);

/* or printBinary(int n, int i) */
void printBinary(int n, int i,int array[])
{

/* Prints the binary representation
of a number n up to i-bits. */
int k;
int j = 0;
for (k = i - 1; k >= 0; k--) {

if ((n >> k) & 1) {
        array[j] = 1;
        j++;
}
else {
  array[j] = 0;
        j++;
     }

}
}

/* Function to convert real value
to IEEE foating point representation */

/* printIEEE needs to return a struct with an array of length, l, m, k */
void printIEEE(myfloat32 var, int l, int m, int k, int a[], int b[], int c[], int d[])
{
int i;

/* Prints the IEEE 754 representation
of a float value (32 bits) */

printBinary(var.raw.sign,l,a);
printBinary(var.raw.exponent,m,b);
printBinary(var.raw.mantissa,k,c);

/* I need to take all of these array elements and roll it into one array */
d[0] = a[0];

for(i = 0; i < m; i++) {
d[i+l] = b[i];
}

for(i = 0; i < k; i++) {
d[i+l+m] = c[i];
}

}

/* function float To IEEE */
Struct32Int floatToIEEE754Int(float q)
{
Struct32Int r;
int *a;
int *b;
int *c;
int *d;
int l = 1;
int m = 8;
int k = 23;
int p, i;

 p = l + m + k;

 a = (int *)calloc(l,sizeof(int));
 b = (int *)calloc(m,sizeof(int));
 c = (int *)calloc(k,sizeof(int));
 d = (int *)calloc(p,sizeof(int));

 myfloat32 var;

 var.f = q;

 printIEEE(var,l,m,k,a,b,c,d);

 /* I need to set r = d */
 for(i = 0; i < p; i++) {
      r.d[i] = d[i];
 }


 free(a);
 free(b);
 free(c);
 free(d);

 /* I need to set r = d... */

 return r;

}

void setup() {
Serial.begin(115200);
float q = 7.75;
int arrayTwo[32];
int i;
Struct32Int p;
p = floatToIEEE754Int(q);
for (i=0; i < 32; i++)
{
arrayTwo[i] = p.d[i];
Serial.println(arrayTwo[i]);
}

}

void loop() {
// put your main code here, to run repeatedly:

}

@bshambaugh
Copy link
Author

output:
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

@bshambaugh
Copy link
Author

My best guess is that the bit shifting in printBinary is problematic. It works fine on Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz (64-bit) and
gives the results from https://www.h-schmidt.net/FloatConverter/IEEE754.html but it gives the above results with ATMEGA328P U (8-bit).

@bshambaugh
Copy link
Author

/// try running this:::

#include <stdlib.h>
// put your setup code here, to run once:
struct binArray32 {
char e[32];
};

typedef struct binArray32 Struct32Int;

typedef union {

    float f;
    struct
    {

            /* Order is important.
             * Here the members of the union data structure
             *  use the same memory (32 bits).
             * The ordering is taken
             * from the LSB to the MSB. */              
            unsigned int byteOne : 8;
            unsigned int byteTwo: 8;
            unsigned int byteThree : 8;
            unsigned int byteFour : 8;

    } raw;

} myfloat32;

void printBinary(int n, int i,int array[]);
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[]);
Struct32Int floatToIEEE754Int(float q);

/* or printBinary(int n, int i) */
void printBinary(int n, int i,int array[])
{

/* Prints the binary representation
of a number n up to i-bits. */
int k;
int j = 0;
for (k = i - 1; k >= 0; k--) {

if ((n >> k) & 1) {
        array[j] = 1;
        j++;
}
else {
  array[j] = 0;
        j++;
     }

}
}

/* Function to convert real value
to IEEE foating point representation */

/* printIEEE needs to return a struct with an array of length, l, m, k */
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[])
{

int i;

/* Prints the IEEE 754 representation
of a float value (32 bits) */

printBinary(var.raw.byteOne ,one,a);
printBinary(var.raw.byteTwo,two,b);
printBinary(var.raw.byteThree,three,c);
printBinary(var.raw.byteFour,four,d);

/* I need to take all of these array elements and roll it into one array */
for(i = 0; i < one; i++) {
e[i] = a[i];
}

for(i = 0; i < two; i++) {
e[i+one] = b[i];
}

for(i = 0; i < three; i++) {
  e[i+one+two] = c[i];

}

for(i = 0; i < four; i++) {
e[i+one+two+three] = d[i];
}

}

/* function float To IEEE */
Struct32Int floatToIEEE754Int(float q)
{
Struct32Int r;
int *a;
int *b;
int *c;
int *d;
int *e;
int one = 8;
int two = 8;
int three = 8;
int four = 8;
int p, i;

 p = one + two + three + four;

 a = (int *)calloc(one,sizeof(int));
 b = (int *)calloc(two,sizeof(int));
 c = (int *)calloc(three,sizeof(int));
 d = (int *)calloc(four,sizeof(int));
 e = (int *)calloc(p,sizeof(int));

 myfloat32 var;

 var.f = q;

 printIEEE(var,one,two,three,four,a,b,c,d,e);

 /* I need to set r = d */
 for(i = 0; i < p; i++) {
      r.e[i] = e[i];
 }


 free(a);
 free(b);
 free(c);
 free(d);

 /* I need to set r = d... */

 return r;

}

void setup() {
Serial.begin(115200);
float q = 7.75;
int arrayTwo[32];
int i;
Struct32Int p;
p = floatToIEEE754Int(q);
for (i=0; i < 32; i++)
{
arrayTwo[i] = p.e[i];
Serial.println(arrayTwo[i]);
}

}

void loop() {
// put your main code here, to run repeatedly:

}

@bshambaugh
Copy link
Author

bshambaugh commented Dec 18, 2020

output on ardunio::
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
0
0
0
0
1
0
0
0
0
0
0

@bshambaugh
Copy link
Author

#include <stdlib.h>
#include <stdio.h>

// put your setup code here, to run once:
struct binArray32 {
char e[32];
};

typedef struct binArray32 Struct32Int;

typedef union {

    float f;
    struct
    {

            /* Order is important.
             * Here the members of the union data structure
             *  use the same memory (32 bits).
             * The ordering is taken
             * from the LSB to the MSB. */              
            unsigned int byteOne : 8;
            unsigned int byteTwo: 8;
            unsigned int byteThree : 8;
            unsigned int byteFour : 8;

    } raw;

} myfloat32;

void printBinary(int n, int i,int array[]);
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[]);
Struct32Int floatToIEEE754Int(float q);

/* or printBinary(int n, int i) */
void printBinary(int n, int i,int array[])
{

/* Prints the binary representation
of a number n up to i-bits. */
int k;
int j = 0;
for (k = i - 1; k >= 0; k--) {

if ((n >> k) & 1) {
        array[j] = 1;
        j++;
}
else {
  array[j] = 0;
        j++;
     }

}
}

/* Function to convert real value
to IEEE foating point representation */

/* printIEEE needs to return a struct with an array of length, l, m, k */
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[])
{

int i;

/* Prints the IEEE 754 representation
of a float value (32 bits) */

printBinary(var.raw.byteOne ,one,a);
printBinary(var.raw.byteTwo,two,b);
printBinary(var.raw.byteThree,three,c);
printBinary(var.raw.byteFour,four,d);

/* I need to take all of these array elements and roll it into one array */
for(i = 0; i < one; i++) {
e[i] = a[i];
}

for(i = 0; i < two; i++) {
e[i+one] = b[i];
}

for(i = 0; i < three; i++) {
  e[i+one+two] = c[i];

}

for(i = 0; i < four; i++) {
e[i+one+two+three] = d[i];
}

}

/* function float To IEEE */
Struct32Int floatToIEEE754Int(float q)
{
Struct32Int r;
int *a;
int *b;
int *c;
int *d;
int *e;
int one = 8;
int two = 8;
int three = 8;
int four = 8;
int p, i;

 p = one + two + three + four;

 a = (int *)calloc(one,sizeof(int));
 b = (int *)calloc(two,sizeof(int));
 c = (int *)calloc(three,sizeof(int));
 d = (int *)calloc(four,sizeof(int));
 e = (int *)calloc(p,sizeof(int));

 myfloat32 var;

 var.f = q;

 printIEEE(var,one,two,three,four,a,b,c,d,e);

 /* I need to set r = d */
 for(i = 0; i < p; i++) {
      r.e[i] = e[i];
 }


 free(a);
 free(b);
 free(c);
 free(d);

 /* I need to set r = d... */

 return r;

}

int main() {
float q = 7.75;
int arrayTwo[32];
int i;
Struct32Int p;
p = floatToIEEE754Int(q);
for (i=0; i < 32; i++)
{
arrayTwo[i] = p.e[i];
printf("%d\n",arrayTwo[i]);
}

}

@bshambaugh
Copy link
Author

output from intel-core-i7-7700hq:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
0
0
0
0
1
0
0
0
0
0
0

@bshambaugh
Copy link
Author

#include <stdlib.h>
// put your setup code here, to run once:
struct binArray32 {
char e[32];
};

typedef struct binArray32 Struct32Int;

typedef union {

    float f;
    struct
    {

            /* Order is important.
             * Here the members of the union data structure
             *  use the same memory (32 bits).
             * The ordering is taken
             * from the LSB to the MSB. */              
            unsigned int byteFour : 8;
            unsigned int byteThree : 8;
            unsigned int byteTwo: 8;
            unsigned int byteOne : 8;

    } raw;

} myfloat32;

void printBinary(int n, int i,int array[]);
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[]);
Struct32Int floatToIEEE754Int(float q);

/* or printBinary(int n, int i) */
void printBinary(int n, int i,int array[])
{

/* Prints the binary representation
of a number n up to i-bits. */
int k;
int j = 0;
for (k = i - 1; k >= 0; k--) {

if ((n >> k) & 1) {
        array[j] = 1;
        j++;
}
else {
  array[j] = 0;
        j++;
     }

}
}

/* Function to convert real value
to IEEE foating point representation */

/* printIEEE needs to return a struct with an array of length, l, m, k */
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[])
{

int i;

/* Prints the IEEE 754 representation
of a float value (32 bits) */

printBinary(var.raw.byteOne ,one,a);
printBinary(var.raw.byteTwo,two,b);
printBinary(var.raw.byteThree,three,c);
printBinary(var.raw.byteFour,four,d);

/* I need to take all of these array elements and roll it into one array */
for(i = 0; i < one; i++) {
e[i] = a[i];
}

for(i = 0; i < two; i++) {
e[i+one] = b[i];
}

for(i = 0; i < three; i++) {
  e[i+one+two] = c[i];

}

for(i = 0; i < four; i++) {
e[i+one+two+three] = d[i];
}

}

/* function float To IEEE */
Struct32Int floatToIEEE754Int(float q)
{
Struct32Int r;
int *a;
int *b;
int *c;
int *d;
int *e;
int one = 8;
int two = 8;
int three = 8;
int four = 8;
int p, i;

 p = one + two + three + four;

 a = (int *)calloc(one,sizeof(int));
 b = (int *)calloc(two,sizeof(int));
 c = (int *)calloc(three,sizeof(int));
 d = (int *)calloc(four,sizeof(int));
 e = (int *)calloc(p,sizeof(int));

 myfloat32 var;

 var.f = q;

 printIEEE(var,one,two,three,four,a,b,c,d,e);

 /* I need to set r = d */
 for(i = 0; i < p; i++) {
      r.e[i] = e[i];
 }


 free(a);
 free(b);
 free(c);
 free(d);

 /* I need to set r = d... */

 return r;

}

void setup() {
Serial.begin(115200);
float q = 7.75;
int arrayTwo[32];
int i;
Struct32Int p;
p = floatToIEEE754Int(q);
for (i=0; i < 32; i++)
{
arrayTwo[i] = p.e[i];
Serial.println(arrayTwo[i]);
}

}

void loop() {
// put your main code here, to run repeatedly:

}

@bshambaugh
Copy link
Author

output on ardunio:

0
1
0
0
0
0
0
0
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

@bshambaugh
Copy link
Author

#include <stdlib.h>
#include <stdio.h>

// put your setup code here, to run once:
struct binArray32 {
char e[32];
};

typedef struct binArray32 Struct32Int;

typedef union {

    float f;
    struct
    {

            /* Order is important.
             * Here the members of the union data structure
             *  use the same memory (32 bits).
             * The ordering is taken
             * from the LSB to the MSB. */              
            unsigned int byteFour : 8;
            unsigned int byteThree : 8;
	unsigned int byteTwo: 8;
	unsigned int byteOne : 8;

    } raw;

} myfloat32;

void printBinary(int n, int i,int array[]);
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[]);
Struct32Int floatToIEEE754Int(float q);

/* or printBinary(int n, int i) */
void printBinary(int n, int i,int array[])
{

/* Prints the binary representation
of a number n up to i-bits. */
int k;
int j = 0;
for (k = i - 1; k >= 0; k--) {

if ((n >> k) & 1) {
        array[j] = 1;
        j++;
}
else {
  array[j] = 0;
        j++;
     }

}
}

/* Function to convert real value
to IEEE foating point representation */

/* printIEEE needs to return a struct with an array of length, l, m, k */
void printIEEE(myfloat32 var, int one, int two, int three, int four, int a[], int b[], int c[], int d[], int e[])
{

int i;

/* Prints the IEEE 754 representation
of a float value (32 bits) */

printBinary(var.raw.byteOne ,one,a);
printBinary(var.raw.byteTwo,two,b);
printBinary(var.raw.byteThree,three,c);
printBinary(var.raw.byteFour,four,d);

/* I need to take all of these array elements and roll it into one array */
for(i = 0; i < one; i++) {
e[i] = a[i];
}

for(i = 0; i < two; i++) {
e[i+one] = b[i];
}

for(i = 0; i < three; i++) {
  e[i+one+two] = c[i];

}

for(i = 0; i < four; i++) {
e[i+one+two+three] = d[i];
}

}

/* function float To IEEE */
Struct32Int floatToIEEE754Int(float q)
{
Struct32Int r;
int *a;
int *b;
int *c;
int *d;
int *e;
int one = 8;
int two = 8;
int three = 8;
int four = 8;
int p, i;

 p = one + two + three + four;

 a = (int *)calloc(one,sizeof(int));
 b = (int *)calloc(two,sizeof(int));
 c = (int *)calloc(three,sizeof(int));
 d = (int *)calloc(four,sizeof(int));
 e = (int *)calloc(p,sizeof(int));

 myfloat32 var;

 var.f = q;

 printIEEE(var,one,two,three,four,a,b,c,d,e);

 /* I need to set r = d */
 for(i = 0; i < p; i++) {
      r.e[i] = e[i];
 }


 free(a);
 free(b);
 free(c);
 free(d);

 /* I need to set r = d... */

 return r;

}

int main() {
float q = 7.75;
int arrayTwo[32];
int i;
Struct32Int p;
p = floatToIEEE754Int(q);
for (i=0; i < 32; i++)
{
arrayTwo[i] = p.e[i];
printf("%d\n",arrayTwo[i]);
}

}

@bshambaugh
Copy link
Author

output on intel-core-i7-7700hq:

0
1
0
0
0
0
0
0
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment