Math Operators

advertisement
Computer Studies - Math Operators
Math Operators
An arithmetic operator is a special function that evaluates expressions and returns a result. These operators
work with one or more operands.
Numeric data types
There are many different numeric data types in C#. You should select the data type that best matches your
needs. They can be categorized into integral types, floating point types, and decimal types.
Integral Data types
Data type
byte
short
int
long
Range of Values
0-255
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Storage
8 bits
16 bits
32 bits
64 bits
Floating Point / Decimal Data Types
Data type
float
double
decimal
Range of Values
±1.5e−45 to ±3.4e38
±5.0e−324 to ±1.7e308
-7.9 x 1028 to 7.9 x 1028
Precision
7 digits
15-16 digits
28-29 digits
Storage
32 bits
64 bits
128 bits
Examples
1. Need to maintain fractional components of a value?
 double or decimal
2. Need to represent financial information (requires a high degree of precision)?
 decimal
3. Need to represent a distinct count of items?
 int
A numeric literal is a number that is directly written in the program.


If the numeric literal value does not have a fractional component then C# will assume it is an int.
If the numeric literal value has a fractional component then C# will assume it is a double.
Examples
Expression
5*4
0.7 / 4.5
Computer Science Notes
Operator
multiplication
division
Operand
5
0.7
[1]
Operand
4
4.5
Result
20
1.75
Math Operators
Computer Studies - Math Operators
Basic Integer Operators
C#’s integer data type support addition, subtraction, multiplication, and division. Use the symbols +, –, *, and
/ for these operations. The result of these operations is always an integer value. This may not be what you
intended when you are performing a division operation.
Example
5 / 2
The result of this expression would be 2 and not 2.5 as you might expect. This is known as integer division.
If you really want to perform the operation and keep the fractional components you should ensure that one
or more of the numeric literals is a double. This can be accomplished by adding a decimal to the value.
Example
5.0 / 2.0
Now the result of the expression would be 2.5. (real number division)
Decimal Literals
Decimal literals are used to hold real numbers with more precision than the double data type. To indicate a
numeric literal should be considered a decimal type use the m or M suffix.
Example
8 * 15.25m;
This expression might represent a worker who worked for 8 hours at $15.25. We want to maintain precision
so we include the suffix m at the end of the wage to ensure the result is of type decimal.
Visual Studio will not even let you compile the code if you do not use the prefix when needed.
For example:
decimal wage = 15.25;
Results in a syntax error. Therefore be sure to include the suffix when needed.
Order of Operations
As you might expect C# will follow the standard order of math operations (BEDMAS).
1.
2.
3.
4.
Brackets
Exponents
Division/Multiplication
Addition/Subtraction
Be sure to include brackets/parentheses where needed to enforce the proper order of operations.
Example
Converting Temperature for Fahrenheit to Celsius
tempC = (tempF – 32.0) * 5.0/9.0
Notice how the parentheses were used and all numeric literals were doubles (included decimal). This would
ensure the result is a double data type.
Computer Science Notes
[2]
Math Operators
Computer Studies - Math Operators
The Modulus Operator
The modulus operator (%) performs division but instead of returning the quotient it returns the remainder.
This operator is at the same precedence level as Division/Multiplication.
Example
5 % 2
The result is 1.
4 % 2
The result is 0.
2 % 3
The result is 2.
Advanced Math Operations
Special Math Methods and Values
Sometimes you want to access special math values or functions. Fortunately, .NET comes with a bunch of
special Math methods you can use. Here are some examples
:
Math.PI
Example
Console.WriteLine(Math.PI);
3.14159265358979
Rounding:
Math.Round(value, digits)
Console.Math.Round(23.346,2)
23.35
Exponents:
Math.Pow(base, exp)
Console.Math.Pow(2,3)
8
Square Root:
Math.Sqrt(value)
Console.Sqrt(14)
4
Trigonometry:
Computer Science Notes
Math.Sin(value)
Math.Cos(value)
Math.Tan(value)
[3]
Math Operators
Computer Studies - Math Operators
Formatting Numeric Output
There are a few different methods of formatting numeric output. The most common formatting requirement
is to display the answer of a real number accurate to a specific number of decimal places.
The first method is to create a special string by calling the String.Format() method. The method
accepts a formatted string and one or more variables/expressions.
In Method 1,
a new string formatOutput is created as a formatted version of the intended output.
The String.Format method is called with a delimited string with the text to be displayed and
where the numeric value is to be inserted/substituted. The insertion point(s) is/are indicated with
the curly brackets {}. The contents of the brackets include an index reference to the variable to be
substituted (in this case 0 (zero) as it is the first and only value), followed by a colon (:) and the
formatting instruction (f2). The (f2) instruction will display/round the floating point value stored in
tempC to 2 decimal places.
Method 2 is very similar as it uses the same formatting options, but the formatting string is used directly in
the WriteLine method instead of creating a new string with the formatted output.
Method 3 uses the same format option (f2), but from within the ToString() method. Notice how a
comination of Write() and WriteLine() methods are used in this version.
Computer Science Notes
[4]
Math Operators
Computer Studies - Math Operators
Exercises
1. Fill in the table with the result and the data type of the result.
Expression
Result
Data Type
12 + 6 / 3
17 % 3 – 2 * 1.1
8 + 12 * (6 – 2)
4 / 3
(3+(4-2.0))+ 6/(4/2)
3 * Math.Sqrt(25)
100 % Math.Pow(5,2)
100 / Math.Pow(5,2)
2. Specify the appropriate primitive numeric data types for these values:
a. 24 dollars
b. 12 bananas
c. 14.5 cm
d. 83 cents
e. 5 cats
Challenges
1. Write a console program that will ask the user for a radius of a circle and calculate the area of the
circle and display its value (correct to 2 decimal places).
2. Write a console program that will ask the user the vehicle's fuel efficiency rating (litres / 100 km) and
ask them for the size of their fuel tank (litres). The program should display the distance the user can
travel on a full tank of fuel (correct to 1 decimal place).
When you have completed these 2 challenges show the teacher your application.
Computer Science Notes
[5]
Math Operators
Download