Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Two impleme...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 10 Topic 45893 of 47032
Post > Topic >>

Re: Two implementations of simple math equation yield different results

by "Alf P. Steinbach" <alfps@[EMAIL PROTECTED] > May 13, 2008 at 07:37 AM

* Avi:
> I need to implement the following calculation:
> f = (a*b) + (c*d)
> where a,b,c,d are given double values and f is a double variable for
> the result
> 
> I found out that using two different implementations gives two
> different results:
> 1) In the first implementation I compute the entire equation in memory
> 2) In the second implementation I store intermediate to variables, and
> then load them to compute the final result.
> The results differ by a tiny amount (1E-15). I suspect that the reason
> for the difference is from round off errors of the compiler.
> Nonetheless, I need to know the reason for the difference as I need to
> have an absolute reference for optimization work.

Most likely calculations are performed using higher precision (e.g. 
80-bits IEEE) than 'double' (e.g. 64-bits IEEE).  Thus, when you store 
intermediate results in 'double' variables you lose a little 
precision.  It depends very much on the compiler, and also which 
optimizations you select and whether and how the optimizations are 
applied by the compiler.

Possibly you can retain the precision if you use 'long double', but on 
some compilers 'long double' has the same precision as 'double'.

And note that unless your compiler gives strict guarantees, whatever 
result you get is not necessarily the same as with other options or 
perhaps with other surrounding code (which can affect optimizations).


Cheers, & hth.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 




 10 Posts in Topic:
Two implementations of simple math equation yield different resu
Avi <avner-moshkovitz@  2008-05-12 22:16:30 
Re: Two implementations of simple math equation yield different
Paavo Helde <nobody@[E  2008-05-13 00:34:44 
Re: Two implementations of simple math equation yield different
"Alf P. Steinbach&qu  2008-05-13 07:37:47 
Re: Two implementations of simple math equation yield different
James Kanze <james.kan  2008-05-13 02:42:18 
Re: Two implementations of simple math equation yield different
Greg Herlihy <greghe@[  2008-05-13 03:15:20 
Re: Two implementations of simple math equation yield different
Avi <avner-moshkovitz@  2008-05-13 10:00:55 
Re: Two implementations of simple math equation yield different
Avi <avner-moshkovitz@  2008-05-13 10:02:01 
Re: Two implementations of simple math equation yield different
Victor Bazarov <v.Abaz  2008-05-13 13:25:30 
Re: Two implementations of simple math equation yield different
Avi <avner-moshkovitz@  2008-05-13 10:52:29 
Re: Two implementations of simple math equation yield different
James Kanze <james.kan  2008-05-14 01:04:47 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:50:07 CDT 2008.