473,386 Members | 1,819 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,386 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 8492
>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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.