0
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
//Declaration of Variables
    float TotalCollected; // Total Amount Collected from sales
    double sales; // Sale excluding tax
    float CountyTaxRate; // Tax rate for County
    float StatesTaxRate; // Tax rate for state

    cout << setprecision(2) << fixed << showpoint;

//We get data from the user
    cout << "What is the Total Collected Amount? ";
    cin >> TotalCollected;
    cout << "What is the County Tax rate? "; 
    cin >> CountyTaxRate;
    cout << "What is the State Tax rate ";
    cin >> StatesTaxRate;

//Calculations
    sales = TotalCollected / (1 + StatesTaxRate + CountyTaxRate); 

//Output    
    cout << "Sales: $" << sales << endl;
    return 0;
}

County tax rate= .02 state tax rate=.04
total collected amount= 26572.89

The output should be 25068.76 but I keep getting 25068.77.

Tas
  • 6,589
  • 3
  • 31
  • 47
  • 2
    *Answer is rounding when it shouldn't C++* -- No, floating point math doesn't work that way. [See this](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – PaulMcKenzie Feb 01 '17 at 23:49
  • For starters, store all your data as doubles instead of floats – RyanP Feb 01 '17 at 23:49
  • Also, there is no need to double space between lines of code. It just makes the code harder to read. – PaulMcKenzie Feb 01 '17 at 23:51
  • 2
    Setting precision to 10 I get this: Sales: $25068.7656250000. Following @RyanP 's advice I get Sales: $25068.7641509434, so yes. The lower precision of `float` is getting you. But instead of using `double`s, consider doing the math in pennies and sticking to integers. Fewer surprises, especially if you have to perform comparisons. – user4581301 Feb 02 '17 at 00:03
  • I find that there is suffient whitespace between the lines, a good amount but not too much @PaulMcKenzie. It divides out the program into neat parts, making it easier to read. – BusyProgrammer Feb 02 '17 at 00:45
  • @ABusyProgrammer -- Putting spaces between each and every line does not make the program easier to read to most programmers. First, we're scrolling up and down for no reason, second, there is no clarity between logical parts of the program, i.e. `for` loop blocks. Double space where it makes sense, not willy-nilly and every single line. – PaulMcKenzie Feb 02 '17 at 03:59

2 Answers2

1

To be honest, the problem you are having here is that you are using the type float, and not double. This, in effect, causes your decimal values to be less precise.

If I set the precision to 3 without changing the data type, then I get the number: 25068.765. However, if I change the data type to double and keep the precision at 3, then I get the number 25068.764.

Now given the 2 floating-point values, we would expect the preecision of the one stored in the variable of type double to be the correct number.

Therefore, you should set the data type of your variables to double, and NOT float. This should fix the problem at hand.

Hope this helps.

BusyProgrammer
  • 2,645
  • 5
  • 16
  • 30
1

It's probably rarely a good idea to use floating point numbers for high precision arithmetic, and 32-bit float is especially bad. It has a precision of only 24 bits, which roughly means 6 to 7 decimal digits. Also if you perform certain arithmetic operations (division for example) on floating point numbers, the result is going to be even less precise.

So if you want to keep things simple, use double. It offers a precision of about 53 bits, which can represent roughly 16 to 17 decimal digits.

Or slightly better use integer types like long long and treat it as cents. Integer arithmetic is far more precise and predictable.

Lastly you can always use a high precision library, though I think double or long long will be good enough for you.

uNiverselEgacy
  • 319
  • 3
  • 11