Sunday, January 20, 2013

5.9.2. Mathematical functions


There are many mathematical functions in C#, such as:

  • Abs(x): Absolute value
  • Acos(x): Arc cosine
  • Asin(x): Arc sine
  • Atan(x): Arc tangent
  • Atan2(y,x): Arc tangent of y/x (in case x or y are 0)
  • Ceiling(x): The nearest integer which is greater than x
  • Cos(x): Cosine
  • Cosh(x): Hyperbolic cosine
  • Exp(x): e raised to x
  • Floor(x): The greatest integer which is less than x
  • Log(x): Natural logarithm (o base "e")
  • Log10(x): Logarithm base 10
  • Pow(x,y): x raised to y
  • Round(x, cifras): Rounds a number
  • Sin(x): Sine
  • Sinh(x): Hyperbolic sine
  • Sqrt(x): Square root
  • Tan(x): Tangent
  • Tanh(x): Hyperbolic tangent

(most of them use "double" parameters X and Y)

and a few constants, such as

E, number "e", 2.71828...
PI, number "Pi" (p), 3.14159...

All of them are used prefixed with "Math."

Most of them are specific to certain mathematical problems, especially if it involves trigonometry, logarithms or exponentials. But some of them they can be useful in many situations:

The square root of 4 would be x = Math.Sqrt(4);
Power: to raise 2 to 3 we would do y = Math.Pow(2, 3);
The absolute value: if we want to work only with positive numbers,  n = Math.Abs(x);

Suggested exercises:
·       (5.9.2.1) Create a program to find any root of a number. The user must indicate the number (for example, 2) and the index of the root (eg, 3 for the cube root). Hint: Finding the cube root of 2 is the same as raising 2 to 1/3.
·       (5.9.2.2) Create a program to solve quadratic equations of the type ax2 + bx + c = 0.  The user must enter the values ​​of a, b and c. The program must contain a function "quadraticSolutions", and receive as parameters the coefficients a, b, and c (by value) and the solutions x1 and x2 (by reference). You must return the values ​​of the two solutions x1 and x2. If a solution does not exist, it must return 100,000 for that solution. Hint: the solution is calculated as follows
x = -b ± root (b2 – 4·a·c)  / 2·a
·       (5.9.2.3) Create a program that asks the user for five numeric data, stores them in an array, asks for another number and shows the value of the array that is nearest to that number, being greater than it.
·       (5.9.2.4) Create a program that asks the user for five numeric data, stores them in an array, asks for another number and shows the value of the array that is nearest to that number in absolute value.
·       (5.9.2.5) Create a function to calculate the distance between two points (x1,y1) y (x2,y2), using the expression d = root [ (x1-x2)2 + (y1-y2)2 ].
·       (5.9.2.6) Create a program that displays the values ​​of the function y = 10 * sin (x * 5), for values ​​of x between 0 and 72 degrees. Remember that the trigonometric functions expect the angle in radians, not degrees. The equivalence is 360 degrees = 2 * PI radians.
·       (5.9.2.7) Create a program to "draw" the graph of the function y = 10 * sin (x * 5), for values ​​of x between 0 and 72 degrees. To help you to write in any coordinates, you can use a two-dimensional array, which you will fill first and then dump on screen (see the exercise 5.9.1.3).
·        (5.9.2.8) Create a program to "draw" a circle within a two-dimensional array, using the equations x = xCenter + radius * cos(angle), y = yCenter + radius * sin(angle). If your array is 24x79, the coordinates of the center might be (12,40). Remember that the angle must be specified in radians (see exercise 5.9.2.6).

No comments:

Post a Comment