float with 3 digits after the point precision [duplicate] - c

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

Related

why my array show in integer instead of float [duplicate]

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.

Float Division in C acting weird? [duplicate]

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.

Data type in C, loss of data [duplicate]

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.

How to generate random number within x-y range, if x and y values varies [language C] [duplicate]

This question already has answers here:
Generating random number between [-1, 1] in C?
(7 answers)
How to generate a random integer number from within a range
(11 answers)
Closed 8 years ago.
How to generate random number within x-y range in C
where
X : [t , t+m] and Y : [r , r+m ].
That is , if x varies from t to t+n and y varies from r to r+n.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
double x = 1.0;
double y = 2.0;
srand(time(NULL));
// Guarenateed keep x1 between x and y.
double x1 = x + rand()*(y-x)/RAND_MAX;
printf("x1: %lf\n", x1);
return 0;
}
Basically you seed the generator with operation system ticks count, modulo your generated number with the upper range and add the lower range to the result.

C multiplication error [duplicate]

This question already has answers here:
Why can't decimal numbers be represented exactly in binary?
(22 answers)
Closed 8 years ago.
Why is it that when I run the C code
float x = 4.2
int y = 0
y = x*100
printf("%i\n", y);
I get 419 back? Shouldn't it be 420?
This has me stumped.
To illustrate, look at the intermediate values:
int main()
{
float x = 4.2;
int y;
printf("x = %f\n", x);
printf("x * 100 = %f\n", x * 100);
y = x * 100;
printf("y = %i\n", y);
return 0;
}
x = 4.200000 // Original x
x * 100 = 419.999981 // Floating point multiplication precision
y = 419 // Assign to int truncates
Per #Lutzi's excellent suggestion, this is more clearly illustrated if we print all the float values with precision that is higher than they represent:
...
printf("x = %.20f\n", x);
printf("x * 100 = %.20f\n", x * 100);
...
And then you can see that the value assigned to x isn't perfectly precise to start with:
x = 4.19999980926513671875
x * 100 = 419.99998092651367187500
y = 419
A floating point number is stored as an approximate value - not the exact floating point value. It has a representation due to which the result gets truncated when you convert it into an integer. You can see more information about the representation here.
This is an example representation of a single precision floating point number :
float isn't large enough to store 4.2 precisely. If you print x with enough precision you'll probably see it come out as 4.19999995 or so. Multiplying by 100 yields 419.999995 and the integer assignment truncates (rounds down). It should work if you make x a double.
4.2 is not in the finite number space of a float, so the system uses the closest possible approximation, which is slightly below 4.2. If you now multiply this with 100 (which is an exact float), you get 419.99something. printf()ing this with %i performs not rounding, but truncation - so you get 419.

Resources