How to Read Math in Code in This Website

Share

Sometimes, articles on this website use math, which is terrifying. I'm not a mathematician. I'm a programmer. Consequently, the way math is written on the articles is based on how math looks like in programming languages, and it may look different from what you would see normally.

Mathematical Symbols

+

Addition: 1 + 2 = 3 (one plus two equals three).

The result of an addition (above, 3) is also called the sum.

-

Subtraction: 3 - 2 = 1 (three minus two equals one).

The result of a subtraction (above, 1) is also called the difference.

*

Multiplication: 2 * 3 = 6 (two times three equals six).

The result of a multiplication is the product, while each value being multiplied is a factor.

Multiplication is adding a factor to itself the amount of times specified in the other factor:

2 * 3 = 2 + 2 + 2 = 6.

The asterisk is used for multiplication across various languages.

In traditional math, he symbol would be an "x" (e.g. 3×2=6); or a dot (e.g. 3·2 = 6).

/

Division: 6 / 2 = 3 (six divided by two equals three).

In traditional math, the division symbol is a horizontal line with a dot above and one below (e.g. 6÷2=3).

()

Parentheses are used to group mathematical expressions. Expressions inside parentheses should be evaluated first. Observe:

2 * 3 + 2 = 6 + 2 = 8.

2 * (3 + 2) = 2 * 6 = 12.

In this site, parentheses can go inside other parentheses, e.g. 1 - (1 / (2 + 3)). In traditional math, other brackets would be used: {[()]}. These brackets are avoided in this site since they typically have special meanings across programming languages.

When parentheses are not found, the correct order of operations is from more complex operations to simpler ones. For example, since multiplication is derived from addition, multiplication is performed before addition.

2 + 2 * 3 = 2 + 6 = 8.

^

Power (base ^ exponent): 2^8=256 (two to the eighth power equals 256).

In traditional math, we would use superscript (e.g. 28=256).

What this does is multiply the ase by itself the amount of times written in the exponent:

2^8 = 2*2*2*2*2*2*2*2.

We say X^2 is the square of X, and X^3 is the cube of X, and X^N is X to the Nth power.

There are negative exponents. They make more sense if we add a factor of 1 before. Observe:

2^3 = 1 * 2 * 2 * 2.

2^-3 = 1 / 2 / 2 / 2.

(2^3)*(2^-3) = (1 * 2 * 2 * 2) * (1 / 2 / 2 / 2) = 1.

There are fractional exponents, e.g. 4^(1/2) = 4^0.5 = 2.

When you have X^(1/2), that is the square root of X; and X^(1/3) is the cube root of X; and so on.

2^8 = 256.

256^(1/8) = 2.

~

Approximately: 10 / 3 = 3.33333~ (or ~3.33333).

%

Remainder of division: 10 % 3 = 1 (10 modulo 3 equals 1).

Although this sounds complicated, basically it makes the number "loop back" when it reaches the modulo. Observe:

 0 % 3 = 0
 1 % 3 = 1
 2 % 3 = 2
 3 % 3 = 0
 4 % 3 = 1
 5 % 3 = 2
 6 % 3 = 0
 7 % 3 = 1
 8 % 3 = 2
 9 % 3 = 0
10 % 3 = 1

In some programming languages, div means integer division, and mod means the remainder.

100 / 3 = 33.33333~.

100 div 3 = 33.

100 mod 3 = 100 - (3 * (100 div 3)) = 100 - (3 * 33) = 100 - 99 = 1.

I don't remember seeing something like this when I was in school, but it's very common in programming.

A simple example is that N % 2 is 0 if N is an even number, and 1 if N is an odd number, because any even number divided by 2 results in a remainder of 0.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *