- Home /
How to you make a number squared in C#?
I have looked around to the answer to this question, but i cant seem to find a way. I know i can multiply the number by itself, but that would add a complication that i may not know how to fix with my basic coding knowledge.
I am basicly trying to find the area of a circle and the eqation is πr(squared). I just dont know how to square my math equation. Here is the part of the script where i am doing the math.
(the numbers were entered into inputfield boxes so i had to convert it in the first 3 statements.)
public void Product() {
int a = Convert.ToInt32(t1.text);
int b = Convert.ToInt32(t2.text);
int c = Convert.ToInt32 (t3.text);
int d = a/2*3.14;
Result.text = d.ToString ();
}
}
you can try this 3.14*r*r..hope u r looking for this
Answer by sparkzbarca · Apr 09, 2015 at 04:25 AM
pi is of course an infinitely large number of decimals, unity contains a close approximation
mathf also has a function called pow which takes a number raised to a power
float radius;
radius = ...(decide yourself)
float area = Mathf.Pow(Mathf.Pi * radius, 2);
now area equals pi * radius squared
alternately
float radius;
radius = ...(decide yourself)
float area = 3.14159 * radius;
area = area * area;
or
float area = 3.14159 * radius;
area *= area;
or
area = (3.14159 * radius) * (3.14159 * radius);
or
basically you can either use mathf.pi instead of 3.14159 or any other approximation of pi.
and you can manually do the math yourself or use the function
mathf.Pow( number to raise to a power, power to raise to);
so
mathf.Pow(2, 2) equals 4
mathf.Pow (2, 3) equals 8
mathf.Pow (3.14159 * radius, 2) equals area of a circle
This is accurate for taking the power of something but this gets the actual area of a circle formula wrong - it's pi * r^2 not (pi * r)^2.
Answer by Eno-Khaon · Apr 09, 2015 at 04:20 AM
Method 1 (as Mehul Rughani stated):
float area = Mathf.pi * radius * radius;
Method 2 (less efficient for only a square, but potentially more scalable otherwise):
float area = Mathf.pi * Mathf.Pow(radius, 2.0f);
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Looking for a way to solve a 4th degree polynomial equation with constraints at runtime 1 Answer
Multiple Cars not working 1 Answer
Explosion damage script only deals damage in the first explosion and none afterwards 0 Answers
Drawing a Mandelbrot Set problem 0 Answers