0
#include <iostream>
#include <iomanip>


using namespace std;


int main()
{
    setlocale(LC_ALL, "RU");
    float P_a2, P_b2,result2,x,y,z;

    cout << "P_a(x)= 9,09*x^9– 9,09*x^3+ 9,09*x\n";
    cout << "P_b(x)= – 8980,032*x^6– 186,34*x^4– 649,23*x^2\n\n";

    x = 1.2;

    cout << "\n";

    y = x * x * x * x * x * x;

    P_a2 = ((9.09 * y - 9.09) * x * x + 9.09) * x;

    cout << setprecision(9) << P_a2 << "\n\n";


    z = x * x;

    P_b2 = ((-8980.032 * z - 186.34) * z - 649.23) * z;
    
    cout << setprecision(9) << P_b2 << "\n\n";
    result2 = P_a2 * P_b2;
    cout <<fixed<< setprecision(15) << result2 << "\n\n"; //prints -1184587.00000000

   
    //right answer is -1 184 586,9806370984
    
}

Is this problem related with type of the value? I really cant understand why it happens and what should i do... Can somebody please explain why does it happens with result2 and can i avoid it without changing the type?

P.S my bad, i've forgot to add the minus, right answer ofc is -1 184 586,9806370984

P.P.S yes, i know that this code can be optimized, but our teacher said us to do so. And yes parentheses are correct.

I know i can fix it by using double, i'm just asking if i can solve this problem without using it

Dwen
  • 1
  • 1
  • Probably going to come down to [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user4581301 Sep 17 '21 at 18:37
  • 1
    You seem to ask for a high precision result (for example the `setprecision(15)` argument), but you are using `float` type which can only encode 7 decimals. Use `double` values. – prapin Sep 17 '21 at 18:52
  • If you use double you get better results, still, for such levels of precision these are not what you shoud use. – anastaciu Sep 17 '21 at 18:54
  • No, you can't 'solve' it without using a bigger precision. The precision of each intermediate result of every multiplication and addition your program performs is limited by the length of the floating-point values representation. – CiaPan Sep 17 '21 at 19:05
  • You can check the precision of the floating point type, q.v.: https://stackoverflow.com/a/50970282/4641116 – Eljay Sep 17 '21 at 19:09
  • Then could you show more details about the acceptance criteria for "solving" the problem. 1) Does that mean using 2 or 3 floating point variables to hold different part of the result? 2) Does that mean we can rearrange P_a2 * P_b2 into different combination of terms? 3) What class are you taking and in which chapter, so that may indicate the kind of approaches or mindset one should take. – Chen Sep 18 '21 at 02:42

1 Answers1

1

There are two issues (not c++ issues, more of general floating point numerical calculation issues):

  1. float type is usually 6-7 decimal digits precision, using double would give you 15-16 decimal digits, more helpful for the problem. Seems like you need more digits than float to at least hold the result.

  2. There is a subtraction in P_a, so should watch for catastrophic cancellation when grouping the terms.

Chen
  • 188
  • 2
  • 11