MORE INFORMATION ON sqrt.c




The file "sqrt.c" demonstrates an intent error due to type conflict. The function "square_root" takes one parameter and returns the square root of that value passed. For perfect squares such as 4, 16, 25, this function executes as desired. However, for values whose square root is not an integer, the exact root value will not be returned by the function. Instead, truncation occurs on any floating point result given and the function returns an integer.

This can be better illustrated in the following example; if x (where x is the single parameter value passed to square_root) = 100, the value returned by the function will be a very accurate 10. In the case where x = 120, however, the function will return a value of 10, very different from the actual value of 10.95. This could lead to severe miscalculations in the use of this function.

Below is a table to give a visual idea of this error, showing individual values of "x", and values returned by square_root (let us call each of these different values "y")


SAMPLE DATA

trial #xy Actual value of y
125 55
230 55.4772
31001010
41201010.9545

An efficient way of ratifying this error is to recast the x variable as a float. By so doing, the function will be able to efficiently return a float value for y if necessary.




Click here for the source code.