I am supposed to write code that will show that if you square root a number and get an irrational result, then square that result that the result won't be the original value because of the imprecise nature of the float value. The trouble is...I haven't been able to reproduce that, in the following code numberToSqrRt ends up having the same value as result.
This is supposed to be fairly simple, the 7th program we're supposed to write. I can use setprecision to get the desired results, but I'm not expected to. Can anyone see what I am doing wrong?
float numberToSqrRt = 7919.0f; // I always fully specify a FP number
float theSquareRoot = std::sqrt(numberToSqrRt); // the std version takes various types
float result = numberToSqrRt * numberToSqrRt;float result = theSquareRoot * theSquareRoot; // use the correct variable
cout << result << endl;
The use of the f on line 33 helps prevent unnecessary implicit casting: 7919 is an int, 7919.0 is a double, 7919.0f is a float.