Browse Source

addition sum of two double numbers precisions

master
Dennis Sperzel 11 months ago
parent
commit
80281b1b90
  1. 17
      src/addition.c
  2. 2
      src/addition.h
  3. 2
      test/test_addition.c

17
src/addition.c

@ -19,9 +19,9 @@ struct datad {
double dnum; double dnum;
unsigned long long lnum; unsigned long long lnum;
} number; } number;
unsigned long sign;
unsigned long exponent;
unsigned long precision;
unsigned long long sign;
unsigned long long exponent;
unsigned long long precision;
}; };
// full_adder is an implementation of two bit addition with carry // full_adder is an implementation of two bit addition with carry
@ -97,7 +97,8 @@ float addition_float(float number1, float number2) {
num1.exponent = exponent_float(num1.number.integer), num2.exponent = exponent_float(num2.number.integer); num1.exponent = exponent_float(num1.number.integer), num2.exponent = exponent_float(num2.number.integer);
num1.precision = precision_float(num1.number.integer) | e, num2.precision = precision_float(num2.number.integer) | e; num1.precision = precision_float(num1.number.integer) | e, num2.precision = precision_float(num2.number.integer) | e;
num3.precision = addition_precision_float(num1.precision, num2.precision >> (num1.exponent - num2.exponent));
unsigned int k = (num1.exponent - num2.exponent > 23 ? 24 : num1.exponent - num2.exponent);
num3.precision = addition_precision_float(num1.precision, num2.precision >> k );
if (num3.precision > 0x00ffffff) { if (num3.precision > 0x00ffffff) {
num3.exponent = addition_uint(num1.exponent, 1); num3.exponent = addition_uint(num1.exponent, 1);
num3.precision = (num3.precision >> 1); num3.precision = (num3.precision >> 1);
@ -136,3 +137,11 @@ unsigned long long exponent_double(unsigned long long number) {
unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision) { unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision) {
return (sign << 63) | (exponent << 52) | precision; return (sign << 63) | (exponent << 52) | precision;
} }
// adding two precision together
unsigned long long addition_precision_double(unsigned long long p1, unsigned long long p2) {
unsigned long long hinten = 0x00000000ffffffff;
unsigned int vornep1 = p1 >> 32, hintenp1 = p1 & hinten, vornep2 = p2 >> 32, hintenp2 = p2 & hinten;
return ((unsigned long long) addition_uint(vornep1, vornep2) << 32) + (unsigned long long) addition_uint(hintenp1, hintenp2);
}

2
src/addition.h

@ -31,4 +31,6 @@ unsigned long long exponent_double(unsigned long long number);
unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision); unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision);
unsigned long long addition_precision_double(unsigned long long p1, unsigned long long p2);
#endif // ADDITION_H #endif // ADDITION_H

2
test/test_addition.c

@ -8,6 +8,8 @@ num carry[1];
unsigned int e = 0x00800000; unsigned int e = 0x00800000;
unsigned long long ed = 0x0010000000000000;
union { union {
float floating; float floating;
unsigned int integer; unsigned int integer;

Loading…
Cancel
Save