utility/src/mathUtility.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file mathUtility.cpp | ||
| 3 | * | ||
| 4 | * @brief Provides functions for math. utilities | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <cstring> | ||
| 8 | |||
| 9 | #include "../header/mathUtility.h" | ||
| 10 | |||
| 11 | using namespace Coruh::Utility; | ||
| 12 | |||
| 13 | |||
| 14 | 1 | double MathUtility::calculateMean(const double data[], int datalen) { | |
| 15 | 1 | double sum = 0.0; | |
| 16 | |||
| 17 |
2/2✓ Branch 4 → 3 taken 5 times.
✓ Branch 4 → 5 taken 1 time.
|
6 | for (int i = 0; i < datalen; ++i) { |
| 18 | 5 | sum += data[i]; | |
| 19 | } | ||
| 20 | |||
| 21 | 1 | return sum / datalen; | |
| 22 | } | ||
| 23 | |||
| 24 | 2 | double MathUtility::calculateMedian(const double data[], int datalen) { | |
| 25 | |||
| 26 | //TODO: Move this memory allocation to control function | ||
| 27 |
1/2✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
|
2 | double* sortedData = new double[datalen]; |
| 28 | 2 | double median = 0; | |
| 29 | |||
| 30 | 2 | memcpy(sortedData, data, sizeof(double) * datalen); | |
| 31 | |||
| 32 | 2 | qsort(sortedData, datalen, sizeof(double), compareDouble); | |
| 33 | |||
| 34 |
2/2✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 1 time.
|
2 | if (datalen % 2 == 0) { |
| 35 | 1 | int index1 = datalen / 2 - 1; | |
| 36 | 1 | int index2 = datalen / 2; | |
| 37 | 1 | median = (sortedData[index1] + sortedData[index2]) / 2.0; | |
| 38 | }else { | ||
| 39 | 1 | int index = datalen / 2; | |
| 40 | 1 | median = sortedData[index]; | |
| 41 | } | ||
| 42 | |||
| 43 |
1/2✓ Branch 10 → 11 taken 2 times.
✗ Branch 10 → 12 not taken.
|
2 | delete[] sortedData; |
| 44 | 2 | return median; | |
| 45 | } | ||
| 46 | |||
| 47 | 12 | int MathUtility::compareDouble(const void* a, const void* b) { | |
| 48 | |||
| 49 | 12 | double val1 = *(const double*)a; | |
| 50 | 12 | double val2 = *(const double*)b; | |
| 51 | |||
| 52 |
2/2✓ Branch 2 → 3 taken 10 times.
✓ Branch 2 → 4 taken 2 times.
|
12 | if (val1 < val2) { |
| 53 | 10 | return -1; | |
| 54 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
|
2 | }else if (val1 > val2) { |
| 55 | 1 | return 1; | |
| 56 | }else { | ||
| 57 | 1 | return 0; | |
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | 2 | void MathUtility::calculateMinMax(const double data[], int datalen, double* min, double* max) { | |
| 62 | |||
| 63 | 2 | *min = data[0]; | |
| 64 | 2 | *max = data[0]; | |
| 65 | |||
| 66 |
2/2✓ Branch 8 → 3 taken 8 times.
✓ Branch 8 → 9 taken 2 times.
|
10 | for (int i = 1; i < datalen; ++i) { |
| 67 | |||
| 68 |
2/2✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 5 times.
|
8 | if (data[i] < *min) { |
| 69 | 3 | *min = data[i]; | |
| 70 | } | ||
| 71 | |||
| 72 |
2/2✓ Branch 5 → 6 taken 5 times.
✓ Branch 5 → 7 taken 3 times.
|
8 | if (data[i] > *max) { |
| 73 | 5 | *max = data[i]; | |
| 74 | } | ||
| 75 | } | ||
| 76 | 2 | } | |
| 77 | |||
| 78 |