Title: Pseudo-code for conversion functions Matlab and J interpretations
1Inverse functions and branching controls
- Pseudo-code for conversion functionsMatlab and J
interpretations
2Binary / Decimal conversions
- Purpose to convert a decimal to a binary number
- Function Name d2b
- Algorithm find the powers of 2 required
- Purpose to convert a binary number to a decimal
- Function Name b2d
- Algorithm sum the powers of 2 indicated
3Matlab implementation of d2b
- function Bs d2b(y,p)
- Syntax Bs d2b(y,p)
- y is a real number (gt0)
- Bs is binary form of y to p places N
floor(log(y)/log(2)) - B 1 y y - 2N
- for k 1 Np
- w 2(N - k)
- b (w lt y)
- B B,b
- y y - bw
- end
- tx num2str(B)
- S tx(find(tx ' ')) delete spaces
- Bs S(1N1), '.', S(N2length(S))
- 0 B p d2b y 0.1 NB. deleted for lack
of space - 1 n?N?floor( log(y) / log(2))
- 2 w ? 2 n
- 3 B? b? 1
- 4 y ? y b w
- 5 for k 1 to Np
- 5.1 n ? n 1
- 5.2 w ? 2 n
- 5.3 b ? w lt y
- 5.4 B ? B , b
- 5.5 y ? y b w
- 6 B ? p List2char B
Some of Matlabs most horrible notational devices
are on display here S(1) , a b
4Executing the saved function
- NOTEnot only have we had to know some Matlab
functions and special syntax for doing what
amounts to the List2char function, but we have
also had to modify the pseudo code look
carefully at the listing and explain why!
This function, saved in the file named d2b.m in
Matlabs WORK directory, executes perfectly
- gtgt d2b(82.65,18)
- ans
- 1010010.101001100110011001
- gtgt d2b(128,10)
- ans
- 10000000.0000000000
5Tracing and testing algorithms
- The two crucial changes that had to be made in
the pseudo-code came from the range for the for
loop and from the binary test.(Had we used the
while loop would we have needed a similar
change?) - Strong minded logic would save us from these
errors, but most people are not that clear headed
unless they are very experienced programmers.
That is why testing code in a machine is so
useful. Alternatively, you can test the
pseudo-code by tracing each step by hand. Most
people prefer the former alternative but an
example of tracing a functions execution by hand
will be given in the tutorial.
6Trace of an algorithm
- Tracing an algorithm involves printing out the
result of every step in the procedure. - Good software development packages will include a
trace option. Such information is invaluable in
debugging programs. - Tracing of pseudo-code must necessarily be by
hand.
7 From binary back to decimal
- If we have a binary number eg. 110110.011 given
as a list rather than as a single numeral, that
is as 1 1 0 1 1 0 0 1
1 then write down the powers of 2 to the
level required 32 16 8 4 2 1
½ ¼ 1/8 - multiply the two lists term by term and add
up the resulting numbers - 3216421/41/8 54(3/8) 54.375
8Define a function called b2d
- Algortihm
- Given a character input, find the point and hence
count how many places to the left there are. - Write down the list of powers required and
compute all the required powers of 2. - Convert the binary string to a numeric list of
bits. - Take the dot product of the list of bits and the
list of powers of 2.
- Pseudo code
- 0 D b2d(B)
- 0.1 NB. suppressed
- 1 p ? ListofPowers(B)
- 2 w ? 2p
- 3 b ? Bitlist(B)
- 4 D ? sum(wb)
- 4.1 NB. or w dot b
- Notice how dependent we are on data manipulation
functions. Also assumed is that powers and
multiplication for lists works element by
element, i.e. - 2 3 4 6 7 8 gives 12 21 32
- and 2 2 3 4 gives 4 8 16
9Matlab code( J code is in file conversions.ijs)
Functions execute in their own workspaces. All
variables are local.
- function D b2d(B)
- suppressed
- p ListofPowers(B)
- w 2 . p
- b BitList(B)
- D sum(w . b)
- D dot(w, b)
- function p ListofPowers(w)
- suppressed
- b find( '. ' w)
- p b - 2length(w)
- function L BitList(b)
- suppressed
- L '1' b('.' b)
Values are passed to a functions workspace
through the input variables and returned to the
workspace from which the function was called
through the output variables.Variable names in
different workspaces do not clash.
10Errors at the edge
But again we might get a surprise
- gtgt b2d('101001110001.')
- ans
- 2673
- gtgt b2d('101001110001')
- ??? Error using gt minus
- Matrix dimensions must agree.
- Error in gt b2d at
- p bp - 2length(B)
- In executing b2d('101001110001') the variable
bp, the index (or location) of the binary point
was actually empty that is, there is nothing at
all being stored in the location to which bp
points.
11Checking the code
- It is not enough to check that the algorithm
works for random inputs but also for extreme
inputs inputs at the edge empty and other
special forms of input. -
- Since the only way to check pseudo-code is by
tracing through the operations step by step it
becomes onerous to repeat it with different
inputs, so one needs to work almost
algebraically, imagining what would happen if the
input was something else. - If you can interpret the pseudo-code into a
computer language then the software should be
able to trace the execution for you. (Matlab has
debugging facilities.)
12Branching controls
- We could insist that the user must put a point at
the end of a whole number, but since we are
probably that user ourselves, we will write code
that passes control to one line or another
depending on some binary decision. This is
illustrated in the revised listing. - Note if a task seems to be one we might use
often then we turn the algorithm for it into a
function, otherwise not.
- function D b2d(B)
- suppressed
- bp find('.'B)
- if length(bp)0
- p length(B)-1 -1 0
- else
- p bp - 2length(B)
- end
- w 2 . p
- b '1' B('.' B)
- D sum(w . b)
13Example of Branch Control
- 0 z Zeros( a, b, c)0.1 NB.Input is the
list of coefficients in descending order0.2
NB.The algorithm will fail if a 0. - 1 d ? b2 - 4 a c
- 2 if d lt 0 do
- 2.1 z ? 'No real zeros.' NB. a
character string - else
- 2.2 z ? -(b - sqrt(d))/(2a) , -(b
sqrt(d))/(2a)
14Data checking and default setting
- Checking that input data is of the right form
before embarking on an algorithm and setting
defaults if certain inputs are not present are
operations that call for branching controls at
the beginning of function code. - The switch branching control is an
alternative to the if ... then elseif control
structure when the alternatives are described by
a character string, or case expression, or
flag. It may not be available in every
environment and it will have its own specific
syntax and conditions when it is.
15Cover functions
- function D dec2bin(y, n)
- suppressed
- if exist('n')0
- n 16 default for n
- end
- if y lt 0 sign 'negative'
- else sign''
- end
- D d2b(abs(y), n)
- switch sign
- case 'negative'
- D '-', D
- end
- Dec2Bin is a cover function for the d2b and
deals with the input data rather than the
conversion process, so it is better to keep
functions with different roles apart. - gtgt Dec2Bin(-16)
- ans
- -10000.0000000000000000
- gtgt Dec2Bin(12.00125)
- ans
- 1100.0000000001010001
- gtgt Dec2Bin(-12.00125, 24)
- ans
- -1100.000000000101000111101011
16Other useful control statements
- continue passes control to the next iteration of
the for or while loop in which it appears,
skipping any remaining statements in the body of
the loop. In nested loops, continue passes
control to the next iteration of the for or while
loop enclosing it. - break terminates the execution of a for or while
loop. Statements in the loop that appear after
the break statement are not executed. In nested
loops, break exits only from the loop in which it
occurs. Control passes to the statement that
follows the end of that loop - return causes a normal return to the invoking
function or to the keyboard. - Notice that these are functions with no input or
output but affect the state of the system, just
like WORD produces an environment
17The term control
- By the control line of a program we mean that
line which at a point in time will be executed
next by the computer. We say that this
particular line has control (of the computer).
- In our pseudo-code, control normally passes
in sequence from one numbered line to the next.
- A control structure is a computer recognized
way of re-directing control to a line other
than the next. Not all software supports all
the control structures considered here and, when
it does, usually has its own special syntax for
that structure.
18Control Statements
- Selective control or branch action
depending on whether some condition is True
or False (a binary decision). - if x ? 1 then else or if then elseif
else - switch value case value1 case value2
- Re-direction controls
- return
- continue
- break
- Repetitive control or loop set of
instructions performed repeatedly while some
condition remains true (a binary decision). - while i ? x do
- for i 1 to n do
19Inverse Functions
- In mathematical jargon two functions are
inverses of one another if one returns what the
other transformed. For example, the square and
square_root functions are inverses the
logarithm to base b ( logb ) and b to the
power ( b ) are inverse functions. - b2d and d2b are inverse functions but only if
their domains are restricted to integers,
because, due to truncation, two (and more)
different numbers could have the same binary
expansion to p places so the transformations of
some numbers when inverted will not return the
original values.
20Floating point conversions suffer the same
problem that not all numbers have a unique
representation.
- J
- 12 4 d2float 237
- 011111110110
- 4 float2d '011111110110'
- 236
- 24 11 d2float 237.37249
- 010000000111111011010101
- 11 float2d '010000000111111011010101'
- 237.3125
- 24 11 d2float 237.3246
- 010000000111111011010101
- In principle we should have inverse functions. In
fact we have a partial sort of inverse.
- Matlab
- gtgt a Dec2Bin(2341)
- a
- 100100100101.0000000000000000
- gtgt b2d(a)
- ans
- 2341
- gtgt a Dec2Bin(2341.0012371)
- a
- 100100100101.0000000001010001
- gtgt b2d(a)
- ans
- 2341.00123596191
21Whats the big idea?
- Values are passed to a functions workspace
through the input variables and returned to the
workspace from which the function was called
through the output variables. - Branching controls if-then-else switch-case
- Error checking, tracing if necessary,
- Inverse functions