1

I dont understand why setprecision(2) not working when using if else statement

I tried doing this and it displays the else statement. I dont see any problem, maybe im using setprecision() wrong? I even displayed the quotient to prove that the if statement should be the one running.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float x = 2;
    float y = 3;
    float quotient, answer;
    quotient = x / y;
    cout << fixed << setprecision(2);
    cout << quotient << " (is the answer)\n";
    cout << " What is " << x << " divided by " << y << " ? ";
    cin >> answer; // answer should be 0.67
    
     if (quotient == answer)
     cout << " You got the right answer! ";
     else
     cout << " Nice Try :( ";

    return 0;
}
Ervin Pejo
  • 13
  • 3
  • 1
    Are you expecting `setprecision` to change the actual value of `quotient`? – cigien Nov 11 '22 at 16:10
  • [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) and [Division of two numbers always returns an integer value](https://stackoverflow.com/questions/44973406/division-of-two-numbers-always-returns-an-integer-value) – Jason Nov 11 '22 at 16:19
  • I changed the x and y to float data type now but it still wont do the if statement... – Ervin Pejo Nov 11 '22 at 16:24
  • yes I want to change the actual value of the quotient. Its just that there are some division problems where the answer is infinite so I need to avoid that and thought setprecision is the right thing to do – Ervin Pejo Nov 11 '22 at 16:26
  • 2
    @ErvinPejo No you misunderstood what `setprecision` does. It changes the way numbers are printed, not the way that calculations are done. – john Nov 11 '22 at 16:30
  • Sorry I am still new with c++ and dont know other way to change the value of the quotient to only 2 decimal places and I cant use the round() since I need the 2 decimal places – Ervin Pejo Nov 11 '22 at 16:34

1 Answers1

0

The line

quotient = x / y;

will assign the value of 0.0 to the variable quotient, because 2/3 is 0, using the rules of integer division. Therefore, if the user enters 0.67, this value will not compare equal to 0.0.

If you want the division 2/3 to evaluate to something like 0.6666666667, then you must make at least one of the operands a floating-point number, for example by using a cast:

quotient = static_cast<float>(x) / y;

However, even if you did this, your comparison would still not work, because the expression

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

will only change the way the variable quotient is printed. It will not change the actual value of the variable.

In order to round the actual value of the variable, you can use the function std::round. Note that this will only round to the nearest integer, so if you want to round to the nearest multiple of 0.01, then you will first have to multiply the number by 100 before performing the rounding operation. If you want, you can then divide the number by 100 again, to get the original number again, rounded to the nearest multiple of 0.01.

However, you should be aware that these operations may introduce slight floating-point inaccuracies. For this reason, it may be better to not require an exact match in the comparison

if (quotient == answer)

but to consider a deviation of up to 0.01 to still be considered a match. You can do this for example by changing the expression to this:

if ( std::abs( quotient - answer ) < 0.01 )

Note that you will have to #include <cmath> in order to use std::round and std::abs.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • I changed x and y to float now but yeah like what you said it wont work. Is there a way to change the value of the quotient to 2 decimal places only? because I am making a game where you will solve arithmetic equations but there are just some division problems where the answer is infinite or too long. any solution? – Ervin Pejo Nov 11 '22 at 16:23
  • @ErvinPejo: I have now edited my question to provide further information. I believe that this addtional information answers the question that you asked in your previous comment. – Andreas Wenzel Nov 11 '22 at 16:41