473,800 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.1415926535897 9323846 /* 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 8542
>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.20 0f", 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.1415926535897 9323846 /* 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************ *************** *******@q27g200 0prf.googlegrou ps.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.1415926535897 9323846 /* 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.1415926535897 9323846 /* 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_approximatio n =
3.1415926535897 932384626433832 795;

int main(void) {
printf("My pi approximation is %.*g\n", DBL_DIG + 1,
pi_approximatio n);
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.1415926535897 9323846 /* 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_approximatio n =
3.1415926535897 932384626433832 795;

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

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

<snip>
why declare pi_approximatio n 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.i nvalidwrote:
pereges said:

<snip>
why declare pi_approximatio n 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.i nvalidwrote:
pereges said:

<snip>
why declare pi_approximatio n 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.i nvalidwrote:
>pereges said:

<snip>
why declare pi_approximatio n 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.1415926535897 9323846 /* M_PI is not defined in math.h

static const double pi_approximatio n =
3.141592653589 793238462643383 2795;
why declare pi_approximatio n 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2805
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 script to convert numbers to arbitrary bases. It aims to take any of whole numbers (python ints, longs, or Decimals), rational numbers (n / m n, m whole) and floating points (the best I can do for reals), and convert them to any base between 2 and...
5
6092
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 purpose, that isn't enough. I was reading a book about some of the financial scandals of the 1990s called "Inventing Money: The Story of Long-Term Capital Management and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions that...
3
23761
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 by C# or they are hardware (processor) dependent. Basic addition operation errors, for ex: 3.6 - 2.4 = 1.19999999999 or 1.20000000003 There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please reply me how these operations are...
2
4239
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", prec, pi ); // 3.1416 The * in "%.*f" says to use the value of the next
6
5717
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
3033
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
6323
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
6611
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, for example, 3.1415926 . Or, in another way, how does the double presented in computer, and
6
17095
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, they say 'for security reasons'. Can the precision level be changed by PHP code as needed?
0
2563
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 significand bits (with an implicit leading 1, for 24 total), and 8 exponent bits double precision: 64 bits including 1 sign bit, 52 significand bits
0
9695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10260
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10042
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6826
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.