sqrt(4) == 4^(0.5)
Added by: ~Paul, 2010 V 06
| Type: |
other
|
| Status: |
closed
|
| Progress: |
100
|
sqrt(4) == 4^(0.5) I think this should come out as true. The thing is they both show the same answer of "2" in
the online calculator for the higher precision calculations.
Added by: tomek, 2010 X 14, Last modified: 2010 X 14
The first problem is that 4^(0.5) gives not exactly decimal 2.
If you print the value as binary (the option is not available in the online calculator) but you can check it with
this simple program:
#include <ttmath/ttmath.h>
#include <iostream>
typedef ttmath::Big<TTMATH_BITS(256), TTMATH_BITS(2048)> Float;
int main()
{
Float a, b;
a = 4;
b = "0.5";
a.Pow(b);
std::cout << "dec: " << a << std::endl;
std::cout << "bin: " << a.ToString(2) << std::endl;
}
You see that the binary result is:
10.00000000000000000000[..../a lot of zeroes/.....]001
At the end of the mantissa you have one bit set. This innacuracy comes from the nature of binary floating point
numbers. 4^(0.5) is calculated by using a logarithm and exponent which are themselves calculated from some kind
of series (a lot of mathematical operations: addind, dividing etc). So at the end you are only with an approximate
result.
But why sqrt(4) is different? Because it returns exactly 2 (binary: 10). It is a feature implemented in sqrt function.
1. sqrt first calculates 4^(0.5)
2. because 4 is integer the sqrt function rounded the result to the nearest integer so:
(binary)10.000000000....1 becames: binary 10
3. the result binary 10 is squared: 10 * 10 giving: binary 100 which is exactly decimal 4, so sqrt as the result
returns binary 10.
Now you can ask why 4^(0.5) is not printed as something like:
2.000000000000000000.....001
The simple answer is it is not so easy to implement. When printing such values in other words converting from binary
to decimal we have to do some mathematical operations like calculating a logarithm, dividing etc. All those operations
cause losing accuracy.
In the other hand it helps to print such values:
decimal: 0.1 which don't have finite binary representation.