File Exchange

image thumbnail

Order of magnitude of number

version 1.1 (1.24 KB) by

Returns the order of magnitude of one or more numbers.

7 Downloads

Updated

View License

Returns the order of magnitude of one or more numbers. Works on arrays. You can also specify the number base.

%Ex:
order( 1.2e-5 ) %returns -5
order(1) = 0
order( 1.12344e8 ) %returns 8
%Specify base 2:
order(1.2*2^5,2) %returns 5

Comments and Ratings (10)

Ben

Now tested with vector:

>> floor(single(log([1000 100 1000 10000 100])./log(10)))

ans =

     3 2 3 4 2

Joel, I seem to experience the same problem when the input is an exact integer value. I think this will be a problem when the input is a whole power of the base. I am not sure yet how this shuold be solved, one would have to do a test on the input to detect if it is a whole power of the base within numerical precision

Joel

Joel (view profile)

I seem to experience a severe precision error using this function. I get order(1000) = 2. order(1000.000000000001) gives me the correct value of 3.

Irem Altan

Carlos

Carlos (view profile)

Thanks!

Efficient.

John

John (view profile)

I see what you're doing and it makes sense, thanks for the reply!

Thanks John. But I think one should leave the order of magnitude of 0 to be minus infinity, that`s just what it is. If you set it to zero, consider this:
x=0;
o = order(x);
x2=10^o;
x2 and x should then be the same, but they won`t be if you set the order to 0. x2 would then be 1.

John

John (view profile)

added the following to the bottom:

% If the value is zero then the order is set to zero
n(isinf(n)) = 0;

John

John (view profile)

works great!

Updates

1.1

Added possibility to specify base.

MATLAB Release
MATLAB 7.14 (R2012a)
Acknowledgements

Inspired by: logb

Inspired: SEROUND: Round value and standard error

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video