Integer variables in C [duplicate] - c

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Why does division result in zero instead of a decimal?
(5 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 2 years ago.
I am trying to write a code that finds pi accurately. When I run my code I get an output of 1.000000 every time because n always equals 0 in the equation to find add and minus after the first equation but when I print it, n is increasing as it should. I don't get any error messages because there are no visible problems with the code that I have. This is a conversion from the same code I wrote in python and I don't know if that is meaning I have forgotten something.
#include <stdio.h>
int main()
{
int n = 1, iterations, times;
long double pi = 0.0, add, minus;
printf("Number of iterations: ");
scanf("%d", &iterations);
for (times = 1; times <= iterations; ++times)
{
add = 1 / n;
printf("%Lf \n", add);
pi = pi + add;
n = n + 2;
minus = 1 / n;
printf("%Lf \n", minus);
pi = pi - minus;
n = n + 2;
printf("%d \n", n);
printf("%Lf \n", pi);
}
pi *= 4;
printf("Pi = %Lf \n", pi);
return 0;
}

Works a treat if you define n as a double.
When n is defined as an integer 'minus = 1 / n;' will always be 0, except when n = 1;

Related

Can't seem to add summation in while loop [duplicate]

This question already has answers here:
Division in c not giving expected value
(4 answers)
Closed 29 days ago.
I was making a code that adds a summation for a specific formula, but the sum is always 0 for some reason. What is the reason nothing is adding? I think maybe it is the declaration of int and double for the variables. When I do a simple equation, such as adding natural numbers, it works, but not for more complicated equations like the one below.
Code:
#include <stdio.h>
int main()
{
int i, n;
double sum = 0;
printf("Enter the max value for the sum: ");
scanf("%d", &n);
i = 1;
while(i <= n)
{
sum = sum + (1 / ((1 + i) * (1 + i)));
i++;
}
printf("Sum = %f\n", sum);
}
I tried the code pasted above, expected the correct sum, but resulted in only 0.0000.
In this statement
sum = sum + (1 / ((1 + i) * (1 + i)));
the sub-expression (1 / ((1 + i) * (1 + i))) uses the integer arithmetic. It means that if to divide 1 by any integer number greater than 1 the result will be equal to 0.
Consider this simple demonstration program
#include <stdio.h>
int main( void )
{
int i = 1;
i = i / 2;
printf( "i = %d\n", i );
}
Its output is
i = 0
You need to use the arithmetic with float numbers.
It will be enough to write
sum += 1.0 / ((1 + i) * (1 + i));
Or it will be even more better to write using long long int constant 1ll within the expression like
sum += 1.0 / ((1ll + i) * (1ll + i));
to avoid overflow for the integer multiplication.
Also as the range is specified as two positive numbers then it will be logically better to specify the variables i and n as having the type unsigned int.
You need to cast the result of the divison as a double since that is what you are expecting as a result. Int division will round it to closest whole number

C program returning incorrect double value [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 4 months ago.
Code:
int main(){
double x = 10;
double Fahrenheit = (x * (9/5)) + 32;
double Celsius = (5 / 9) * (x - 32);
printf("%lf\n", Fahrenheit);
printf("%lf\n", Celsius);
return 0;
}
output:
42.000000
-0.000000
I have tried re-arranging the formulas, I have also tried using functions but I can't seem to figure out why the ouput is incorrect.
For reference the expected output:
50.00000
-12.22222
re-arranging to:
double Fahrenheit = (x * 9 / 5) + 32;
double Celsius = (x - 32) * 5 / 9;
worked!
but so did adding a .0 to all int numbers
The problem is that you are performing the computation as ints (9/5) == 1 when (9.0 / 5.0) == 1.8.
Add .0 to all your numbers and you'll be fine

Not the right result with Arithmetic Progression in C [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 8 years ago.
Im trying to sum the multiples of a number (x0) with a progression number (r) and a number of times (n). If I use the number x0 = 6, r = 3, n = 3, the result should be
6+9+12=27, but the program gives me always 18.
I try different times changing the formula but if I do on the paper the result is right, so Im afraid the problem can be the syntax...
So theres the program in C:
#include <stdio.h>
int sum_progression(int x0, int r, int n)
{
return (n/2) * ((2 * x0) + ((n - 1) * (r)));
}
void test_sum_progression(void)
{
int x0;
int r;
int n;
scanf("%d", &x0);
scanf("%d", &r);
scanf("%d", &n);
int z = sum_progression(x0,r,n);
printf("%d\n", z);
}
int main(void)
{
test_sum_progression();
return 0;
}
Thanks for helping!
When using ints with division the value is calculated and then truncated to int.
if you divide int by int you should do something like:
return (n/(double)2) * ((2 * x0) + ((n - 1) * (r)));

10k by 10k matrix malloc curiousity in c [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to sum a large number of float number?
I have a matrix 'x' that is 10,000 elements by 10,000 elements.
In the first case I declare the matrix like:
int n = 10000;
unsigned int size_M = n*n;
unsigned int mem_size_M = sizeof(int)*size_M;
int* x = (int*)malloc(mem_size_M);
Step (1) The matrix is initialized:
for(i=0;i<n;i++)
for(j=0;j<n;j++)
x[i*n+j] = 1;
Step (2) Sum the elements of the matrix and print the total:
for(i=0i<n;i++)
for(j=0j<n;j++)
sum +=x[i*n+j];
printf("sum: %d \n", sum);
As I would expect the above code prints 'sum: 100000000 '.
However if I declare the matrix like:
int n = 10000;
float size_M = n * n;
float mem_size_M = sizeof(float) * size_M;
float* x = (float*)malloc(mem_size_M);
And again perform the steps 1 and 2 the correct answer is not printed out, but '16777216' instead. Why is this?
ANSWER: To get the appropriate answer do a type conversion...
sum +=(int)x[i*n+j];
This happens because of the precision limitations of the float type. You can't just add 1.0 to float with value > 16777216 (2^24), but you can add 2.0, or 0.1:
#include <stdio.h>
int main(void)
{
float f = 16777220;
printf("f = %f\n", f + 1);
printf("f = %f\n", f + 2);
printf("f = %f\n", f + 0.1);
return 0;
}
The IEEE-754 standard floating-point numbers have have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. It's a bit complicated to explain precisely why it happens, but I can say that this is a extreme case when operation error reaches its maximum.

Dividing 1/n always returns 0.0 [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Why does division result in zero instead of a decimal?
(5 answers)
Closed 4 years ago.
I am trying to calculate p1=(1/1)*(1/2)*...*(1/n) but something is wrong and the printf gives me 0.000...0
#include <stdio.h>
int main(void) {
int i,num;
float p3;
do {
printf ("give number N>3 : \n" );
scanf( "%d", &num );
} while( num <= 3 );
i = 1;
p3 = 1;
do {
p3=p3*(1/i);
printf( "%f\n",p3 );
} while ( i <= num );
printf("\nP3=%f",p3);
return 0;
}
(1/i)
i is an int, so that's integer division, resulting in 0 if i > 1. Use 1.0/i to get floating point division.
1 is an integer, i is an integer. So 1/i will be an integer, ie the result will be truncated. To perform floating-point division, one of the operands shall be of type float (or, better, of type double):
p3 *= 1. / i;
I had the same issue. The basic case:
when you want to get float output from two integers, you need to convert one into float
int c = 15;
int b = 8;
printf("result is float %f\n", c / (float) b); // result is float 1.875000
printf("result is float %f\n", (float) c / b); // result is float 1.875000

Resources