8

Everyone knows sqrt function from math.h/cmath in C/C++ - it returns square root of its argument. Of course, it has to do it with some error, because not every number can be stored precisely. But am I guaranteed that the result has some precision? For example, 'it's the best approximation of square root that can be represented in the floating point type usedorif you calculate square of the result, it will be as close to initial argument as possible using the floating point type given`?

Does C/C++ standard have something about it?

yeputons
  • 8,478
  • 34
  • 67
  • 1
    http://stackoverflow.com/questions/20137105/c-sqrt-function-precision-for-full-squares – Mike Mar 07 '14 at 20:05
  • 1
    @Mike, thank you, but my question is a bit wider: I'm interested in what I'm guaranteed to have? Because if there are no guarantees at all, I can write a standard-compliant sqrt() which always returns 0 for positive numbers. – yeputons Mar 07 '14 at 20:07
  • 1
    @yeputons: See [this answer](http://stackoverflow.com/questions/20137105/c-sqrt-function-precision-for-full-squares/20137304#20137304) to the linked question -- the C/C++ standard gives no guarantees, but the IEEE-754 spec does – Chris Dodd Mar 07 '14 at 20:22
  • 1
    @ChrisDodd I see, that answers my question. Thank you very much. – yeputons Mar 07 '14 at 20:26
  • @yeputons Interesting you chose 0 to express your point in the comment. 0 has a curious `sqrt()` property. IEEE `sqrt(+0.0)` --> +0.0 and `sqrt(-0.0)` --> -0.0. – chux - Reinstate Monica Mar 07 '14 at 22:38
  • Standard libraries have been around since the 60s that guarantee precision to at least +/- the least significant fraction bit. Since several are public domain it would be foolish for a given runtime environment to not use one (though, as we know, fools are a dime a dozen). – Hot Licks Mar 09 '14 at 13:39

2 Answers2

9

For C99, there are no specific requirements. But most implementations try to support Annex F: IEC 60559 floating-point arithmetic as good as possible. It says:

An implementation that defines __STDC_IEC_559__ shall conform to the specifications in this annex.

And:

The sqrt functions in <math.h> provide the IEC 60559 square root operation.

IEC 60559 (equivalent to IEEE 754) says about basic operations like sqrt:

Except for binary <-> decimal conversion, each of the operations shall be performed as if it first produced an intermediate result correct to infinite precision and with unbounded range, and then coerced this intermediate result to fit in the destination's format.

The final step consists of rounding according to several rounding modes but the result must always be the closest representable value in the target precision.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
1

This question was already answered here as Chris Dodd noticed in the comments section. In short: it's not guaranteed by C++ standard, but IEEE-754 standard guarantees me that the result will be as close to the 'real result' as possible, i.e. error will be less than or equal to 1/2 unit-in-the-last-place. In particular, if the result can be precisely stored, it should be.

Community
  • 1
  • 1
yeputons
  • 8,478
  • 34
  • 67
  • I think the "1/2 unit-in-the-last-place" depends on rounding mode. In some rounding modes it will be "1 unit-in-the-last-place". In any case, it will be the best per rounding mode. – chux - Reinstate Monica Mar 07 '14 at 22:36