2.5 Math Flashcards

(14 cards)

1
Q

Dividing a nonzero floating-point number by zero is undefined in regular arithmetic. Many programming languages produce an error when performing floating-point division by 0, but Coral does not. Coral handles this operation by producing infinity or -infinity, depending on the signs of the operands. Printing a floating-point variable that holds infinity or -infinity outputs Infinity or -Infinity.

If the dividend and divisor in floating-point division are both 0, the division results in a “not a number”. Not a number indicates an unrepresentable or undefined value. Printing a floating-point variable that is not a number outputs NotANumber.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Coral’s Builtin math functions

A
  • SquareRoot(x)
  • RaiseToPower(x,y)
  • AbsoluteValue(x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

RandomNumber() function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SeedRandomNumbers() Function

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Seed

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pseudo-random

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Floating Point Division

A

Produces integers with fractionals. Will run if at least 1 side of the / is float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Integer Division

A

Always produces a whole number, does not round, drops fractional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Type conversion

A

conversion of one data type to another, such as an integer to a float.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

implicit conversion

A

Coral automatically performs several common conversions between integer and float types, and such automatic conversion is known as implicit conversion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How type conversion works

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Type cast

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

!!!! Review!!!!
modulo operator (%)

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly