LCOV - code coverage report
Current view: top level - utility\src\mathUtility.cpp - mathUtility.cpp (source / functions) Hit Total Coverage
Test: calculator library documentation coverage - genhtml (Windows) Lines: 1 1 100.0 %
Date: Wed Sep 30 03:07:43 2026 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1           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             : double MathUtility::calculateMean(const double data[], int datalen) {
      15             :         double sum = 0.0;
      16             : 
      17             :         for (int i = 0; i < datalen; ++i) {
      18             :                 sum += data[i];
      19             :         }
      20             : 
      21             :         return sum / datalen;
      22             : }
      23             : 
      24             : double MathUtility::calculateMedian(const double data[], int datalen) {
      25             : 
      26             :         //TODO: Move this memory allocation to control function
      27             :         double* sortedData = new double[datalen];
      28             :         double median = 0;
      29             : 
      30             :         memcpy(sortedData, data, sizeof(double) * datalen);
      31             : 
      32             :         qsort(sortedData, datalen, sizeof(double), compareDouble);
      33             : 
      34             :         if (datalen % 2 == 0) {
      35             :                 int index1 = datalen / 2 - 1;
      36             :                 int index2 = datalen / 2;
      37             :                 median = (sortedData[index1] + sortedData[index2]) / 2.0;
      38             :         }else {
      39             :                 int index = datalen / 2;
      40             :                 median = sortedData[index];
      41             :         }
      42             : 
      43             :         delete[] sortedData;
      44             :         return median;
      45             : }
      46             : 
      47             : int MathUtility::compareDouble(const void* a, const void* b) {
      48             :         
      49             :         double val1 = *(const double*)a;
      50             :         double val2 = *(const double*)b;
      51             : 
      52             :         if (val1 < val2) {
      53             :                 return -1;
      54             :         }else if (val1 > val2) {
      55             :                 return 1;
      56             :         }else {
      57             :                 return 0;
      58             :         }
      59             : }
      60             : 
      61             : void MathUtility::calculateMinMax(const double data[], int datalen, double* min, double* max) {
      62             :         
      63             :         *min = data[0];
      64             :         *max = data[0];
      65             : 
      66             :         for (int i = 1; i < datalen; ++i) {
      67             :         
      68             :                 if (data[i] < *min) {
      69             :                         *min = data[i];
      70             :                 }
      71             :         
      72             :                 if (data[i] > *max) {
      73             :                         *max = data[i];
      74             :                 }
      75             :         }
      76             : }
      77             : 

Generated by: LCOV version 1.15.alpha0w