-
Double Precision Problem
Hello guys,
I'm new on C. And I have problems with double precision.
Whenever I build & run the code, the output is:
mass of electron is -0.000000
(I use Code::Blocks)
Code:
#include <stdio.h>
#include <stdlib.h>
main()
{
long double me=9.1094e-31;
printf("mass of electron is %Lf\n",me);
}
With my best regards, and thanks
-
You need to print out more decimal places. C only uses 6 places after the decimal by default. Try "%.35Lf". That gives you 35 digits after the decimal. Adjust as needed.
EDIT: Also, check out this article (link). Understanding the limits of floating point precision will be very handy when dealing with scientific calculations like yours, since you are working with such large exponents (whether positive or negative).
-
Problem was solved. Thank you
Code:
#include <stdio.h>
#include <stdlib.h>
main()
{
double me=9.1094e-31;
printf("mass of electron is %.40lf\n",me);
}
-
Check the edit in my previous post, there's a very useful link there.
-
For extra fun, try "%e" instead and see what happens.