First, my environment:
Distributor ID: Bunsenlabs Description: BunsenLabs GNU/Linux 10.5 (Lithium) Release: 10.5 Codename: buster
Thread model: posix gcc version 8.3.0 (Debian 8.3.0-6)
I working on an old C program and ran into a rounding error in the program. I have copied the code into an the program "example.c" below:
main()
{
float acntBalance;
int dollars;
int cents;
float fcents;
dollars = 303466;
cents = 95;
fcents = cents * 0.01;
acntBalance = dollars + fcents;
printf("Dollars(%d) + Cents(%f) = %f \n",dollars,fcents,acntBalance);
}
and compiled this code using the GNU compiler as follows:
gcc -w -g -o example example.c
Adding 303466 dollars to 95 cents should be 303466.95, but prints out as 303466.937500.
This is an accounting program and being 1 cent off is not acceptable.
This looks like a bug to me, but it has been so long since I worked on a C program, so I will say "user error" is the mostly like problem here. But this seems so basic, that I don't see where I am making an error.
If the error is not mine, is it H/W or S/W. I have run the program on 2 different hosts, so it leads me to believe it is a S/W error. But where?
Can anyone see an error in my code?