From help-octave-request at bevo dot che dot wisc dot edu Thu Jan 11 10:37:12 2001 Subject: Re: Wrong 'log' results when using .oct files From: Przemek Klosowski To: javier dot perez at pipebeach dot com, help-octave@bevo.che.wisc.edu Date: Thu, 11 Jan 2001 11:37:09 -0500 I'm trying to compute the logarithm in base 2 of a number, in C++ .... Commenting out the line #include everything works fine, and I obtain the result "3". jape at shiraz:~/cur/# ./test_log (int) log2(8): 3 3 The problem is when including that file, because the result is "2". jape at shiraz:~/cur/# ./test_log (int) log2(8): 3 2 Any idea why is this way? While I don't know why including octave headers changes your result, the mere fact that a floating number truncated to an integer behaves unpredictably is not at all strange. When you print 'j', the printing routine rounds the least significant bits so that you get a printout of '2' instead of 2.00000000000000000001 or 1.999999999999999999997. When you truncate such numbers to integers, however, you get different results (2 and 1, respectively). Which exact floating point number you get is a tossup, and may depend on order of expressions, level of optimization used in the compilation, phase of the moon, or any number of circumstances beyond your control. Use rounding, or (int) (j+0.5) for a more reliable operation; truncation is really a very blunt operation, and should be avoided. ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------