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
Related
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 4 months ago.
i want to extract the decimal part of a float variable by substracting the whole part, the thing is i get a wrong value
#include<stdio.h>
int main(){
int k ;
float a=12.36,i;
k = (int)a;
i = a - k ;
i*=10;
printf("%f",i);
return 0;
}
well, the output is 3.599997 not 3.6 , is there a way to solve this ?
edit : i know it's because of the binary conversion, i m asking if there is a solution to get the right result, not the cause of it. ty for the replies anw.
edit 2 : sadly it's not a matter of display, i want the stored value to be 3.6 ( in this case) because i need it in other calculations.
This question already has answers here:
How to use % operator for float values in c
(6 answers)
Floating Point Modulo Operation
(4 answers)
Closed 2 years ago.
I need to reset the value of a variable called theta back to 0 everytime its value reaches or exceeds 2 PI. I was thinking something along the lines of:
int n = 10;
float inc = 2*PI/n;
for(int i=0;i<10;i++)
theta = (theta + inc) % 2*PI;
Of course it wont work because % doesn't work on floating points in C. Is there another equivalent or better way to achieve what I'm trying to do here? All replies are welcome. Thanks
Use the standard fmod function. See https://en.cppreference.com/w/c/numeric/math/fmod or 7.2.10 in the C17 standard.
The fmod functions return the value x − n y , for some integer n such that, if y is nonzero, the result
has the same sign as x and magnitude less than the magnitude of y.
So theta = fmod(theta, 2*PI) should be what you want, if I understand your question correctly.
If it really must be done on float instead of double, you can use fmodf instead.
Since division is really just repeated subtraction, you can get the remainder by checking if the value is at least 2*PI, and if so subtract that value.
int n = 10;
float inc = 2*PI/n;
for(int i=0;i<10;i++) {
theta += inc;
if (theta >= 2*PI) theta -= 2*PI;
}
Note that because the amount of the increment is less than the 2*PI limit we can do the "over" check just once. This is likely cheaper than the operations that would be involved if fmod was called. If it was more you would at least need while instead, or just use fmod.
This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
I have an array of double:
double theoretical_distribution[] = {1/21, 2/21, 3/21, 4/21, 5/21, 6/21};
And I am trying to computer it's entropy as:
double entropy = 0;
for (int i = 0; i < sizeof(theoretical_distribution)/sizeof(*theoretical_distribution); i++) {
entropy -= (theoretical_distribution[i] * (log10(theoretical_distribution[i])/log10(arity)));
}
However I am getting NaN, I have checked the part
(theoretical_distribution[i] * (log10(theoretical_distribution[i])/log10(arity)))
And found it to return NaN itself, so I assume it's the culprit, however all it's supposed to be is a simple base conversion of the log? Am I missing some detail about the maths of it?
Why is it evaluating to NaN.
You are passing 0 to the log10 function.
This is because your array theoretical_distribution is being populated with constant values that result from integer computations, all of which have a denominator larger than the numerator.
You probably intended floating computations, so make at least one of the numerator or denominator a floating constant.
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 7 years ago.
simple question I assume, but I just stumbled across this:
float y=2+2/3;
Output: 2
How come float cannot process 2/3?
My guess is that float interprets 2/3 as integers.
But how come it accepts 2 in the beginning?
That's integer division. You're basically computing:
float y = 2 + (2 / 3);
float y = 2 + (0 );
float y = 2;
Try:
float y = 2 + 2.0 / 3;
float y = 2 + (float)2 / 3;
Just typecast, it will also work.
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