C++ sqrt function precision for full squares

Rafi Kamal

Let, x is an integer and y = x * x.

Then is it guaranteed that sqrt(y) == x?

For example, can I be sure that sqrt(25) or sqrt(25.0) will return 5.0, not 5.0000000003 or 4.999999998 ?

Joe Z

No, you cannot be guaranteed. For integers and their squares that fit in the dynamic range of the floating point type's mantissa (2^53 for a typical C/C++ double), you're likely to be OK, but not necessarily guaranteed.

You should avoid equals comparisons between floating point values and exact values, especially exact integer values. Floating point rounding modes and other such things can really get in your way.

You either want to use a "comparison range" to accept an "approximately equal" result, or recast your algorithm in terms of integers. There are multiple StackOverflow questions covering floating point equality comparisons. I suggest you search for them and read up.

For a certain class of problem, I wrote up an alternate solution here: Find n-th root of all numbers within an interval

That solution took a different approach than relying on tricky floating point arithmetic.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Guaranteed precision of sqrt function in C/C++

From Dev

Guaranteed precision of sqrt function in C/C++

From Dev

Numerical precision for difference of squares

From Dev

domain_error in c in sqrt() function

From Dev

Precision of acos function in c++

From Dev

Precision of acos function in c++

From Dev

Full precision display of floating point numbers in C++?

From Dev

C++ How to write full precision of doubles into a .dat file

From Dev

Full precision display of floating point numbers in C++?

From Dev

C++ How to write full precision of doubles into a .dat file

From Dev

Getting: error C2668: 'sqrt' : ambiguous call to overloaded function

From Dev

sqrt() function not working in c what is my math error?

From Dev

C: datatypes. sqrt function working with int why?

From Dev

Applying sqrt function on a column

From Dev

Sqrt function invokes an error

From Dev

Python Squares Function

From Dev

BC—automatic full precision multiplication

From Dev

Lua Sqrt function inaccurate result

From Java

C++ Function full specialization giving error

From Dev

jQuery function makes responsive squares

From Dev

Double precision in FORMAT function

From Dev

Double precision in FORMAT function

From Dev

LOCALTIMESTAMP function with precision parameter

From Dev

Set the mtime of a file with full microsecond precision in python

From Dev

How to format a double with full precision in Java?

From Dev

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

From Dev

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

From Dev

Decidable sqrt function in Z3

From Dev

Finding square root without using sqrt function?

Related Related

HotTag

Archive