The problem is that in the part of the calculation where you use (1/2)
.
28: r=(1/2)*(ra+(num/ra));
When performing this arithmetic operation, the compiler interprets the operation as a division between integers, resulting in 0 .5 . Therefore, by throwing 0
as a result of that operation and multiplying it by the rest of the expression, the value of r
is converted to 0
.
In this Stack Overflow question in English you will see a better explanation of the topic:
Why is (1/2) * x different from 0.5 * x?
For the compiler to take it as a floating number, or with a decimal part, you have to change the values by:
(1/2)
per (1.0/2.0)
or
(1/2)
per 0.5
This topic is explained in the next chapter of the course:
9. Conversion of types.
In these cases, when the operands of each binary operation
associated with an operator are of different types, the compiler
convert to a common type. There are rules that govern these
conversions, and although they can change slightly from a compiler to
another, in general they will be more or less like this:
Any small integer type such as char or short is converted to int or unsigned int . At this point any pair of operands will be int (with or without a sign), long , long long , double , float or long double .
If one operand is long double , the other will be converted to long double .
If one operand is double , the other will be converted to double .
If one operand is float , the other will be converted to float .
If an operand is unsigned long long , the other will be converted to unsigned long long .
If one operand is long long , the other will be converted to long long .
If one operand is of type unsigned long , the other will be converted to unsigned long .
If one operand is long , the other will be converted to long .
If an operand is of type unsigned int , the other will be converted to unsigned int .
At this point both operands are int .
This case can be explained with an example of the course citation:
4. Operators I.
Let's see, for example, the expression 17/7, that is 17 divided between
7. When we learned to divide, before we knew how to draw decimals, we were taught that the result of this operation was 2, and
the rest 3, that is 2 * 7 + 3 = 17.
In C ++, when the expressions that intervene in one of these
operations are whole, the result will also be whole, that is,
if 17 and 7 are stored in whole variables, the result will be whole,
in this case 2.
On the other hand if the expressions are floating point, with decimals,
the result will be floating point, that is, 2.428571. In this case:
7 * 2.428571 = 16.999997, that is, there is no rest, or it is very small.
For that reason, calculating the rest, using the operator%, only has
meaning if the expressions involved are whole, since in case
otherwise as many decimals will be calculated as the precision of the
type used.
Continuing with the example, the expression 17% 7 will result in 3,
which is the rest of the entire division of 17 divided by 7.
Keeping your code correct:
#include<stdio.h>
#include<math.h>
float calculo_raiz(float,float);
int main()
{
float rta;
float numero;
float tol;
printf("\n\t\t ¿¿¿ AVERIGUA LA RAIZ CUADRADA ???\n\n" );
printf("Usted desea averiguar la raiz del numero:");
scanf("%f",&numero);
printf("\nIngrese la tolerancia que admitira en el resultado:" );
scanf("%f",&tol);
rta=calculo_raiz(numero,tol);
printf("\nLa raiz de %.2f es %.2f\n",numero,rta);
return 0;
}
float calculo_raiz(float num,float tolerancia)
{
float r=0;
float ra=1;
int inc=1;
while(inc>=1)
{
r=(1.0/2.0)*(ra+(num/ra)); // ó r=0.5*(ra+(num/ra));
if((r-ra)<tolerancia)
{
return r;
}
else
{
inc++;
ra=r;
}
}
}