Browse Source

addition number output with sign, exponent and precision

master
Dennis Sperzel 11 months ago
parent
commit
9454b90569
  1. 6
      src/addition.c
  2. 15
      test/test_addition.c

6
src/addition.c

@ -48,6 +48,12 @@ unsigned int e(unsigned int a) {
return (a & e) >> 23; return (a & e) >> 23;
} }
// writing number out of sign, exponent and precision
unsigned int o(unsigned int s, unsigned int e, unsigned int p) {
return (s << 31) | (e << 23) | p;
}
// addition of two floating numbers // addition of two floating numbers
float addition_float(float number1, float number2) { float addition_float(float number1, float number2) {

15
test/test_addition.c

@ -138,7 +138,8 @@ 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_signfloatingnumber(void)
{
unsigned int result[2]; unsigned int result[2];
unsigned int expected[2] = { 0, 1 }; unsigned int expected[2] = { 0, 1 };
number[0].floating = 1, number[1].floating = -1; number[0].floating = 1, number[1].floating = -1;
@ -150,7 +151,8 @@ 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_precisionfloatingnumber(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;
unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9}; unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9};
@ -168,7 +170,8 @@ 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_exponentfloatingnumber(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;
unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24}; unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24};
@ -186,7 +189,8 @@ 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_float_0plus0gleich0(void) {
void test_addition_addition_float_0plus0gleich0(void)
{
float result; float result;
float expected = (float) 0.0; float expected = (float) 0.0;
@ -195,7 +199,8 @@ void test_addition_addition_float_0plus0gleich0(void) {
TEST_ASSERT_EQUAL_FLOAT(expected, result); TEST_ASSERT_EQUAL_FLOAT(expected, result);
} }
void test_addition_addition_float_0plusnumbergleichnumber(void) {
void test_addition_addition_float_0plusnumbergleichnumber(void)
{
float result[5]; float result[5];
number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698; number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698;
float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating }; float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating };

Loading…
Cancel
Save