log2() only works with double types?

조회 수: 3 (최근 30일)
atharva aalok
atharva aalok 2021년 10월 24일
댓글: atharva aalok 2021년 10월 24일
log2() seems to only work with 'double' types. It throws error when argument is int64.
num = 10000;
d = floor(num / 7);
a = log2(d);
disp(a);
10.4798
d = int64(num / 7);
b = log2(double(d));
disp(b);
10.4808
b = log2(d);
Check for incorrect argument data type or missing argument in call to function 'log2'.
disp(b);

채택된 답변

Simon Chan
Simon Chan 2021년 10월 24일
Check MATLAB documentation and this fucntion log2 supports type single or double as the input argument type.
  댓글 수: 3
Steven Lord
Steven Lord 2021년 10월 24일
The output of the log2 function is the same type as the input. [This is a general pattern for many element-wise functions.] There are only a handful of int64 numbers whose base 2 log is representable as an int64 number. If it were defined for int64 we'd likely have a lot of people complaining about "bugs" in log2 when it was returning the correct answer just not the answer they expected.
log2Int64 = @(x) int64(log2(double(x)));
x = int64(10:20);
y = log2Int64(x)
y = 1×11
3 3 4 4 4 4 4 4 4 4 4
check = x - 2.^y % Is this what you would expect?
ans = 1×11
2 3 -4 -3 -2 -1 0 1 2 3 4
atharva aalok
atharva aalok 2021년 10월 24일
That was great help Steven! Thanks a ton!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by