Simple division of 2/12 with double causing errors - c

I don't know what is causing this error, I try dividing 2/12 using double but it gives me a completely wrong number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a;
a = 2/12;
printf("\n a = %lf \n", a);
return 0;
}
This returns -272632568 and makes no sense. Thanks for your help.

First of all, you should consider the declaration:
double a = 2 / 12; // (integer)
It'll just give you zero and then assign to double since they're integers. If you use rather:
double a = 2.0 / 12.0; // explicitly defining
Then you'll get right precision output by using this statement:
printf("\n a = %f\n", a);
Or,
printf("\n a = %lf\n", a);

You used %d in your format string, however %d means integer not double.
You want to use %f inplace of %d
See here for details: https://www.gnu.org/software/libc/manual/html_node/Table-of-Output-Conversions.html#Table-of-Output-Conversions

Related

Is there something in C that prevent my large numbers from turning into "6.95278e-310". I would like the full number to be printed [duplicate]

How can I display a double like
5000683
Instead of 5.000683e6 in C?
I have tried %d, %g and %f, but to no avail.
It looks like %f works just fine:
#include <stdio.h>
int main()
{
double d = 5000683;
printf("%f\n", d);
printf("%.0f\n", d);
return 0;
}
The output of this code will be
5000683.000000
5000683
The second printf() statement sets the precision to 0 (by prefixing f with .0) to avoid any digits after the decimal point.

Adding doubles in ansi C produces unexpected results

For my school project we are required to do math with doubles. My current code produces some unexpected results.
/* Hello World program */
#include<stdio.h>
int main()
{
double result = 0.0;
double x;
x = 10.0;
result = x + 10.0;
printf("%d", result);
return 0;
}
Upon running, this code prints: "-1267258024"
I don't understand why this happens? Why does the code not print 20.0?
Thanks!
EDIT: I'm so dumb. %d is for floats. Thank you!
The line
printf("%d", result);
indicates that you want to print an integer.
You probably want
printf("%f", result);
There are also things like %lf (acts the same as %f) and %Lf (works for long doubles) which you can read about on this answer.
A complete list of formatting options can be found here.

Using pow() in C

I have this problem with this homework I'm supposed to do.
[ It says Create a program that's able to calculate and show the sum of S]
Like S=1+1/4+1/8+1/16 ... till 1/ [2 pow n]
So I worked on it and came up with this code
#include <stdio.h>
void main()
{
int n,i;
float p,s;
printf("Enter the maximum power n :");
scanf("%d",&n);
s=0;
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",&s);
}
But when I execute it is always like S=0.
What am I doing wrong?
You are printing an address ('&s) with%f` variable. Using a wrong specifier invokes undefined behavior. You may get anything.
Also, No need of variable s. Remove the line
s+=p;
It should be like:
#include <stdio.h>
int main(void)
{
int n,i;
float p;
printf("Enter the maximum power n :");
scanf("%d",&n);
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
printf("p = %f\n",p);
}
printf("The sum of this equation is :%f",p);
}
You need to include <math.h> to get the proper prototype of pow().
You might need to link to the math library too gcc main.c -Wall -lm
#include <math.h>
....
for (i=0;i<n;i++)
{
p=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",s);
Your program has multiple problems. Enabling compiler warnings should tell you about some of them.
You should include the C header which contains the declaration of the pow function.
You add each addend twice.
In your second printf statement, you pass a float. But the %f format specifier expects a double argument. In your third printf statement, you pass a pointer to a float.
Another cosmetic problem is that your main function should return an int.
just a guess, may you replace the line
p+=1/pow(2, i);
with
p+=1.0f/(float)pow(2, i);
and
printf("The sum of this equation is :%f",&s);
with
printf("The sum of this equation is :%f",s);
Typo may be.. but you will have say %f and s (not &s)
printf("The sum of this equation is :%f",s);
On side note:
Once you include <math.h> you will get compiler warning for using correct pow(..) prototype. Below code would be relevant.
p+=1.0f/(float)pow(2.0f, i);

Using '/' with long double?

I am trying to understand why using '/' with long double in the following way leads to a 0.000000 value while the same code with double does not
double d = (double)total_results / (double)total_points;
Gives the value 0.785403 but
long double d = (long double)total_results / (long double)total_points;
Gives the value 0.000000. I am trying to get the most accurate value for 'total_results / total_points'
EDIT: In the end the error was simply that I was outputting it using '%f' instead of '%Lf'
Before
printf("Running on %d thread(s), results is %f.\n", NUM_THREADS, d);
After
printf("Running on %d thread(s), results is %Lf.\n", NUM_THREADS, d);
This is obviously just a guess, but how are you outputting the results? If you're using printf with the wrong field specifier, printing an erroneous zero is definitely a possible result. Using g++, I tried "%lf" and got "-2.0000" when I should have gotten "0.75". The right specifier is "%Lf", with a capital L.

C complex number and printf

How to print ( with printf ) complex number? For example, if I have this code:
#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex dc1 = 3 + 2*I;
double complex dc2 = 4 + 5*I;
double complex result;
result = dc1 + dc2;
printf(" ??? \n", result);
return 0;
}
..what conversion specifiers ( or something else ) should I use instead "???"
printf("%f + i%f\n", creal(result), cimag(result));
I don't believe there's a specific format specifier for the C99 complex type.
Let %+f choose the correct sign for you for imaginary part:
printf("%f%+fi\n", crealf(I), cimagf(I));
Output:
0.000000+1.000000i
Note that i is at the end.
Because the complex number is stored as two real numbers back-to-back in memory, doing
printf("%g + i%g\n", result);
will work as well, but generates compiler warnings with gcc because the type and number of parameters doesn't match the format. I do this in a pinch when debugging but don't do it in production code.
Using GNU C, this works:
printf("%f %f\n", complexnum);
Or, if you want a suffix of "i" printed after the imaginary part:
printf("%f %fi\n", complexnum);

Resources