C- precision problem
Hai,
Is it possible to display more than 16 digits after a decimal point, even if they cycle in a C program. To be clear: 1/57 Result: fraction that displays 16 digits afetr decimal point and then only zeroes how can I display more digits Thanks for the solutions |
What data type are you using? float, double or long double?
What format specifier are you using %e %f %g or (%Le %Lf %Lg for long) graeme. |
Here is an example:
Code:
#include <stdio.h> Code:
j_shaw@jshaw ~ $ ./test I put in the example without a cast to prove a point.... if you are going to use double or long double for precision make sure you do all your math in the right precision or you'll end up with results you probably don't expect... |
Excuse me for providing so little info for analysis.
By the way the code that tries the problem is here... Code:
void main() |
What is the output your getting with your code?
I got all the precision I would have expected. Code:
j_shaw@jshaw ~ $ ./test |
ditto...
using fedora 4, gcc 4.0.2, run from the console and the code provided the output was as expected. (after adding the required #include) If you are still not getting the output can you tell us the compiler version and anything different you may be doing? graeme |
Sorry jitshaw for..
1. missing ur post by a whisker 2. I was actually working on MS Visual studion then and its output doesnt seem to be altered by the type casts. 3. For delaying my response Now in gcc its goin fine and I am able to get the results even without typecasts. Thanks for ur response.. And here is one another bug in Visual Studio ----------------------- I missed ur post too graemef sorry-- |
are you using gcc in windows or Linux - I seem to remember that gcc in windows doesn't support the L modifier and so the output is restricted. That may have been fixed not certain.
graeme. |
No..
I switched to my Linux(Fedora) to run that By the way what is that L modifier? |
It's a type modifier used for long precision L = long. I believe it is the standard but it not always supported.
graeme. |
Are u sure?
But it is a part of K & R specificaions .. isnt it? |
The L specifier applied to a, A, e, E, f, F, g, or G is a part of the C99 specification. The SUSv2 spec also contains the L specifier for all conversions except F.
|
Quote:
Also of note is that the digits at the end of that string probably aren't even right. If you do several arithmatic operations you get a truncation error that will build up, and can be the main source of error in many scientific modeling applications. |
Quote:
Code:
/*computes constant e*/ |
Warg,
You have a few cast converstion problems going on there. The most drastic is that e is declared as a double instead of a long double. You going to loose precision each time you return from fact because your putting a long double result into a double variable. I've made a few changes that make it better... though you probably want to get a high precision math library if you really need that much precision. Code:
/*computes constant e*/ |
All times are GMT -5. The time now is 01:15 AM. |