How to do arithmetic operations using directly binary in C

0

I'm a beginner so there may be things wrong here.

I need to do thousands of calculations with data (decimal numbers float) of several giant csv files, so I figured that using the binary in the ieee754 standard would avoid conversion and this would save time.

This was my test:

#include <stdio.h>

int main()
{
    float a = 0b00111111110000000000000000000000; /* 1.5 */ /* uint32_t !? */
    float b = 0b00111111110000000000000000000000; /* 1.5 */  /* uint32_t !? */
    float c;

    c = a + b; /* 3.0 !? */

    printf("a: %f\n", a);
    printf("b: %f\n", b);
    printf("a+b: %f\n", c);

    return 0;
}  

It returns me:

a: 1069547520.000000
b: 1069547520.000000
a+b: 2139095040.000000

I realized that the value is integer, it seems to me that I should use uint32_t , it added up the two integer values that returned 2139095040 .

This 2139095040 value that returned left me confused it is ieee754 ? I can convert to decimal 3.0 I expected to return the default binary ieee754 01000000010000000000000000000000 and thus could convert to decimal again.

Finally, how can I use ieee754 binary data to do arithmetic operations and return the resulting value in binary and decimal in order to get more processing speed?

    
asked by anonymous 21.10.2018 / 18:46

1 answer

0

If you're wanting to use the native compiler implementation (and associated libraries) then you do not want to be smarter than the compiler: believe me, the people who wrote the compiler and the math libraries are pretty competent. So the best thing to do is not want to reinvent the wheel.

#include <stdio.h>

int main()
{
  double a = 1.5;
  double b = 1.5;
  double c;

  c = a + b; /* 3.0 !? */

  printf("a: %f\n", a);
  printf("b: %f\n", b);
  printf("a+b: %f\n", c);

  return 0;
}

Output:

zv@localhost Misc]$ ./so337866 
a: 1.500000
b: 1.500000
a+b: 3.000000
[zv@localhost Misc]$ 

Optionally, there are libraries of routines specific to many areas of mathematics, but usually only those who need them are experts.

    
21.10.2018 / 19:04