Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have an embarrassingly simple question, but I need to make sure I understand this correctly. I have a *.c file with the following line:

    CSRConstant = 2*macroParticleCharge*particleCharge/pow(3*rho0*rho0, 1./3.)/(4*PI*epsilon_o*particleMass*sqr(c_mks));

Is this interpreted as:

enter image description here

share|improve this question
1  
No. The pow(3*rho0*rho0, 1./3.) part is interpreted as (3rho^2)^1/3. While algebraically this is the same, in operation it is not, since each step can generate it's own errors, such as overflow, underflow, and truncation. –  andy256 Jun 21 at 7:25
    
I guess @andy256 wanted to say "almost, as long as no errors occur", not blatantly "no". Surprisingly your original C code can deal with rho0<0, while your formula does not. –  Doc Brown Jun 21 at 7:29
    
@Doc Yes, it was a bit shoot-from-the-hip, but it also highlights that the operations are not the order the OP suggests. –  andy256 Jun 21 at 7:32
add comment

2 Answers 2

The answer is almost as embarrassingly simple. No, absolutely not, and anything you do to change what you have written will most likely be wrong until you have read and understood What Every Computer Scientist Should Know About Floating-Point Arithmetic

I only understand about 1/4 of that paper, but I understand enough of it to know that my chances of getting floating point correct are low. Fortunately in my line of work, like many programmers, floating point is either easily avoided or quite simple so when I do have to go there I can spend effort on getting it right.

Essentially the paper describes how floating point on computers is achieved, and what kinds of errors can occur that can generate incorrect results. At first glance, your implementation of the formula will likely exhibit most of the errors described in the link.

share|improve this answer
    
Actually, that answer may contain some truth only when the denominator is nearby 0. If the OP can exclude that case , there is nothing much to change in the formula to improve the floating point exactness. So IMHO this is probably a bit misleading. –  Doc Brown Jun 21 at 8:29
    
@Doc: But does he know this? If so, how? He is using complex floating point, he should still read the paper. –  mattnz Jun 21 at 8:34
2  
Honestly, I posed the question from a physics standpoint and I was trying to analyze the units. The constant is suppose to be in units of energy per unit path length but, the constant doesn't fit this description (unless he/she is working in normalized coordinates). So, that's why I asked, I was just trying to confirm the form of the equations, not really the machine precision/ computer utilization of it; I am analyzing the equations by hand. Thanks though! –  user1886681 Jun 21 at 9:23
add comment

Yes, I as far as I can tell the expression in the form you have written it will produce a very good approximation to the correct result of evaluating that formula over a very large range of input values.

You have performed a specific simplification of one constant calculation, for reasons I do not understand. If you're going to simplify one calculation, why not simplify all of them?

If this calculation appears inside a loop, you might want to check how good your compiler is at optimising those floating point calculations, especially the constants. If it doesn't, you might want to.

There are some rather specific issues that can arise in using floating point calculations on computers, either when you deal with numbers at extreme values (small and large) or when you perform operations that lose significant digits (like subtraction, in particular). I see none of those here.

Worry not. The computer says yes. This time.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.