Visual C# Guide
Math Functions
Not a spelling mistake or an americanism. Bear in mind that American English is spoken by more people on the planet than our quaint old version. As you would expect, there are lots more of them programming too. Expect lots of these things in programming languages. You can expect the word color to show up later on.
The Math Class has lots of ready-made maths stuff for us to use as well as a couple of useful constants,
Math.E
Math.PI
The Functions
The table below is a list of most of the methods in the Math class. The Math class is part of the .NET class library. It is the same for all of the languages in the .NET family. In the table, the letters x and y are used to represent the values passed to the function. These are called the parameters of the function call.
Function | Description | Accepts | Returns |
Math.Abs(x) | Absolute value of x (removes sign) | Any numeric data type | The same type |
Math.Acos(x) | Returns the angle whose cosine is the x | Double | Double |
Math.Asin(x) | Returns the angle whose sine is x | Double | Double |
Math.Atan(x) | Returns the angle whose tangent is x | Double | Double |
Math.Ceiling(x) | Rounds x up to the nearest integer | Double/Decimal | Double/Decimal |
Math.Cos(x) | Returns the cosine of x | Double | Double |
Math.Exp(x) | Returns e raised to the power of x | Double | Double |
Math.Floor(x) | Rounds x down to the nearest integer | Double/Decimal | Double/Decimal |
Math.Max(x,y) | Returns the largest of x and y. | Any numeric data type | The same type |
Math.Min(x,y) | Returns the smallest of x and y. | Any numeric data type | The same type |
Math.Pow(x,y) | Returns x raised to the power of y. | Double, Double | Double |
Math.Round(x,y) | Returns x rounded to y decimal places | Double, Integer | Double |
Math.Sign(x) | Returns -1 if x is negative, 0 if it is zero and 1 if positive. | Any numeric data type | The same type |
Math.Sin(x) | Returns the sine of x | Double | Double |
Math.Sqrt(x) | Returns the square root of x | Double | Double |
Math.Tan() | Returns the tangent of x | Double | Double |
Math.ToString(x) | Returns a human-readable representation of the object | Any object | String |
Math.Truncate(x) | Returns the integer part of x | Double/Decimal | Integer |
Angles In C#
C#, like most programming languages does not work in degrees when performing trigonometric operations. Instead it works in radians. Worry not though, little one, it is easy to convert between the two.
Angle In Radians = Angle In Degrees * Math.Pi/180
Having A Go
- Write a program that uses Pythagoras' theorem to calculate the length of the longest side.
- Freda is paid £4.80 and hour. This rate is for the first 35 hours that she works. Any overtime she works is paid at time and a half. Input the number of hours worked and calculate the wage due. Assume that she works more than 35 hours each week. Round the value down and display the result nicely.
- Write a program that converts degrees to radians and rounds the result appropriately.
- Write a program which allows the user to enter an amount of money as a whole number. Work out how to make this quantity in £20, £10, £5 notes and £2 and £1 coins.