Browse Source

addition reading sign out of double number

master
Dennis Sperzel 11 months ago
parent
commit
c151871e6c
  1. 15
      src/addition.c
  2. 4
      src/addition.h
  3. 18
      test/test_addition.c

15
src/addition.c

@ -14,6 +14,16 @@ struct data
unsigned int precision; unsigned int precision;
}; };
struct datad {
union {
double dnum;
unsigned long lnum;
} number;
unsigned long sign;
unsigned long exponent;
unsigned long precision;
};
// full_adder is an implementation of two bit addition with carry // full_adder is an implementation of two bit addition with carry
void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) { void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) {
@ -99,4 +109,9 @@ float addition_float(float number1, float number2) {
num3.number.integer = output_float(num1.sign, num3.exponent, num3.precision); num3.number.integer = output_float(num1.sign, num3.exponent, num3.precision);
return num3.number.floating; return num3.number.floating;
}
unsigned long sign_double(unsigned long a) {
if (a & 0x8000000000000000 == 0x8000000000000000) return 1;
else return 0;
} }

4
src/addition.h

@ -5,6 +5,8 @@ typedef unsigned int num;
struct data; struct data;
struct datad;
void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry);
unsigned int addition_uint(num number1, num number2); unsigned int addition_uint(num number1, num number2);
@ -21,4 +23,6 @@ unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int
float addition_float(float number1, float number2); float addition_float(float number1, float number2);
unsigned long sign_double(unsigned long a);
#endif // ADDITION_H #endif // ADDITION_H

18
test/test_addition.c

@ -82,7 +82,7 @@ void test_addition_full_adder_completesumwithcarry(void)
TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]); TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]);
} }
void test_addition_addition_basecasezeropluszeroequalzero(void)
void test_addition_addition_uint_basecasezeropluszeroequalzero(void)
{ {
unsigned int result; unsigned int result;
unsigned int expected = 0; unsigned int expected = 0;
@ -92,7 +92,7 @@ void test_addition_addition_basecasezeropluszeroequalzero(void)
TEST_ASSERT_EQUAL_UINT(expected, result); TEST_ASSERT_EQUAL_UINT(expected, result);
} }
void test_addition_addition_basecaseonescolumns(void)
void test_addition_addition_uint_basecaseonescolumns(void)
{ {
unsigned int result[2]; unsigned int result[2];
unsigned int expected[2] = { 1, 1 }; unsigned int expected[2] = { 1, 1 };
@ -104,7 +104,7 @@ void test_addition_addition_basecaseonescolumns(void)
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
} }
void test_addition_addition_basecasetotenscolumns(void)
void test_addition_addition_uint_basecasetotenscolumns(void)
{ {
unsigned int result[5]; unsigned int result[5];
unsigned int expected[5] = { 3, 3, 2, 3, 3 }; unsigned int expected[5] = { 3, 3, 2, 3, 3 };
@ -122,7 +122,7 @@ void test_addition_addition_basecasetotenscolumns(void)
TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
} }
void test_addition_addition_allunsignedinteger(void)
void test_addition_addition_uint_allunsignedinteger(void)
{ {
unsigned int result[5]; unsigned int result[5];
unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 }; unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 };
@ -140,7 +140,7 @@ void test_addition_addition_allunsignedinteger(void)
TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
} }
void test_addition_signfloatingnumber(void)
void test_addition_sign_floatingnumber(void)
{ {
unsigned int result[2]; unsigned int result[2];
unsigned int expected[2] = { 0, 1 }; unsigned int expected[2] = { 0, 1 };
@ -153,7 +153,7 @@ void test_addition_signfloatingnumber(void)
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
} }
void test_addition_precisionfloatingnumber(void)
void test_addition_precision_floatingnumber(void)
{ {
unsigned int result[5]; unsigned int result[5];
number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123; number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123;
@ -172,7 +172,7 @@ void test_addition_precisionfloatingnumber(void)
TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
} }
void test_addition_exponentfloatingnumber(void)
void test_addition_exponent_floatingnumber(void)
{ {
unsigned int result[5]; unsigned int result[5];
number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943; number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943;
@ -191,7 +191,7 @@ void test_addition_exponentfloatingnumber(void)
TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
} }
void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void)
void test_addition_addition_precision_float_positivnumberlowerthan2tothepowerof24(void)
{ {
unsigned int result[2]; unsigned int result[2];
number[0].floating = 652468761.2348, number[1].floating = 2.675312546943, number[2].floating = 321647956212.2146, number[3].floating = 0.00000324567861, number[4].floating = number[0].floating + number[1].floating; number[5].floating = number[2].floating + number[3].floating; number[0].floating = 652468761.2348, number[1].floating = 2.675312546943, number[2].floating = 321647956212.2146, number[3].floating = 0.00000324567861, number[4].floating = number[0].floating + number[1].floating; number[5].floating = number[2].floating + number[3].floating;
@ -211,7 +211,7 @@ void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
} }
void test_addition_output_numberwithsignexponentprecision(void)
void test_addition_output_float_numberwithsignexponentprecision(void)
{ {
unsigned int result[5]; unsigned int result[5];
number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14; number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14;

Loading…
Cancel
Save