1

So when I input a decimal number like 12.45, it gets decremented by 0.00001 or something that causes my function to work badly.

For example: If x is 12.45 and div is 0.1 when watching x you can see that it becomes 12.449999999

BUT If x is 12.455 and div is 0.01 it doesn't reduce x

double round(double x, double div){
if (div != 0){
double temp2 = 0;
int temp = x;
double dec = x - temp;
dec = dec/div;
temp = dec;
temp2 = dec-temp;
temp2 = temp2 * div;
cout << x << endl << endl;
if (temp2 >= div/2){
    x+=(div-temp2);
}else{
    x-=temp2;
}
cout << temp << "  " << dec << "  " << x << "  " << temp2 << "  " << div/2;
return x;
   }else{
    cout << "div cant be equal to zero" << endl;
}
}

I was trying to make a function that rounds up decimal numbers. I know its probably not the best to do it, but it works except the problem I described earlier.

To fix it I tried limiting decimal places at the input, didn't work. Also tried using other methods instead of using a double/integer combo without any results.

I expect the output of 12.5 when x is 12.45 and div is 0.1 but it's not working, because of the 0.000001 of the input getting lost.

GoRgo 3
  • 13
  • 3

1 Answers1

1

Your program is going to be miss informed and will not work.

This is how floating point values are handled in programming languages as is defined in this standard.
https://en.wikipedia.org/wiki/IEEE_754#Basic_formats

They often require rounding as a result of an operation to fit within their finite representation making them difficult to compare.
https://www.boost.org/doc/libs/1_36_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html

The issues you are seeing are artifacts of the rounding error.
https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf

Bryan
  • 441
  • 2
  • 8
  • 1
    My constant quibble: floating-point math is exact on its own terms. It does not represent real numbers, so the intuition people have developed over the years about arithmetic in the real world is often misleading. – Pete Becker Aug 03 '19 at 20:37
  • So I have to use a rounding function to make a rounding function? How as the first rounding function made then? – GoRgo 3 Aug 04 '19 at 17:15