Board index » cppbuilder » log(2.0)-log(2.0)
cory
CBuilder Developer |
cory
CBuilder Developer |
log(2.0)-log(2.0)2004-12-17 05:26:18 AM cppbuilder14 In debugging a code I have the following line printf( "%e", log(2.0) - log(2.0)) which should be zero. What I get is -2.320193e-17 Any ideas? |
Bruce Salzman
CBuilder Developer |
2004-12-17 05:35:16 AM
Re:log(2.0)-log(2.0)QuoteIn debugging a code I have the following line docs.sun.com/source/806-3568/ncgTOC.html Esp. Appendix D Regards, Bruce |
cory
CBuilder Developer |
2004-12-17 06:24:39 AM
Re:log(2.0)-log(2.0)
I understand floating point arithmetic, but the computer should still give
me zero in this case. The numbers are EXACTLY the same. Cory " Bruce Salzman" < XXXX@XXXXX.COM >wrote in message Quote
{smallsort} |
Greg Chicares
CBuilder Developer |
2004-12-17 07:46:49 AM
Re:log(2.0)-log(2.0)
On 2004-12-16 4:26 PM, cory wrote:
QuoteIn debugging a code I have the following line set the floating-point hardware to use 80-bit 'long double' internally have the floating-point hardware compute ln 2 and store that value into a double, which loses some precision have the floating-point hardware compute ln 2 and leave it on the floating-point stack push the stored value onto the stack subtract and pop the result into a double Then the result would be something like long double x0 = 0.693147180559945309417; double x1 = 0.693147180559945; double x3 = x1 - x0; // -0.000000000000000309... which is about what you report. Of course, the lossy conversion to double takes place in binary, so it doesn't give a value exactly equal to the decimal 0.693147180559945; but I bet you can create a test program this way that reproduces your observed discrepancy exactly. If you want the answer to be exactly zero, then store intermediate values into doubles. There are probably compiler options to force such stores, but writing it explicitly is more portable. |
Bruce Salzman
CBuilder Developer |
2004-12-17 08:03:04 AM
Re:log(2.0)-log(2.0)
"cory" < XXXX@XXXXX.COM >wrote in message
QuoteI understand floating point arithmetic, but the computer should still x*y != y*x or x < y but 1.0 * x>y happen. If you want to test for equality, or test the results of a computation for zero, it is usually advisable to use a threshold. See Greg's answer for an explanation of what is no doubt happening in your example. HTH, Bruce |
Ed Mulroy [TeamB]
CBuilder Developer |
2004-12-17 11:38:55 AM
Re:log(2.0)-log(2.0)QuoteI understand floating point arithmetic, but the computer are handled in a register which has extra length for maintaining precision. Because of the way you formed the expression in your code one of the terms in that expression is in that intermediate register and the other is not. Have the compiler create an assembly source file from your code and read the processor instructions. support.intel.com/design/PentiumIII/documentation.htm docs.sun.com/source/806-3568/ncg_goldberg.html www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html docs.sun.com/app/docs/doc/800-7895/6hos0aou4 . Ed Quotecory wrote in message |
cory
CBuilder Developer |
2004-12-17 02:20:38 PM
Re:log(2.0)-log(2.0)
I ran a little test and you are correct. Thanks for pointing that out.
Cory "Greg Chicares" < XXXX@XXXXX.COM >wrote in message QuoteOn 2004-12-16 4:26 PM, cory wrote: |