# Why does rounding cents in greedy always give the inputted float as integer?

Assume input is 4.10 This is saved as 4.09 (I checked) Now round(4.09*100) = 410

If the float would have been saved as 4.04 then rounded value would have been 400 ,but this never happens why?

• Do you have a code example? Can you post code with your question? – Wafeeq Jun 10 '16 at 17:00

round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function. If decimal value is from ”.1 to .5″, it returns integer value less than the argument. If decimal value is from “.6 to .9″, it returns the integer value greater than the argument.

round of 5.400000 is 5

round of 5.600000 is 6

I think that's than you questions, so is the implementation of round, and so it works ...

EDIT

If I understand your question, you want to know how the numbers with decimals are rounded. Surely you know what is the nature of real numbers, there is a subset, called irrational numbers whose decimal expression is infinite and nonrecurring, such as pi, e, log2 etc.. when operating with these numbers in computers or calculators, we must always stay with a finite set, that is where comes into play rounding, usually mathematicians use a method called normal rounding,it is easy to find references on the web. Usually also used computers and calculators (by the way computers only know how to add, so to compute complex functions such as logarithms, use series expansions such as Taylor). To show you how the above method works, I wrote a small program, with different approaches, sure you'll understand quickly.

``````#include <stdio.h>

# define PI 3.141592653589793238462643383279502884

int main(void)
{
long double x = PI;

printf("PI: %Lg\n", x);
printf("PI: %Lf\n", x);
printf("PI: %1.7Lf\n", x);
printf("PI: %1.10Lf\n", x);
return 0;
}
``````

Output:

``````PI: 3.14159
PI: 3.141593
PI: 3.1415927
PI: 3.1415926536
``````

I hope to clarify something your questions

• But is there some pattern to imprecision of floating values? because 4.49(decimal) may save as 4.5(decimal) which on rounding will give 5(which will result in faulty output ,ie, no. of coins) However it always saves as some value x(decimal) which follows: round(x*100)=449(no decimal). Plz elaborate ! I want to know about X(decimal) not 449(no decimal) – taichouvik Jun 14 '16 at 8:42
• @taichouvik you can read my EDIT – MARS Jun 14 '16 at 19:13