[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Wrong 'log' results when using .oct files

From: Przemek Klosowski
Subject: Re: Wrong 'log' results when using .oct files
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 <octave/config.h>
   everything works fine, and I obtain the result "3". 

           address@hidden:~/cur/# ./test_log 
           (int) log2(8): 3 3

   The problem is when including that file, because the result is "2".

           address@hidden:~/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 
or any number of circumstances beyond your control.

Use rounding, or (int) (j+0.5) for a more reliable operation; truncation is 
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:
How to fund new projects:
Subscription information:

reply via email to

[Prev in Thread] Current Thread [Next in Thread]