comparison scripts/strings/bin2dec.m @ 14478:e995b1c97e13 stable

Fix regression in bin2dec which did not allow space-separated input. * base2dec.m: Squeeze spaces from input before applying algorithm. * bin2dec.m: Add tests for using spaces in binary number.
author Rik <octave@nomad.inbox5.com>
date Fri, 16 Mar 2012 16:51:06 -0700
parents 72c96de7a403
children 5bd9e47e9277
comparison
equal deleted inserted replaced
14464:21ac4b576003 14478:e995b1c97e13
26 ## bin2dec ("1110") 26 ## bin2dec ("1110")
27 ## @result{} 14 27 ## @result{} 14
28 ## @end group 28 ## @end group
29 ## @end example 29 ## @end example
30 ## 30 ##
31 ## Spaces are ignored during conversion and may be used to make the binary
32 ## number more readable.
33 ##
34 ## @example
35 ## @group
36 ## bin2dec ("1000 0001")
37 ## @result{} 129
38 ## @end group
39 ## @end example
40 ##
31 ## If @var{s} is a string matrix, return a column vector with one converted 41 ## If @var{s} is a string matrix, return a column vector with one converted
32 ## number per row of @var{s}; Invalid rows evaluate to NaN@. 42 ## number per row of @var{s}; Invalid rows evaluate to NaN@.
33 ## 43 ##
34 ## If @var{s} is a cell array of strings, return a column vector with one 44 ## If @var{s} is a cell array of strings, return a column vector with one
35 ## converted number per cell element in @var{s}. 45 ## converted number per cell element in @var{s}.
52 62
53 %!assert(bin2dec ("0000"), 0); 63 %!assert(bin2dec ("0000"), 0);
54 %!assert(bin2dec ("1110"), 14); 64 %!assert(bin2dec ("1110"), 14);
55 %!assert(bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1); 65 %!assert(bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1);
56 %!assert(bin2dec ({"1110", "1111"}), [14; 15]); 66 %!assert(bin2dec ({"1110", "1111"}), [14; 15]);
67 %!assert (bin2dec ("1 0 1"), 5)
68 %!assert (bin2dec (char ("1 0 1", " 1111")), [5; 15]);
57 69
58 %%Test input validation 70 %%Test input validation
59 %!error bin2dec (); 71 %!error bin2dec ();
60 %!error bin2dec (1); 72 %!error bin2dec (1);
61 %!error bin2dec ("1", 2); 73 %!error bin2dec ("1", 2);