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.
Related
This question already has answers here:
C program division error?
(7 answers)
Closed 3 years ago.
I am working with STM32 and GCC Compiler and I make the following division:
uint8_t w, h;
w=2;
h=5;
float test = (w * h) / 8;
and the result is test=1
Why am I wrong? I cannot understand this behavior.
Could someone explain me the reason?
(w * h) / 8 is an integer expression with an integer result.
The assignment to float is an implicit cast, but will not recover the lost fractional part.
You can make the division a floating point expression by ensuring that at least one of the operands is floating point:
float test = (w * h) / 8.0f ;
You need to do the following modification if you want to get the correct result:
uint8_t w, h;
w = 2;
h = 5;
float test = (float)(w * h) / 8;
If you divide integers, you get an integer as a result.
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.
This question already has answers here:
How to get fractions in an integer division?
(2 answers)
What is the behavior of integer division?
(6 answers)
Closed 7 years ago.
#include <stdio.h>
int main()
{
int x;
float f1[11], f2[11], s;
for (x = 1; x <= 10; x++)
{
f1[x] = (x * x) / 4;
printf("f1(%d)=%.2f\n", x, f1[x]);
}
return 0;
}
f1 = (x^2)/4
Current output is:
f1(1) = 0.00
f1(2) = 1.00
f1(3) = 2.00 //etc
I kinda want those decimals though.
f1(1) = 0.25
f1(2) = 1.00
f1(3) = 2.25
What you do with a value doesn't affect how it's computed. So the fact that you store the result of some math in a double doesn't change the fact that you're performing integer operations.
There are lots of fixes, but the simplest is probably (1.0 * x * x) / 4.0.
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
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