How to get more precision in C ?

P

pereges

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;

}
 
W

Walter Roberson

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);
 
U

user923005

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;
}
 
P

pereges

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 ?
 
P

pereges

pereges said:



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 ?
 
P

pereges

pereges said:



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 ?
 
P

Philip Potter

pereges said:
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
 
J

jacob navia

Richard said:
pereges said:



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...

:)
 
U

user923005

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.
 
U

user923005

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/R...28bit_long_double_floating-point_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).
 
A

Anonymous

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top