1

I am going through a C++ course and am asked to make a simple cashier program with change in dollars and cents, separately.

In doing so, I came across an instance where if I cout my calculation of cout<<change*100 - dollars*100<<endl; I get 40 cents correctly.

But when I set the int cents = change * 100 - dollars * 100; Then cout<<cents<<endl; I get 39.

Is this because of the data types I am using have some unintended consequences that I'm not aware of? Here is the whole program:

#include <iostream>
using namespace std;

int main()
{
    double price, paymentAmount, change;
    int dollars, cents;

    price = 23.00;
    paymentAmount = 24.40;

    cout<<"total: "<<price<<endl;
    cout<<"paid: "<<paymentAmount<<endl;

    change = paymentAmount-price;

    dollars = change; // implicit conversion from double -> int
    cents = change * 100 - dollars * 100;

    cout<<"dollars: "<<dollars<<endl;
    cout<<"cents: "<<change*100 - dollars*100<<endl; // outputs 40
    cout<<"cents: "<<cents<<endl; // outputs 39

    return 0;
}

Thanks for the help

Edit:

Turns out this is from converting the double to int, which truncates the decimals. My number was something like 39.99999 but was truncated to 39 when converting to int. I think that conversion aspect makes this a non duplicate question

danielito
  • 63
  • 7
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – phuclv Jun 03 '17 at 15:59
  • For future reference, never use standard floating point types and arithmetics when dealing with money. It might be enough for a simple beginners exercise but once you're past being a beginner don't do it. – Some programmer dude Jun 03 '17 at 16:12
  • Save the currency as cents in an integer/long long instead, to avoid loss of information, caused by the floating point format using large numbers or periodic cents (because 1/10 or 1/100 cannot be exactly stored in binary firmat). – cmdLP Jun 03 '17 at 18:38
  • I don't think this is a duplicate since the issue is due to the conversion to int, resulting in the truncation rather than rounding – danielito Jun 03 '17 at 21:20

2 Answers2

0

The problem is that change*100 - dollars*100 is a floating point expression of type double. This will be rounded when you output it.

But when you assign it to an integer variable, the floating point value is truncated, where the decimals are simply cut away. So if you have something like 39.9 that will be rounded to 40 and truncated to 39.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, I think this is a clearer answer for a beginner because it highlights the rounding vs truncating – danielito Jun 03 '17 at 21:32
0

It is related to decimal point precision. In line cents = change * 100 - dollars * 100; precision is lost because result is converted into int. But in line cout<<"cents: "<<change*100 - dollars*100<<endl; result is in double so precision is not lost.

You can verify it by following statement

double cent2=change * 100 - dollars * 100;
cout<<cent2<<endl;
cse
  • 4,066
  • 2
  • 20
  • 37