# Function Reliabilty

• August 16th, 2013, 12:11 AM
rockx
Function Reliabilty
Just a quick question. in
Code:

`<cmath>`
and
Code:

`<math.h>`
there is an
Code:

`sqrt`
function.

I just wish to know how reliable would it be. What i am asking is that, is the function constraint to any bounds, or is it universal.
• August 16th, 2013, 01:05 AM
Paul McKenzie
Re: Function Reliabilty
Quote:

Originally Posted by rockx
Just a quick question. in
Code:

`<cmath>`
and
Code:

`<math.h>`
there is an
Code:

`sqrt`
function.

I just wish to know how reliable would it be. What i am asking is that, is the function constraint to any bounds, or is it universal.

http://www.cplusplus.com/reference/cmath/sqrt/

Regards,

Paul McKenzie
• August 16th, 2013, 10:46 PM
rockx
Re: Function Reliabilty
It did not really answer my question
• August 16th, 2013, 11:08 PM
Paul McKenzie
Re: Function Reliabilty
Quote:

Originally Posted by rockx
It did not really answer my question

Because the question you asked was so vague, the only thing to do is give you the link to the documentation to the function. The description of sqrt() at the link tells you what it does, what it returns, and what happens if the argument is not valid. What else do you need to know? What do you mean by "reliable" or "universal"?
Quote:

is the function constraint to any bounds,
The function takes a double or float. Whatever the limits of a double or float are, that's what the constraints are. And again, if the number is deemed negative or invalid, then the link tells you what is returned. Everything you need to know is at that link.

Regards,

Paul McKenzie
• August 17th, 2013, 12:01 AM
rockx
Re: Function Reliabilty
what i was trying to ask was that. lets consider the size of all positive integers only. say if we get a 25-50 digit integer, would this function be able to operate wtih precisiion??
• August 17th, 2013, 01:53 AM
laserlight
Re: Function Reliabilty
Quote:

Originally Posted by rockx
lets consider the size of all positive integers only. say if we get a 25-50 digit integer

How are you going to get that? The unsigned long long type in C99, later incorporated into C++11, is guaranteed to provide at least a range of [0, 18446744073709551615], but I find it improbable that you will get a significantly larger range at this point of time. Hence, that means a maximum of 20 digits in decimal representation.

So, if you need to work with "a 25-50 digit integer", you might use an arbitrary precision integer library, which may provide a square root function that you can use with the integer type(s) provided.
• August 17th, 2013, 04:31 AM
Paul McKenzie
Re: Function Reliabilty
Quote:

Originally Posted by rockx
what i was trying to ask was that. lets consider the size of all positive integers only. say if we get a 25-50 digit integer,

What data type gives you a 50 digit integer?

Regards,

Paul McKenzie
• August 19th, 2013, 03:15 AM
zizz
Re: Function Reliabilty
Quote:

Originally Posted by rockx
what i was trying to ask was that. lets consider the size of all positive integers only. say if we get a 25-50 digit integer, would this function be able to operate wtih precisiion??

Since the sqrt function you're referring to belongs to the C++ standard library you can expect it to work within the bounds of all mathematically relevant types that belong to standard C++, like say float and double.

The C++ standard is carefully matched and balanced to avoid surprises but this also implies that no single feature, say sqrt, will very much exceed this core body of bounds. If you want that you need to look elsewhere. In this case say here,

http://gmplib.org/
• August 20th, 2013, 10:51 AM
OReubens
Re: Function Reliabilty
it'll work with full precision for all possible ranges of float and double in so far as it can give a proper result.
giving it bogus input (nan's, infinity, negatives) will give the defined outputs.

note that this says nothing about performance. Performance may or may not suffer for certain values depending on the algorithm used to calculate the result. typically for sqrt this isn't much of an issue as it's a fairly linearly solvable equation.