473,239 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

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 8478
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
3
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled...
2
by: mdeaver2003 | last post by:
I'm trying to output a double using a precision that varies, governed by the value of a precision variable. In C I can do it like this: double pi = 3.14159; int prec = 4; printf( "%.*f",...
6
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
2
by: rupert | last post by:
i've got the following code: #include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; int main(double argc, char* argv) { double r = 0.01;
9
by: asdf | last post by:
I want to set the computation precision to quadruple precision, how can I do it in C++ coding? Thanks all.
10
by: Bo Yang | last post by:
Hi, I am confused by the double type in C++. I don't know whether below is legal or possible: double PI = 3.141592675932; Now, can I get another double variable from PI with lower precision,...
6
by: Matthew | last post by:
Hi, I want to change the precision level of floating point variables and calculations which is done in php.ini. However the server I rent for my domain does not give me access to php.ini,...
0
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.