This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 7 years ago.
Why does output equal zero in this code?
Number 1 with weight of 2, number two with weight of 3 and number three with weight of 5. I can not understand why output = 0.
#include <stdio.h>
int main ()
{
float A ,B, C ,MEDIA=0 ;
scanf("%f%f%f",&A ,&B,&C);
MEDIA+=1/2*A + 1/3*B + 1/5*C;
printf("MEDIA = %.1f", MEDIA );
return 0;
}
MEDIA+=1/2*A + 1/3*B + 1/5*C;
Because 1/2, 1/3 and 1/5 will be evaluated as 0. Since they are integers.
Either write
1.0/2, 1.0/3 and 1.0/5 instead. So the compiler will know to treat the result as float.
Or
MEDIA+=A/2 + B/3 + C/5;
P.S.
Maybe I am wrong but if I understood correctly what you wrote at the description then I think your calculation of the weighted average is incorrect. It should be something like
(A*2 + B*3 + C*5)/10
Related
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 3 months ago.
#include <stdio.h>
typedef struct ElapsedTime_struct {
int decadesVal;
int yearsVal;
} ElapsedTime;
ElapsedTime ConvertToDecadesAndYears(int totalYears) {
ElapsedTime tempVal;
tempVal.decadesVal = totalYears/10;
tempVal.yearsVal = (((double) totalYears/10) - tempVal.decadesVal) * 10;
return tempVal;
}
int main(void) {
ElapsedTime elapsedYears;
int totalYears;
scanf("%d", &totalYears);
elapsedYears = ConvertToDecadesAndYears(totalYears);
printf("%d decades and %d years\n", elapsedYears.decadesVal, elapsedYears.yearsVal);
return 0;
}
my logic is that if you take number of total years( 26) and divide the integer value by 10, you will get the number of decades. int (26/10) = 2
my logic for the number of years is that if you take the double value of 26/10, you will get 2.6000
then subtracting 2.6 - 2 (number of decades), you will get the decimal value for the number of years(0.6)
i then multiplied by 10 to get a whole value (6) so together its 2 decades and 6 years.
however when i try running the code for some inputs (34 total years) i am getting 3 decades and 3 years for some reason whereas i should get 3 decades and 4 years.
i am confused as to why this is happening.
Floating point math is inexact. Values such as 2.6 and 3.3 cannot be exactly represented in binary floating point. You instead end up with a value that is either slightly larger or slightly smaller.
The latter case is what you're seeing. 3.4 is stored as roughly 3.39999999999999991. Subtracting 3 from that and multiplying by 10 gives you 3.9999999999999991, then when you convert that to an int the fractional part is truncated giving you 3.
That being said, you don't need floating point operations here. You can instead use the modulus operator % to get the remainder when dividing by 10.
tempVal.yearsVal = totalYears%10;
This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 4 years ago.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,i;
double abc,sum=0;
printf("Enter a value for x:");
scanf("%d",&x);
for(i=0;i<=6;i++)
{
abc=pow(1/2,i)*pow((x-1)/x,i+1);
sum=sum+abc;
}
printf("Sum is %f\n",sum);
}
As what i have checked there is no overflow of values in data type either or is it something else?
The issue is in this line of code:
abc=pow(1/2, i) * pow((x-1) / x, i + 1);
1/2 is always zero, and (x - 1)/x is also zero when x > 0. You can use 0.5 or 1.0 / 2.0 if you'd like to use a decimal value. Also, be careful about dividing by zero.
The resulting code would look like this:
abc=pow(0.5, i) * pow((x - 1.0)/x, i + 1.0);
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
What I am trying to do here is, I am taking a precision value (less than 1) and printing count of all the numbers of type 1/n (n is natural number) which are greater than or equal to the precision value (input).
#include <stdio.h>
int main(){
int i=1,terms=0;
float n;
printf("Enter precision value : ");
scanf("%f",&n);
while(i>0){
if ((1.0/i) >= n){
printf("%f\n",1.0/i);
sum = sum + (1.0/i);
terms++;
i++;
}
else
break;
}
printf("number of terms : %d\n",terms);
return 0;
}
But if I give input 0.1 than the count (output) is showing only 9. but it should show 10 (it is not including 1/10).
I was using a for loop before and I know that breaking a loop with an if-else statement is not the best thing.
That's just the way math works with limited precision. For example, say you're using six digits of decimal precision. The best you can do for one-third is 0.333333 and the best you can do for 2/3 is 0.666667, but then 2 * 1/3 will not equal 2/3. And 3 * 1/3 will not equal 1. But 1/3 + 2/3 will.
Just as 1/3 cannot be expressed exactly in decimal, 0.1 cannot be expressed exactly in binary. So testing for equivalence is not smart.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I tried to sum some numbers in a for loop but it didn't go as I expected
float sum = 0;
int i;
printf("0.1+0.1=%f\n", 0.1 + 0.1);
for (i = 0; i<1000000; i++)
{
sum = sum + 0.1;
}
printf("the sum need to be 100000 \n");
printf("the real sum is:\n %f\n", sum);
system("PAUSE");
this program prints:
0.1+0.1=0.200000
the sum need to be 100000
the real sum is:
100958.343750
Press any key to continue . . .
can you explain please this strange result?
the international standard for floating point numbers does not have an exact representation for some decimal numbers.
http://en.wikipedia.org/wiki/IEEE_754
It is due to the way they are stored in memory, the way the mantissa and exponent are stored.
https://en.wikipedia.org/wiki/Floating_point
This is also the reason why you should never compare two float numbers even if they look "the same".
I still remember how surprised I was the fist time a simple code comparing two float numbers didn't work :) This alone would open a dedicated universe of discussions. It is very worth reading anyway:
http://floating-point-gui.de/errors/comparison/
The floating numbers are stored in memory as x*2^y where x is between 0 and 1 with some precision and y is integer and so they accurately don't represent most numbers, they represent numbers "close enough".
When you do this addition multiple times, the error is just more visible.
You can use double type for better accuracy.
This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 8 years ago.
I have this little program in C that calculates the square root x of a positive integer N using a recursive function (implemented using a while loop). If I calculate x using this:
x = (1/2)*(x + N/x) //x0 = 1.0
Then x keeps growing to inf and then nan. However if I use this:
x = (x + N/x)/2 //x0 = 1.0
It works fine, why? Thanks.
1/2 does integer division, its result is 0, change either or both operand to double, e.g:
1.0/2