2.6 Math Methods

Processing has a number of methods to help with more involved math calculations. These will allow you to do things beyond basic arithmetic. Most of these methods will accept either int or float values and generate int or float values. Java has similar methods using the Math class. Their methods will also involve double and long values. To see how Java handles these methods you can look in the Java textbook

abs

Gives the absolute value of the argument. If the argument is an int then the absolute value will be an int. If the argument is a float then the absolute value will be a float.

Example

  • abs(-53.5) gives 53.5 as a float
  • abs(-15) gives 15 as an int
  • abs(99) gives 99 as an int

sqrt

Gives the square root of the argument. The square root will always be returned as a float.

Example

  • sqrt(25) gives 5.0 as a float
  • sqrt(2) gives 1.4142135 as a float
  • sqrt(-9) gives NaN

pow

Allows you to compute powers. This method requires two arguments. The first argument is the base of the power. The second argument is the exponent. The power will be always returned as a float.

Example

  • pow(2, 3) gives 8.0 as a float (the value of 23)
  • pow(-1.5, 5) gives -7.59375 as a float (the value of (-1.5)5)
  • pow(-10,-2) gives 0.01. (the value of (-10)-2 = 1 / 100)
  • pow(100, 1/2) gives 1.0 as a float (the value of 1000 since 1/2 uses integer division)
  • pow(100, 1.0/2) gives 10.0 as a float (the value of 1000.5 which is really the square root of 100)
  • pow(8, 1.0/3) gives 2.0 as a float (the value of 81/3 which is really the cube root of 8)
  • pow(-8, 1.0/3) gives NaN. If the exponent is a fraction and the base is negative you will always get NaN even if as in this case an answer is mathematically possible (-2)

round

Rounds the float parameter to the nearest int. If the decimal part is exactly .5 then the number is always rounded up (see examples below).

Example

  • round(6.7) gives 7 as a int
  • round(5.5) gives 6 as a int
  • round(-5.5) gives -5 as a int
  • round(145.4999) gives 145 as a int
  • round(-3.6543e1) gives -37 as a int
  • round(123.4567 * 100) / 100.0 gives 123.46 (ie. rounded to 2 decimal places)

random

There are two versions of the random function. The first version takes one argument. It will return a random float value between 0 and the argument. The random value could be 0 but it will always be less than the argument.

To simulate rolling a die you could say: int die = (int)random(6) + 1;
This works because random(6) will give a random value in this range: 0 ≤ random(6) < 6. By casting to an int you will then have a value in the set {0, 1, 2, 3, 4, 5}. Then adding 1 to that gives a number in the set {1, 2, 3, 4, 5, 6}.

The second version of the method takes 2 arguments. The first argument is the lowest possible float value you would like to be able to get. The random number will be less than the second argument. To generate a random x coordinate between 100 and 200 (could be 100 but will always be less than 200) you could do : float x = random(100, 200);

Example

To give the int variable number a random value uniformly distributed over the set {9, 12, 15, 18, 21, 24, 27} do the following:
number = (int)random(7) * 3 + 9;
random(7) gives a float greater than or equal to 0 and less than 7. We choose 7 because there are 7 numbers in that set. When you cast that result to an int you get an int in the set {0, 1, 2, 3, 4, 5, 6}. When you multiply that by 3 you get a number in the set {0, 3, 6, 9, 12, 15, 18} and then finally when you add 9 you get the required set.

Constants

Processing has some predefined constants that can help with math and graphics. The value of π is given by the constant PI. There are also constants QUARTER_PI, HALF_PI and TWO_PI.

Exercise 2.6

  1. State the value and type of each expression
    1. abs(-5) - abs(-7)
    2. abs(-1e-1) + abs(-2e-2)
    3. sqrt(0.0064)
    4. sqrt(pow(2.7, 2))
    5. round(3.499)
    1. pow(16, 0.25)
    2. pow(4, -2)
    3. round(1.49 + 0.1)
    4. round(1.49) + 0.1
  2. Write as Processing expressions.
    1.  
  3. Write a statement that will make the int variable result take on a random value uniformly distributed over the given set.
    1. {1, 2, 3, ..., 10}
    2. {1, 2, 3, ..., 52}
    3. {5, 10, 15, ..., 100}
    4. {-5, -4, -3, ..., 5}
    5. {100, 110, 120, ..., 300}
    6. {a, a + b, a + 2b, ..., a + kb} where a, b and k are integers.
  4. Write a statement that will assign the char variable randChoice a random value from the set {'A', 'B', 'C', 'D', 'E'}.
  5. Write a statement that will assign the float variable randVal a random value from the set {1.00, 1.25, 1.50, ..., 4.00}
  6. Write a program that will input a float value from the user. Round the number to the nearest tenth and store it in a variable. Round the number to the nearest thousand and store in another variable. Print the original value and the two rounded values.