Using '/' with long double? - c

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.

Related

Simple division of 2/12 with double causing errors

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

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.

Please help me to understand these error

#include<stdio.h>
float func (float t, float y){
return y ;
}
int main (){
float t0,y0,t,y;
printf ("the value of t: ");
scanf ("%f",&t0);
printf ("the value of y: ");
scanf ("%f",&y0);
t=t0;
y=y0;
static int n=0;
// t[0]=t0;
// y[0]=y0;
for (n=0;n<=3;n++){
y[1]=y[0];
printf ("value of y %f %f \n",t,y);
}
return 0;
}
The error is:
Building prog.obj.
D:\master\c language\ch3\prog.c(166): warning #2117: Old-style function definition for 'main'.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
*** Error code: 1 ***
You cannot array index something that is not an array, or a pointer into an array.
Your y and t floats are not pointers into arrays in your program.
You should make them float *y, *t into pointers so you can point them into array.
Change float t0,y0,t,y; to float t0,y0,*t,*y;
and
t=&t0; //assign address of t0 to t
y=&y0;
Change printf ("value of y %f %f \n",t,y); to
printf ("value of y %f %f \n",*t,*y); //note to dereference t and y here, to get their values
Here's a example of your program I fixed to work
The 'Old-style function definition for main()' message means that you've not given a prototype definition. The correct forms are:
int main(void) { ... }
int main(int argc, char **argv) { ... }
The version int main() is fine in C++, but not strictly a prototype in C, and hence gets the 'old-style' tag.
The other messages are more inscrutable; the line numbers do not correspond to the code you show. However, as Tony The Lion notes in his answer, the line
y[1] = y[0];
is erroneous since y is not an array. There is room to think that should be:
y = y0;
and you'd need a companion:
t = t0;
in order to have defined values printed in the printf() statement.
Even with these changes, the code does not make a lot of sense. However, given that you removed 150-odd lines, we can suppose that the missing code would make more sense.
There is no need to make n into a static variable; it is better not to do so.
Please make sure, in future, that your error messages correspond to the source code you post, not to some variant version of the code you post. The line numbers should not be as large as 166 or 182; they should be single digit numbers or small double digit numbers. But even more importantly, they should match the code!

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