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.
Related
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
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.
I know this is a simple questions, but it came up when I was coding and I am wondering how it works now. So, my first question is that when printf is given an integer like below, but expecting a %f float value, why is it always outputting 0.000000? I am running this in GCC on linux terminal.
int main() {
int a = 2, b = 5, result = 0;
result = b/a*a;
printf("%f\n", result);
}
//Above printf statement outputs 0.000000 every time.
Then when I use the code below and give printf a double when it is expecting an %i integer value, the output is always random/garbage.
int main() {
double a = 2, b = 5, result = 0;
result = b/a*a;
printf("%i\n", result);
}
//Above printf statement outputs random numbers every time.
I completely understand the above code is incorrect since the printf output type is not the same as I am inputting, but I expected it to act the same way for each form of error instead of changing like this. Just caught my curiosity so I thought I would ask.
Basically because if you interpret the bits that make up a small integer as if they were a double, it looks like the double value for 0. Whereas if you interpret the bits that represent a small double value as an integer, it looks like something more interesting. Here is a link to a page that describes how the bits are used to represent a double: http://en.m.wikipedia.org/wiki/IEEE_floating_point . With this link and a little patience, you can actually work out the integer value that a given double would be interpreted as.
You used the wrong format specifiers
It should be
int a = 2, b = 5, result = 0;
result = b/a*a;
printf("%d\n", result);
...
double a = 2, b = 5, result = 0;
result = b/a*a;
printf("%f\n", result);
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.
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);