This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
Closed 3 years ago.
The question look like this
https://i.imgur.com/u0LJO0g.png
where do i get wrong?
#include<stdio.h>
int main(){
int i,n;
int a[] = {3,5,7};
float x[] = {0,0,0};
printf("function f(x)=(x^3-2x^2+10x-5)/(x-10)\n");
for(i = 0;i<3;i++){
x[i] = (a[i]^3-2*(a[i]^2)+10*a[i]-5)/(a[i]-10);
}
for(n = 0;n<3;n++){
printf("if x is %d,f(x) is %f\n",a[n],x[n]);
}
}
I expect the output will look that this
if x is 3,f(x) is -5.14
if x is 5,f(x) is -24.00
if x is 7,f(x) is -103.33
but the actual output is
if x is 3,f(x) is -3.000000
if x is 5,f(x) is -7.000000
if x is 7,f(x) is -20.000000
^ is XOR in C, not exponentiation.
If you do math on ints you're going to get int results. You'll need to cast some of those a[i] to float or double to get floating point arithmetic.
Related
This question already has answers here:
Unsigned values in C
(3 answers)
Closed 9 months ago.
I ran into an interesting scenario with integer conversion:
#include <stdio.h>
int main()
{
unsigned int x = 20;
unsigned int y = 40;
printf("%d\n", x - y);
printf("%d\n", (x - y) / 4);
}
~ % ./a.out
-20
1073741819
I wasn't expecting the 2nd result. Since x and y are both unsigned ints is the result of x - y unsigned (and in this case displayed as signed by printf)?
The things you are printing are indeed unsigned integers and you should print them with %u, but that does not explain the surprising result for the second number. The surprising result comes from an overflow occurring when you calculate x - y, since the result of that subtraction is negative and thus not representable as an unsigned int.
Unsigned overflow/underflow is not undefined behavior, so it's OK to have code like this in production if you know what you are doing.
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 6 years ago.
after compiling this code,I get k=0. shouldn't it be k=0.8?
what's wrong with the code?
#include <stdio.h>
#include <math.h>
void main()
{
int x=8;
int y=10;
int m=6;
float k;
k=x/y;
printf("k=%f",k);
}
Although you are assigning the result of the division to a float, the result itself is computed in integers. This is because both operands are of type int.
There are multiple ways of fixing this problem - for example, by assigning the dividend to k, and then dividing it by the divisor, like this:
int x=8;
int y=10;
int m=6;
float k = x;
k /= y;
printf("k=%f",k);
Type casting would be more useful and simple.
int x=8;
int y=10;
int m=6;
float k;
k=(float)x/y;
printf("k=%f",k);
This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 7 years ago.
I have the following program:
float x = 3.e17;
printf("x = %f", x);
which gives the result:
x = 299999995292024832.000000
why is the result not 300000000000000000.000000?
#include <stdio.h>
#include <stdlib.h>
union
{
double d;
float f;
unsigned int ui[2];
} myun;
int main ( void )
{
float fx = 3.e17;
double dx = 3.e17;
printf("fx %f\n",fx);
printf("dx %lf\n",dx);
myun.f=fx;
printf("fx 0x%08X\n",myun.ui[0]);
myun.ui[0]++;
printf("fx %lf\n",myun.f);
myun.d=dx;
printf("dx 0x%08X 0x%08X\n",myun.ui[1],myun.ui[0]);
return(0);
}
(yes this is an incorrect/invalid way to use a union but it just happened to work)
fx 299999995292024832.000000
dx 300000000000000000.000000
fx 0x5C853A0D
fx 300000029651763200.000000
dx 0x4390A741 0xA4627800
wikipedia points out that single can handle up to 9 digits without a loss of precision and double 15-17. So right there is your answer, didnt necessarily need to do an experiment.
Because of limited float precision. Use double for more precision, but floating point is not always exact
This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 7 years ago.
I've written a code for the following program but the output seems to be wrong.
Question:
https://www.hackerrank.com/challenges/plus-minus
Code:
#include <stdio.h>
int main() {
int N,num,i,cp=0,cn=0,cz=0;
double fp,fn,fz;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&num);
if(num>0)
cp=cp+1;
else if(num==0)
cz=cz+1;
else
cn=cn+1;
}
fp=cp/N;
fn=cn/N;
fz=cz/N;
printf("%lf\n%lf\n%lf",fp,fn,fz);
return 0;
}
The Output comes as:
0.000000
0.000000
0.000000
Istructions:
fp=cp/N;
fn=cn/N;
fz=cz/N;
Are performed as integer division.
Change your code to:
fp=(double)(cp)/(double)(N);
fn=(double)(cn)/(double)(N);
fz=(double)(cz)/(double)(N);
you are doing an integer division which creates only integer results. If you want to calculate floating point results you need to perform floating point divisions.
int a = 1;
int b = 3;
int c = a / b;
// c is now 0 -> integer division
double i = 1.0;
double j = 3.0;
double k = i / j;
// k is now 0.3333333 -> floating point division
For correct result cast these expression's to double-
like this
fp=cp/N; // fp=(double)cp/N;
fn=cn/N; // fn=(double)cn/N;
fz=cz/N; // fz=(double)cz/N;
Working code
In previous case if cp(or cn or cz) is less than N then due to integer division you will get 0(fraction part will be discarded). Therefore m these casts .
Another method would be to use all variables as double .
This question already has answers here:
Floating point inaccuracy examples
(7 answers)
Closed 8 years ago.
How can i can calaulate this in c? :
float x = 5;
float y = 4.999
float z = x-y; // 0.001000
Now i want z to be exactly 3 digits after the point, so that z will be = 0.001.
I don't need to print z, i just need to initialize it with 0.001.
Use the integer datatype and transform all numbers to a multiply of 1000
int x = 5000
int y = 4999
int z = x-y // 1