Python NumPy log2 vs MATLAB -
i'm python newbie coming using matlab extensively. converting code uses log2
in matlab , used numpy log2
function , got different result expecting such small number. surprised since precision of numbers should same (i.e. matlab double vs numpy float64).
matlab code
a = log2(64); --> a=6
base python code
import math = math.log2(64) --> = 6.0
numpy code
import numpy np = np.log2(64) --> = 5.9999999999999991
modified numpy code
import numpy np = np.log(64) / np.log(2) --> = 6.0
so native numpy log2
function gives result causes code fail test since checking number power of 2. expected result 6, both native python log2
function , modified numpy code give using properties of logarithm. doing wrong numpy log2
function? changed code use native python log2
now, wanted know answer.
no. there nothing wrong code, because floating points cannot represented on our computers. use epsilon value allow range of error while checking float values. read the floating point guide , this post know more.
edit - cgohlke has pointed out in comments,
depending on compiler used build numpy np.log2(x) either computed c library or 1.442695040888963407359924681001892137*np.log(x) see this link.
this may reason erroneous output.
Comments
Post a Comment