0

I am studying Qt, I find it really amazing and a bit easier because I've already used MFC in Microsoft.

I've developed a scientific calculator for educational use. But what faced me is:

I want float high precision like in real-world calculators so after searching I've found this function:

    long double ldRes =  std::sqrt(3);
    QMessageBox m;
    m.settext(QString::number(ldRes, 'g', 30));
    m.exec();

The output:

1.73205080756887719317660412344

While the output, according the The Casio Calculator, of the same operation to the same precision, should be:

1.73205080756887729352744634151

So it is apparent that the result is not the same so can anyone advise me how to get correct float-precision in my applications using Qt? or any other library?

Should I use GMP MPIR MPFR?

Thank you!

Maestro
  • 2,512
  • 9
  • 24

1 Answers1

5

When you use std::sqrt and pass a double (well, an int, which will end up being converted to double), the result is a double.

Converting that result to long double achieves nothing but gaining a ton of low-value bits with no relation to the result of the square-root operation!

I don't know what "other calculators" you're using, but something about the difference between the two approaches leads to those different results.

std::sqrtl works with long doubles, or you can pass a long double argument to std::sqrt which picks the right overload.

// Effectively equivalent:
long double a = std::sqrtl(3);
long double b = std::sqrtl(3);
long double c = std::sqrt(3.0L);

Now, your calculation has the precision of a long double from end to end, and ought to be mathematically correct to that level of precision.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • Converting a `double` to a `long double` does not “[gain] a ton of low-value bits” except for zero bits. A conversion to a more precise type does not change the value. – Eric Postpischil Mar 26 '20 at 10:44
  • @EricPostpischil What? You said it yourself: zero bits (by "low-value" I'm referring to their low magnitude, the fact that they are on the least-significant side of the overall value). And those zero bits are _wrong_. They make the result mathematically incorrect. No, the value isn't different from what it was as a `double`, but it _is_ different from what it will be in any tool that gives that mathematical result to that precision correctly. And that's what I said: _"achieves nothing but gaining a ton of low-value bits **with no relation to the result of the square-root operation**"_ – Asteroids With Wings Mar 26 '20 at 11:07
  • The language in the answer should be more precise and explicit. As humans use language, “digits” or “bits” may refer to either to significant non-zero digits or bits or to formal bits in a representation. This is particularly so in the case of floating-point discussion, where people sometimes refer to unexpected or incompletely understood values has having various noise or “randomness” in the low bits. Even if you are using language in a specific deliberate way, readers cannot know that unless they are told or at least there are indications of it. – Eric Postpischil Mar 26 '20 at 11:33
  • @EricPostpischil Feel free to write your own. I stand by my wording - nobody else seems to have misinterpreted it. But I do appreciate your feedback. Take care! – Asteroids With Wings Mar 26 '20 at 15:19