Converting strings to numbers.
authorClaude Heiland-Allen <claude@mathr.co.uk>
Fri, 20 Dec 2013 20:02:26 +0000 (20:02 +0000)
committerClaude Heiland-Allen <claude@mathr.co.uk>
Fri, 20 Dec 2013 20:02:26 +0000 (20:02 +0000)
commit024682465b9a56ec28bceb65e877d6e5636effcd
tree0a8bf291a4cf613b52135857ce2b48aae7f7eb89
parentbcba5f0deb472299e17b96e35fe514992496ce30
Converting strings to numbers.

The region of interest in the image we have so far looks a bit small
and to the left of the frame.  We could edit the program to change the
center and radius and recompile it, but that would tire quickly.  Instead
we'll use the command line arguments.  The arguments are passed to main()
as strings, but we need numbers.  The C library header ``stdlib.h''
declares a function ``atof()'' to read a representation of a number in
decimal format from a string and convert it to a floating point number.

However, we should first check that we have been given arguments before
trying to use them.  If there is no string where we try to read a string,
we get ``undefined behaviour'': anything from a program crash (if we're
lucky) to carrying on with incorrect data and generating meaningless
output.  We want three floating point numbers from three arguments, for
the real and imaginary parts of the center, and the radius of the view;
we get the program name as the first argument, so we need to be sure that
we have more than three arguments.  In case fewer arguments were provided,
we leave the center and radius unchanged at the default values we set
previously.

Now we can zoom in to see more of the detail of the Mandelbrot set:

    $ ./mandelbrot -0.75 0.00 1.25
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ----------------------------------------------xxx-----------------------
    ----------------------------------------------xxx-----------------------
    ----------------------------------------xxxxxxxxxxxxx-xxx---------------
    ----------------------------------------xxxxxxxxxxxxxxx-----------------
    --------------------------------------xxxxxxxxxxxxxxxxxxx---------------
    -----------------------------xxxxx---xxxxxxxxxxxxxxxxxxxx---------------
    ----------------------------xxxxxxxx-xxxxxxxxxxxxxxxxxxx----------------
    --------------xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx------------------
    ----------------------------xxxxxxxx-xxxxxxxxxxxxxxxxxxx----------------
    -----------------------------xxxxx---xxxxxxxxxxxxxxxxxxxx---------------
    --------------------------------------xxxxxxxxxxxxxxxxxxx---------------
    ----------------------------------------xxxxxxxxxxxxxxx-----------------
    ----------------------------------------xxxxxxxxxxxxx-xxx---------------
    ----------------------------------------------xxx-----------------------
    ----------------------------------------------xxx-----------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    $ ./mandelbrot -1.50 0.00 0.50
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ----------------------------------------------------------x------------x
    ---------------------------------------------------xxxxxxxxxxxxx-------x
    --------------------------------------------------xxxxxxxxxxxxxxxxx---xx
    -------------------------------------------------xxxxxxxxxxxxxxxxxxx--xx
    ------------------------------------------------xxxxxxxxxxxxxxxxxxxxx-xx
    -------------------------------------------xxxx-xxxxxxxxxxxxxxxxxxxxx-xx
    --------------xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    -------------------------------------------xxxx-xxxxxxxxxxxxxxxxxxxxx-xx
    ------------------------------------------------xxxxxxxxxxxxxxxxxxxxx-xx
    -------------------------------------------------xxxxxxxxxxxxxxxxxxx--xx
    --------------------------------------------------xxxxxxxxxxxxxxxxx---xx
    ---------------------------------------------------xxxxxxxxxxxxx-------x
    ----------------------------------------------------------x------------x
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    $ ./mandelbrot -0.10 0.85 0.25
    xxx---------xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx---------------
    ------------xxx---xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-----xx---------------
    -------------------x--x---x-xxxxxxxxxxxxxxx-x-x---x---------------------
    -----------------------------xxxxxxxxxxx--------------------------------
    ---------------------------xxxxxxxxxxxxxx-------------------------------
    --------------------------xxxxxxxxxxxxxxxxx-----------------------------
    --------------------------xxxxxxxxxxxxxxxxx-----------------------------
    -------------------------xxxxxxxxxxxxxxxxxx-----------------------------
    --------------------------xxxxxxxxxxxxxxxxx-----------------------------
    --------------------------xxxxxxxxxxxxxxx-------------------------------
    ------------------------------xxxxxxxxx---------------------------------
    ---------------------------------xxxx-----------------------------------
    ---------------------------------xxxx-----------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    -----------------------------------------x------------------------------
    ------------------------------------------------------------------------
    -------------------------------x----------------------------------------
    ------------------------------------------------------------------------
    ------------------------------------------------------------------------
    $
mandelbrot.c