|
1 | 1 | # Introduction
|
2 | 2 |
|
3 |
| -_Left blank for now. Contents of `concepts/numbers/introduction.md` to be copied here when finalized._ |
| 3 | +Julia is a general-purpose language that can be used for most programming tasks. |
| 4 | +In practice, however, the main use cases tend to be in engineering and science. |
| 5 | +Fast, versatile, sophisticated numerical calculations are central to the design. |
| 6 | + |
| 7 | +## Integers |
| 8 | + |
| 9 | +An integer is a "round" number with no decimal point. |
| 10 | + |
| 11 | +In the [Basics][basics] concept, we saw that an integer value can be assigned to a variable without specifying a type. |
| 12 | + |
| 13 | +For readability, underscores can be used as a digit separator. |
| 14 | +They are ignored by the compiler. |
| 15 | + |
| 16 | +```julia-repl |
| 17 | +julia> x = 3 |
| 18 | +3 |
| 19 | +
|
| 20 | +julia> typeof(x) |
| 21 | +Int64 |
| 22 | +
|
| 23 | +julia> large_number = 1_234_567_890 |
| 24 | +1234567890 |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +## Floating-point |
| 29 | + |
| 30 | +It will be no surprise that floating-point numbers optionally have a decimal point, and a fractional part after the point. |
| 31 | + |
| 32 | +```julia-repl |
| 33 | +julia> f = 3.45 |
| 34 | +3.45 |
| 35 | +
|
| 36 | +julia> typeof(f) |
| 37 | +Float64 |
| 38 | +``` |
| 39 | + |
| 40 | +Of course, scientific notation is supported. |
| 41 | + |
| 42 | +```julia-repl |
| 43 | +julia> avogadro = 6.02e23 |
| 44 | +6.02e23 |
| 45 | +``` |
| 46 | + |
| 47 | +The maximum and minimum values may come as a surprise: |
| 48 | + |
| 49 | +```julia-repl |
| 50 | +julia> typemax(Float64) |
| 51 | +Inf |
| 52 | +
|
| 53 | +julia> typemin(Float64) |
| 54 | +-Inf |
| 55 | +``` |
| 56 | + |
| 57 | +Infinity is a valid value! |
| 58 | + |
| 59 | +## Arithmetic operators |
| 60 | + |
| 61 | +As discussed in the Basics concept, arithmetic operators mostly work the same as standard arithmetic, as taught to children. |
| 62 | +Note that exponentiation uses `^`, _not_ `**` (both are common in other languages). |
| 63 | + |
| 64 | +```julia |
| 65 | +2 + 3 # 5 (addition) |
| 66 | +2 - 3 # -1 (subtraction) |
| 67 | +2 * 3 # 6 (multiplication) |
| 68 | +8 / 2 # 4.0 (division) |
| 69 | +8 % 3 # 2 (remainder) |
| 70 | +2 ^ 3 # 8 (exponentiation) |
| 71 | +``` |
| 72 | + |
| 73 | +However, a few Julia-specific details are worth discussing. |
| 74 | + |
| 75 | +### Multiplication |
| 76 | + |
| 77 | +```julia-repl |
| 78 | +julia> x = 4.2 |
| 79 | +4.2 |
| 80 | +
|
| 81 | +julia> 2 * x |
| 82 | +8.4 |
| 83 | +
|
| 84 | +julia> 2x |
| 85 | +8.4 |
| 86 | +
|
| 87 | +julia> 2.4x |
| 88 | +10.08 |
| 89 | +``` |
| 90 | + |
| 91 | +That may be surprising. |
| 92 | + |
| 93 | +It is always possible to use `*` as an infix operator, as in most other computer languages. |
| 94 | + |
| 95 | +However, Julia is designed by people who believe that code should look as much as possible like mathematical equations. |
| 96 | + |
| 97 | +Because variable names must start with a letter, prefacing the name with a number (integer or floating-point) is treated as implicit multiplication. |
| 98 | + |
| 99 | +For example, if we want the surface area of a sphere, instead of `4 * pi * r * r` we could do this : |
| 100 | + |
| 101 | +```julia-repl |
| 102 | +julia> surface(r) = 4π * r^2 |
| 103 | +surface (generic function with 1 method) |
| 104 | +
|
| 105 | +julia> surface(3) |
| 106 | +113.09733552923255 |
| 107 | +``` |
| 108 | + |
| 109 | +Although π is a built-in constant, it is also a (Greek) letter. |
| 110 | +The parser therefore still needs one explicit `*` to separate `π` from `r`. |
| 111 | + |
| 112 | +### Division |
| 113 | + |
| 114 | +Using `/` as the infix operator will always give a floating-point result, even for integer inputs. |
| 115 | + |
| 116 | +For integer division, there are more options. |
| 117 | + |
| 118 | +```julia-repl |
| 119 | +julia> 10 / 3 # floating-point division |
| 120 | +3.3333333333333335 |
| 121 | +
|
| 122 | +julia> div(10, 3) # integer division |
| 123 | +3 |
| 124 | +
|
| 125 | +julia> 10 ÷ 3 # synonym for div() |
| 126 | +3 |
| 127 | +
|
| 128 | +julia> 10 // 3 # rational number (fraction) |
| 129 | +10//3 |
| 130 | +``` |
| 131 | + |
| 132 | +The `div()` function is for integer division, with the result truncated towards zero: downwards for positive numbers, upwards for negative numbers. |
| 133 | + |
| 134 | +As a synonym, we can use the infix operator `÷`, again aiming to make it look more mathematical. |
| 135 | +If you are using a Julia-aware editor, enter this as `\div` then hit the `<Tab>` key. |
| 136 | + |
| 137 | +The `//` operator is beyond the scope of this Concept. |
| 138 | +For now, we can just say that the result of `//` is a "rational" number, which most people call a _fraction_. |
| 139 | + |
| 140 | +## Conversion of numeric types |
| 141 | + |
| 142 | +This can often happen automatically: |
| 143 | + |
| 144 | +```julia-repl |
| 145 | +julia> x = 2 + 3.5 |
| 146 | +5.5 |
| 147 | +
|
| 148 | +julia> typeof(x) |
| 149 | +Float64 |
| 150 | +``` |
| 151 | + |
| 152 | +We added an `Int64` to a `Float64`, and got a `Float64` result. |
| 153 | + |
| 154 | +In fact, the integer was silently converted to a `Float64` before doing the addition. |
| 155 | + |
| 156 | +**Float-to-integer** conversions are inevitably more complicated. |
| 157 | +What do you want to do with anything after the decimal point? |
| 158 | + |
| 159 | +- The `round()` function converts to the nearest whole number, with ties such as 4.5 rounding to the nearest _even_ whole number. |
| 160 | +- `floor()` rounds down, `ceil()` rounds up, `trunc()` rounds towards zero. |
| 161 | +- Attempting to cast directly, for example with `Int32()`, will fail with an `InexactError`. |
| 162 | + |
| 163 | +However, by default these functions do not return the integer type you might have wanted. |
| 164 | +The desired output type can be specified. |
| 165 | + |
| 166 | +```julia-repl |
| 167 | +julia> round(4.5) |
| 168 | +4.0 |
| 169 | +
|
| 170 | +julia> ceil(Int, 4.3) |
| 171 | +5 |
| 172 | +``` |
| 173 | + |
| 174 | +Rounding to a specified number of digits after the decimal point is also possible with the `digits` keyword. |
| 175 | + |
| 176 | +```julia-repl |
| 177 | +julia> round(π, digits=10) |
| 178 | +3.1415926536 |
| 179 | +``` |
| 180 | + |
| 181 | +## Divide-by-zero |
| 182 | + |
| 183 | +Surely this just throws an error? |
| 184 | +In fact, the situation is not that simple. |
| 185 | + |
| 186 | +Integer division with `÷` or `//` will result in an error, as you might expect. |
| 187 | + |
| 188 | +Floating-point division with `/` takes what might be considered an engineering approach, rather than a standard computer science approach: |
| 189 | + |
| 190 | +```julia-repl |
| 191 | +julia> 2 / 0 |
| 192 | +Inf |
| 193 | +
|
| 194 | +julia> 0 / 0 |
| 195 | +NaN |
| 196 | +``` |
| 197 | + |
| 198 | +As discussed in a previous section, infinity is a valid floating-point number in Julia, represented by `Inf`. |
| 199 | + |
| 200 | +When the numerator is also zero, the result is mathematically undefined. |
| 201 | +Julia then treats it as "not a number", represented by `NaN`. |
| 202 | + |
| 203 | +[basics]: https://exercism.org/tracks/julia/concepts/basics |
0 commit comments