Skip to main content
Mathematics LibreTexts

2.1: Numbers

  • Page ID
    53704
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Solving Scientific Computing problems ultimately boils down to manipulating data and at the most basic is that of strings and numbers. We begin with understanding these data types and how to store values in them.

    We also show some of julia syntax, which looks like other languages (like python). Hopefully you have some basic knowledge of computing, but no assumption of any particular language is necessary.

    Numbers

    Not surprisingly, the most important data type in scientific computing–at least at the most atomic level is numbers. Numbers in computation mimic those in mathematics with some important differences. Julia like most computing languages have two main number types, integers and floating points. Julia’s integers and much like mathematical integers in that they store numbers like \(0, 10, -400\). Floating-point numbers generally are approximations to real or decimal numbers and more details will be covered in Chapter ??.

    Julia also has a rational data type for numbers commonly though of as fractions like \(-\frac{2}{3}\) or \(\frac{22}{7}\). Recall that typically \(i\) represents \(\sqrt{-1}\), the base imaginary number. Complex numbers like \(2+3i\) are also native to julia. We will explore both rational and complex numbers in greater depth in Chapter ??.

    Assignment Statements and Variables

    Anything can be stored as a variable using the single equal sign like

    x=6

    This is an assignment operator, which creates the number 6 and stores it under the namex.

    And now that the variable is stored, we can use it in calculations. For example

    x+3
    9

     

    Variables in julia, much like other languages are primarily sequences of alphanumeric characters as well as an underscore . Primarily, a variable needs to start with a alphabetic character or and after the first character can contain numbers.

    Julia also allows many unicode symbols in variable names, however not everything. For example, all of the greek letters are allowed, so \(\alpha=45\) is valid.

    To get a greek letter in Jupyter or the REPL, type \alpha, hit the TAB key and it will be turned into an \(\alpha\).

    Storing Variables in a Virtual Whiteboard

    The details of storing variables in computer hardware isn’t necessary, however, thinking of storing as writing variables and values on a whiteboard is a helpful paradigm. Imagine a whiteboard with a column of variable names and a column of values. For example, if we have

    x=6
    y=-1
    z=8.5
    8.5

     then you can think of the whiteboard looking like:

    variable value
    x 6
    y -1
    z 8.5

     

    If we evaluate any expression containing any of these variables, then the value looked up substituted into the expression.  For example, 

    2*y
    -2

    looks up the value of y (which is -1) and substitutes that value in and multiplies the result by 2.  As you can see the result is -2.

    If we change one of the values, like

    y = y+5

    this means that the right hand side is evaluated by looking up the value of y and the result is 4.  Then the 4 is placed into the whiteboard, which will now look like:

    variable value
    x 6
    y 4
    z 8.5

    If you are thinking of how a piece of code works, often you will need to get to the point of writing down a version of the whiteboard.

    Strings

    In many Scientific Computing fields, such as Data Science, strings arise often and it is important to understand some of the basics of them. In Julia, a string is a sequence of characters surrounded by "" (double quotes). For example:

    str ="This is a string"

    and if you enter

    typeof(str)
     

    which shows that it is a String.  Section 1.2 will go into more details about data types in Julia. The individual parts of the string are called characters, which are of type Charand are by default Unicode Characters (which will we see are super helpful). A few other helpful things about strings are

    • The length of a string is found using the length command. length(str)returns 16.

    • To access the first element of the string, type first(str), the last is found by last(str)and the 3rd character for example is str[3].In julia, string indexing starts at 1

    • To turn other data types into string, use the stringfunction. For example 
       

      string(3.0)
      "3.0"

      and again note that the double quotes indicate that this is a string. 

    String Operations

    A common string operations is concatenation, or the merging of two strings. Consider

    str1 = "The tide is high "
    str2 = "and I'm having fun."

    We can concatenate in two ways, with the *operator symbol or the string function. Both

    str1 * str2
    string(str1,str2)
    "The tide is high and I'm having fun."

    returns the same string concatenation. I find the second option clearer in that *is an odd choice for string concatenation. Many languages including java and ecmascript (javascript) use +instead for string concatenation, which I think intuitively makes more sense.  

    Another cute operation for strings is the caret operation. This could be helpful, however a not-so-helpful example is

    "Hip, hip, hooray! "^3
    "Hip, hip, hooray! Hip, hip, hooray! Hip, hip, hooray! "

    concatenates the string three times. Other important functions related to strings can be found at JULIA DOCUMENTATION ON STRINGS

    String Interpolation

    Mixing strings and other variables together is often needed and we can use the methods of concatenation as above.  However, there is a better way.  Let's use the value of x that we stored above and insert it at the end of "The value of x is ".  The following will do this

    result = "The value of x is $x"
     

    There is actually another way to do string concatenation if you use this method. Using the variables stored in str1 and str2, then 

    "$str1$str2"
     

    will also concatenate the strings.

    We can also go beyond just looking up strings in that we can execute code as well.  Let's say that we have the variable

    n=6

    If we want a string with its square, we can enter:

    "The square of $n is $(n^2)."
    "The square of 6 is 36."

    where you should notice that the code n^2 is executed since that code is within the $(). 

    Exercise

    Use the volume of a sphere \(\frac{4}{3}\pi r^3\), to calculate the volume and produce a string. For example, store r=3 and then return the result

    "The volume of a sphere of radius 3 is 113.0973355292"

    where 3 and the volume is generated with string interpolation.

     
     

     

    Expressions

    An expression is a combination of variables, data elements (like numbers and strings), operations (like + or *) and functions (like ). We’ve seen a number of expressions throughout this chapter so far like

    x=6
    x+3
    str1 * str2
    length(str)
     

    In short, writing things in julia will consist of writing expressions (and slightly more complicated structures).

    Operator Precedence

    When we type out an expression like 11+2*(4+3)^3, it is important to understand the order in which operators are performed. For mathematics, the PEMDAS pnemonic is helpful to rememember in that the order is:

    • Parentheses: The expression inside the ( ) are done first. For the example above, the 4+3 is the first operation done.

    • Exponentials: The ^ is done next. Raise the 7 from above to the power of 3 resulting in 343.

    • Multiplication and Division: In this example, the 2*(343) is done next

    • Addition and Subtraction: Lastly add 11 to the result and the result is 697.

    In any computing language, there are other operators as well and there is order to that precedence, so we will see that there are other things to think about. For example, the assignment operator, has the lowest precedence. That is when assigning something to a variable, all calculations are done on the right side of the = before the assignment.

    Details on all this can be found on the JULIA DOCUMENTATION ON OPERATOR PRECEDENCE

    Comments

    A comment in computer code is sequences of characters which are ignored. The purpose of a comment is to alert a human on what is going on. You may have been told to write comments so that someone else who reads your code understands what you are doing. However, I have found that the person mostly like to read your code is you at a later date. You should add comments for yourself.

    In julia, a comment is anything to the right of a , pound sign or hash tag. For example:

    ## This calculates the area of a circle
    r=3
    pi*r^2 # this is the actual formula for the area

    Both lines 1 and 3 have comments. On line 1, the entire line is ignore since the line starts with #. On line 3, everything after the 2 (the power) is ignored. Also, notice that there are two hash tags on line 1 and 1 on line 3. This is simply different style. Since anything after a single is a comment, everything after the first one is ignored.


    2.1: Numbers is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?