tags:

views:

120

answers:

6

I'm not particularly knowledgable about programming and I'm trying to figure out how to get a precise value calculated in a C program. I need a constant to the power of negative 7, with 5 significant figures. Any suggestions (keeping in mind I know very little, have never programmed in anything but c and only during required courses that I took years ago at school)?

Thanks!

+2  A: 

For a constant value, the required calculation is going to be constant too. So, I recommend you calculate the value using your [desktop calculator / MATLAB / other] then hard-code it in your C code.

ahmadabdolkader
when math types say "constant" they mean something different than us programming types. She just means that the number won't be changing after its introduced, not that it's known at design time.
Segfault
A: 

Are you talking about defining the constant, or printing it out?

A good link on defining constants

A good link to printing floating points with printf

Uri
+3  A: 

You can get high-precision math from specialized libraries, but if all you need is 5 significant digits then the built-in float and double types will do fine. Let's go with double for maximum precision.

The negative 7th power is just 1 over your number to the 7th power, so...

double k = 1.2345678;  // your constant, whatever it is
double ktominus7 = 1.0 / (k * k * k * k * k * k * k);

...and that's it!

If you want to print out the value, you can do something like

printf("My number is: %9.5g\n", ktominus7);
Carl Smotricz
If the absolute value of k is near one (and not zero), then this algorithm will work. But this is not a robust or stable algorithm for general floats or doubles. Extremely large or small magnitudes resulting from the early multiplications might swamp or be swamped by the later multiplications. The built-in power function would be best in all cases.
Jeffrey L Whitledge
Note: C does have a library containing a `pow` function in `math.h`.
Brian
@Jeffrey: The range of floating point numbers is symmetric around 1; so long as the 7th power of k doesn't exceed that range, it can be expressed with the maximum accuracy of `double`, and its reciprocal will be no less accurate. Also, of course, the way I've written it, the entire computation can and most likely will be done at compile time.
Carl Smotricz
Just to clarify, I'm talking multiplicatively symmetric. Typical ranges are from roughly 1E-308 to 1E308. Within that range, floating point multiplication loses no accuracy except possibly in the last significant digit, of which there are (typically) about 18, so the first 5 will be fine.
Carl Smotricz
@Carl Smotricz - You're right. I retract my statement. I was thinking of repeated additions--rather than multiplications--in which the values could get swamped. Sorry for the confusion.
Jeffrey L Whitledge
A: 

you can use log to transform small numbers into larger numbers and do your math on the log transformed version. it's kind of tricky but it will work most of the time. you can also switch to python which does not have this problem as much.

madmik3
I think logarithms are overkill for a negative power (see my solution); also, switching languages looks very unnecessary to me as well.
Carl Smotricz
-1: c floats and doubles already use exponential representation. Also, anything described as, "kind of trucky but it will work most of the time" is probably a bad idea if there is an existing, library implementation. And there are plenty of libraries involved with C numbers if for some reason the OP finds `float` and `double` (and `long double`, if available) insufficient.
Brian
Python, written in C, suffers the same overhead and more.
Tim Post
A: 

long double offers the best precision in most cases and can be statically allocated and re-used to keep waste to a minimum. See also quadruple precision. Both change from platform to platform. Quadruple precision says the left most bit (1) continues to dictate signedness, while the next 15 bits dictate the exponent. IEEE 754 (i.e binary128) if the links provided aren't enough, they all lead back to long double :)

Simple shifting should take care of the rest, if I understand you correctly?

Tim Post
+1  A: 

In the realm of computer floating-point formats, five significant digits is not a lot. The 32-bit IEEE-754 floating-point type used for float in most implementations of C has 24 bits of precision, which is about 7.2 decimal digits. So you can just use floating-point with no fear. double usually has 53 bits of precision (almost 16 decimal digits). Carl Smotricz's answer is fine, but there's also a pow function in C that you can pass -7.0 to.

There are times when you have to be careful about numerical analysis of your algorithm to ensure you aren't losing precision with intermediate results, but this doesn't sound like one of them.

Kragen Javier Sitaker