Visual C# 2005 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.

FunctionDescriptionAcceptsReturns
Math.Abs(x)Absolute value of x (removes sign)Any numeric data typeThe same type
Math.Acos(x)Returns the angle whose cosine is the xDoubleDouble
Math.Asin(x)Returns the angle whose sine is xDoubleDouble
Math.Atan(x)Returns the angle whose tangent is xDoubleDouble
Math.Ceiling(x)Rounds x up to the nearest integerDouble/DecimalDouble/Decimal
Math.Cos(x)Returns the cosine of xDoubleDouble
Math.Exp(x)Returns e raised to the power of xDoubleDouble
Math.Floor(x)Rounds x down to the nearest integerDouble/DecimalDouble/Decimal
Math.Max(x,y)Returns the largest of x and y.Any numeric data typeThe same type
Math.Min(x,y)Returns the smallest of x and y.Any numeric data typeThe same type
Math.Pow(x,y)Returns x raised to the power of y.Double, DoubleDouble
Math.Round(x,y)Returns x rounded to y decimal placesDouble, IntegerDouble
Math.Sign(x)Returns -1 if x is negative, 0 if it is zero and 1 if positive.Any numeric data typeThe same type
Math.Sin(x)Returns the sine of xDoubleDouble
Math.Sqrt(x)Returns the square root of xDoubleDouble
Math.Tan()Returns the tangent of xDoubleDouble
Math.ToString(x)Returns a human-readable representation of the objectAny objectString
Math.Truncate(x)Returns the integer part of xDouble/DecimalInteger

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

  1. Write a program that uses Pythagoras' theorem to calculate the length of the longest side.
  2. 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.
  3. Write a program that converts degrees to radians and rounds the result appropriately.
  4. 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.