468,256 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

How to get more precision in C ?

I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.

#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;

}
Apr 11 '08 #1
14 8123
>I would like to have precision upto atleast 8 digits in my numerical
>computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.
You only *print* 6 digits after the decimal. You may be getting greater
precision in the actual computation.

Try: printf("%300.200f", s);
although this is primarily useful for debugging. A double won't
have nearly this many digits of precision.
>#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;

}
Apr 11 '08 #2
In article <bd**********************************@q27g2000prf. googlegroups.com>,
pereges <Br*****@gmail.comwrote:
>I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.
>#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/
>int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;
}
Specify a precision with your %f format, such as %.60f . The default
for %f is 6.

Also, to be safe, ensure your output ends in a newline. For example,

printf("%.9f\n", s);

--
Q: Why did the chicken cross the Mobius strip?

A: There were manifold reasons.
Apr 11 '08 #3
On Apr 10, 8:53*pm, pereges <Brol...@gmail.comwrote:
I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.

#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
* * * * double s;
* * * * s = M_PI;
* * * * printf("%f", s);

return 0;

}
Try it this way:

#include <stdio.h>
#include <float.h>
static const double pi_approximation =
3.1415926535897932384626433832795;

int main(void) {
printf("My pi approximation is %.*g\n", DBL_DIG + 1,
pi_approximation);
return 0;
}

Apr 11 '08 #4
On Apr 11, 11:10 am, user923005 <dcor...@connx.comwrote:
On Apr 10, 8:53 pm, pereges <Brol...@gmail.comwrote:
I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.
#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/
int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;
}

Try it this way:

#include <stdio.h>
#include <float.h>
static const double pi_approximation =
3.1415926535897932384626433832795;

int main(void) {
printf("My pi approximation is %.*g\n", DBL_DIG + 1,
pi_approximation);
return 0;

}
why declare pi_approximation as a static const ?
Apr 11 '08 #5
pereges said:

<snip>
why declare pi_approximation as a static const ?
Were you planning on changing it?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 11 '08 #6
On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.invalidwrote:
pereges said:

<snip>
why declare pi_approximation as a static const ?

Were you planning on changing it?

if you are not planning to change it, then declare it as const. what
is the purpose behind declaring it as static ?

Apr 11 '08 #7
On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.invalidwrote:
pereges said:

<snip>
why declare pi_approximation as a static const ?

Were you planning on changing it?

if you are not planning to change it, then declare it as const. what
is the purpose behind declaring it as static ?

Apr 11 '08 #8
pereges said:
On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>pereges said:

<snip>
why declare pi_approximation as a static const ?

Were you planning on changing it?


if you are not planning to change it, then declare it as const. what
is the purpose behind declaring it as static ?
I would imagine that Dann did that to avoid the possibility of a name clash
with other file scope objects, either in other translation units or
perhaps in a third-party library. It wouldn't really matter in a toy
program like this, of course.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 11 '08 #9
pereges wrote:
On Apr 11, 11:10 am, user923005 <dcor...@connx.comwrote:
>On Apr 10, 8:53 pm, pereges <Brol...@gmail.comwrote:
>>#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h

static const double pi_approximation =
3.1415926535897932384626433832795;
why declare pi_approximation as a static const ?
#defines and consts are different beasts which can often serve a similar
purpose. Each has their own advantages and disadvantages - for example,
consts will do type-checking, while #defined constants can be used in
array dimensions (without creating a VLA).

There's a much more rigorous discussion of the differences between
#define and const here:

http://c-faq.com/cpp/constdefine2.html
Apr 11 '08 #10
Richard Heathfield wrote:
pereges said:

<snip>
>why declare pi_approximation as a static const ?

Were you planning on changing it?

Ahh you never know when pi will change and circles
become squares. Specially after two or three Martinis,
I get those wobbling feelings...

:-)
Apr 11 '08 #11
On Apr 11, 12:17*am, pereges <Brol...@gmail.comwrote:
On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.invalidwrote:
pereges said:
<snip>
why declare pi_approximation as a static const ?
Were you planning on changing it?

if you are not planning to change it, then declare it as const. what
is the purpose behind declaring it as static ?
If I intended to use it in another module, it would not be static. If
the complete system does not need the value to be exposed for other
translation units, then it must become static (my rule, not imposed by
C at all). Lint will tell you if that is the case. There is no need
to pollute the end-user's namespace unless you really have to.

I don't like macros for floating point constants. I do tend to use
them for array dimentions and integral constants of that nature. It's
a personal choice and certainly not any better than using a macro.
Apr 11 '08 #12
pereges wrote:
I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.

#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;

}
The "%f" format string dooms you to six decimals after the point. If you
want to display our common 64-bit double at full precision, use..

printf("%.16e", s);

...will print..

3.1415926535897931e+00

...which is all the precision there is in the 64-bit double.

This is good for any double. You can coerce printf to print more that 17
digits but the 'more' doesn't count.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 11 '08 #13
On Apr 11, 3:04*pm, Joe Wright <joewwri...@comcast.netwrote:
pereges wrote:
I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.
#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/
int main(void)
{
* *double s;
* * * * s = M_PI;
* * * * printf("%f", s);
return 0;
}

The "%f" format string dooms you to six decimals after the point. If you
want to display our common 64-bit double at full precision, use..

* *printf("%.16e", s);

..will print..

* *3.1415926535897931e+00

..which is all the precision there is in the 64-bit double.

This is good for any double. You can coerce printf to print more that 17
digits but the 'more' doesn't count.
Assuming 8 byte doubles with 52-53 bits in the mantissa that is true
(OK, every implementation I can think of is like that).

DBL_DIG must be _at least_ 10, but it could be arbitrarily large on a
hypothetical C compiler.
Some available floating point in hardware:
http://www.quadibloc.com/comp/cp0201.htm

It's interesting that AIX implements 128 bit floating point in
software:
http://www.ncsa.uiuc.edu/UserInfo/Re...t_datatype.htm

A compiler vendor could certainly use that technique and supply (for
instance):

float = 64 bit
double = 128 bit
long double = 256 bit

if they had the notion to do so.

Now, I do not know of any machine with doubles larger than 8 bytes but
I do know of 16 byte floating point that is 20+ years old (DEC VAX).
Jun 27 '08 #14
if you are not planning to change it, then declare it as const. what is
the purpose behind declaring it as static ?
A number of reasons. As others have already pointed out, static variables
will not cause name conflicts with other files.

Also, nonstatic global variables can be far slower than static ones,
especially in a shared library, as they may be indirected through a
global offset table. Making a variable global and nonstatic may also make
it difficult for the compiler to do certain optimizations.
Jun 27 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian van den Broek | last post: by
5 posts views Thread by DAVID SCHULMAN | last post: by
3 posts views Thread by Madan | last post: by
2 posts views Thread by mdeaver2003 | last post: by
6 posts views Thread by R.Biloti | last post: by
2 posts views Thread by rupert | last post: by
9 posts views Thread by asdf | last post: by
10 posts views Thread by Bo Yang | last post: by
reply views Thread by Charles Coldwell | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.