Somehow I get wrong result from equation which involves library function abs(). Here's my code where the issue is present:
main.cpp -------------------------------------
#include <iostream>
#include <cmath>
#include "test999.h"
using namespace std;
int main()
{
float n = 11;
ak r;
r = test(n);
printf("\n");
for(int i = 0; i < n; i++){
printf("r.yp[%d] = %3.7f \n", i, r.yp[i]);
}
return 0;
}
test999.h ---------------------------------------
struct ak{
float yp[20];
};
ak test(int n){
float u[] = {0, 0, 0, 0, 0, 0, 0, 0.5, 4.5, 35, 10, 25, 40, 55};
ak r;
float a, b;
for(int i = 0; i < n; i++){
a = abs(u[i+3] - u[i+2]);
b = abs(u[i+1] - u[i]);
printf("i = %d a = u[%d](%2.4f) - u[%d](%2.4f) = %2.4f \n b = u[%d]-u[%d] = %2.4f (%2.4f - %2.4f) \n", i, i+3, u[i+3], i+2, u[i+2], a, i+1, u[i+1], i, u[i], b);
if ((a+b) != 0.0f){
r.yp[i] = (a * u[i+1] + b * u[i+2]) / (a+b);
}
else{
r.yp[i] = (u[i+2] + u[i+1]) / 2.0f;
}
printf("yp[%d] = %2.4f \n\n", i, r.yp[i]);
}
return r;
}
and it results wrong value for equation of abs(0.5)-0.0 (case i=4) and abs(35.0)-4.5 (case i=6):
Changing equation to use std::abs() calculates correctly:
If I move everything to main.cpp module both cases works so, is it just that because of I have made defect code ... or rounding from 0.5 to 0.0 happened somewhere I should have taken into account... ?