2.5 Math Flashcards
(13 cards)
Function
A list of statements executed by invoking the function’s name, with such invoking known as a function call. Any function input values, or arguments, appear within ( ), and are separated by commas if more than one. Below, the function SquareRoot is called with one argument, areaSquare. The function call evaluates to a value, as in SquareRoot(areaSquare) below evaluating to 7.0, which is assigned to sideSquare.
Coral’s Builtin math functions
- SquareRoot(x)
- RaiseToPower(x,y)
- AbsoluteValue(x)
RandomNumber() function
- Built-in Coral function that takes two arguments, lowValue and highValue, and returns a random integer in the range lowValue to highValue.
- Ex: RandomNumber(1, 10) returns a random integer in the range 1 to 10.
SeedRandomNumbers() Function
A programmer can specify the seed using the function SeedRandomNumbers(), as in SeedRandomNumbers(10) or SeedRandomNumbers(99). Note that the seeding should only be done once in a program, before the first call to RandomNumber().
Seed
- For the first call to RandomNumber(), no previous random integer exists, so the function uses a built-in integer known as the seed.
- Coral automatically seeds the pseudo-random number generator with a number based on the current time.
- Since, the time is different for each program run, the program will get a unique sequence.
pseudo-random
- The “pseudo” is because the sequence is the same for the given seed, with the next “random” number computed from the previous one.
- But the sequence does not repeat itself; millions of numbers can be generated without reaching a repeating pattern within the sequence.
Floating Point Division
Produces integers with fractionals. Will run if at least 1 side of the / is float
Integer Division
Always produces a whole number, does not round, drops fractional
Type conversion
conversion of one data type to another, such as an integer to a float.
implicit conversion
Coral automatically performs several common conversions between integer and float types, and such automatic conversion is known as implicit conversion.
How type conversion works
- For an arithmetic operator like + or *, if either operand is a float, the other is automatically converted to float, and then a floating-point operation is performed.
- For assignments, the right side type is converted to the left side type.
- integer-to-float conversion is straightforward: 25 becomes 25.0.
- float-to-integer conversion just drops the fraction: 4.9 becomes 4.
Type cast
- Converts a value of one type to another type.
- A programmer can type cast an integer to float by multiplying the integer by the float literal 1.0. Ex: If myIntVar is 7, then myIntVar * 1.0 converts integer 7 to float 7.0.
- Another common error is to cast the entire result of integer division, rather than the operands, thus not obtaining the desired floating-point division.
!!!! Review!!!!
modulo operator (%)
Evaluates (division) to the remainder of the division of two integer operands. Ex: 23 % 10 is 3.
Integer only
Examples:
- 9 % 5 is 4. Reason: Since 9 = 5 * 1 + 4, the integer division 9 / 5 results in 1, and the remainder is 4.
- 70 % 7 is 0. Reason: 70 / 7 is 10 with remainder 0.
- 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1.
- 10 % 4.0 is not valid. “Remainder” only makes sense for integer operands.