Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ian-McCleary · Apr 09, 2015 at 03:49 AM · c#mathsquareequation

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 ();
     }
     
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mehul-Rughani · Apr 09, 2015 at 03:56 AM 0
Share

you can try this 3.14*r*r..hope u r looking for this

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sullyy1288 · Dec 03, 2017 at 07:35 AM 0
Share

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.

avatar image
1

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges