How to do arithmetic operations using directly binary in C

Asked

Viewed 95 times

0

I’m a beginner so there might be some wrong things here.

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

That 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;
}  

He returns to me:

a: 1069547520.000000
b: 1069547520.000000
a+b: 2139095040.000000

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

That value 2139095040 who returned left me confused he is ieee754? can convert to decimal 3.0 I was hoping to return the standard binary iee754 01000000010000000000000000000000 and so could convert to decimal again.

Finally, how can I use ieee754 binary data to perform arithmetic operations and return the resulting value of this operation in binary and decimal in order to achieve more processing speed?

  • *How to do arithmetic operations using directly binary in c (was convert is wrong sorry, can not change after posted)

  • 2

    Isn’t that what you’re trying to do, why write in binary? In the code you just get a headache, if it is a read data from somewhere you will need to do conversion and it will be much slower. Don’t try to do something you don’t know. Forget it, do the simple and be happy. And I hope that those values are not monetary or that they need accuracy. Take a look here because I talk in various places about this crazy thing that people are putting into the head that there is binary or non-binary number, there is number, just this, the way you write it doesn’t matter, use easy.

  • cannot change after posting - just click on the link edit and make the changes. And by complementing Maniero’s comment, binary numbers are not "binary", they can be represented in binary form. A number is just a value. Ex: the number 3 represents only the value 3 (the idea of a quantity, "3 things"). This value can be represented in several ways: as the symbol (digit) 3, in torque (base 2) 11, as the word três (or six in English, or in Japanese, etc). The representation changes but the value is the same.

  • @hkotsubo ok worked

1 answer

0

If you want to use the native compiler implementation (and associated libraries) then it is no use to want to outsmart the compiler: believe me, the people who wrote the compiler and the mathematical libraries are quite 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;
}

Exit:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.