Jump to content

How can I calculate a square root of a number in c#?

Desktop 1: CPU: Intel Core i7 4770  GPU: Nvidia Geforce GTX 960 Ram: Crucial DDR3 2x8GB 1600 MHz  Storage: Samsung 850 Evo 250GB and Segate 1TB Hard drive  Desktop 2: CPU: Intel Pentium G3258  GPU: AMD R7 250  Ram: Corsair Vengance DRR3 2x8GB 1600 MHz  Mobo: MSI H81M-E33  PSU: Corsair CX430M  Case: Rosewill MicroAtx Mini Tower

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/
Share on other sites

Link to post
Share on other sites

a = sqrt(x);

 

don't forget the math.h lib.

Could you give me some more example code?(new to c#)

Desktop 1: CPU: Intel Core i7 4770  GPU: Nvidia Geforce GTX 960 Ram: Crucial DDR3 2x8GB 1600 MHz  Storage: Samsung 850 Evo 250GB and Segate 1TB Hard drive  Desktop 2: CPU: Intel Pentium G3258  GPU: AMD R7 250  Ram: Corsair Vengance DRR3 2x8GB 1600 MHz  Mobo: MSI H81M-E33  PSU: Corsair CX430M  Case: Rosewill MicroAtx Mini Tower

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108577
Share on other sites

Link to post
Share on other sites

Could you give me some more example code?(new to c#)

 

 
public double SquareRoot(double x)        {           return Math.Sqrt(x)        }

Simple function.

Laptop: Acer V3-772G  CPU: i5 4200M GPU: GT 750M SSD: Crucial MX100 256GB
DesktopCPU: R7 1700x GPU: RTX 2080 SSDSamsung 860 Evo 1TB 

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108596
Share on other sites

Link to post
Share on other sites

are you talking about c#.net? working in visual studio?
if yes, look above answer of @JuztBe

 

you can either put the Math.Sqrt in a function or use inline.

Don't forget to assign it to a variable.

 

double squareRoot = Math.Sqrt(x);

Or

double squareRoot = SquareRoot(x); //If using function as answered by @JuztBe

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108599
Share on other sites

Link to post
Share on other sites

are you talking about c#.net? working in visual studio?

Yes

Desktop 1: CPU: Intel Core i7 4770  GPU: Nvidia Geforce GTX 960 Ram: Crucial DDR3 2x8GB 1600 MHz  Storage: Samsung 850 Evo 250GB and Segate 1TB Hard drive  Desktop 2: CPU: Intel Pentium G3258  GPU: AMD R7 250  Ram: Corsair Vengance DRR3 2x8GB 1600 MHz  Mobo: MSI H81M-E33  PSU: Corsair CX430M  Case: Rosewill MicroAtx Mini Tower

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108605
Share on other sites

Link to post
Share on other sites

Could you give me some more example code?(new to c#)

For standart C# finctions there are a lot of examples on the internet. Just google 2 minutes and you find it. It's faster than writhing a question here.

An example: http://www.dotnetperls.com/sqrt

Mineral oil and 40 kg aluminium heat sinks are a perfect combination: 73 cores and a Titan X, Twenty Thousand Leagues Under the Oil

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108606
Share on other sites

Link to post
Share on other sites

 

 
public double SquareRoot(double x)        {           return Math.Sqrt(x)        }

Simple function.

 

Thanks this helped a lot, but how can I convert an int to a double?

Desktop 1: CPU: Intel Core i7 4770  GPU: Nvidia Geforce GTX 960 Ram: Crucial DDR3 2x8GB 1600 MHz  Storage: Samsung 850 Evo 250GB and Segate 1TB Hard drive  Desktop 2: CPU: Intel Pentium G3258  GPU: AMD R7 250  Ram: Corsair Vengance DRR3 2x8GB 1600 MHz  Mobo: MSI H81M-E33  PSU: Corsair CX430M  Case: Rosewill MicroAtx Mini Tower

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108662
Share on other sites

Link to post
Share on other sites

Thanks this helped a lot, but how can I convert an int to a double?

 

double aDouble = Convert.ToDouble(a);

Don't be afraid of a google. You will be able to find everything you want. ;)

Laptop: Acer V3-772G  CPU: i5 4200M GPU: GT 750M SSD: Crucial MX100 256GB
DesktopCPU: R7 1700x GPU: RTX 2080 SSDSamsung 860 Evo 1TB 

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5108722
Share on other sites

Link to post
Share on other sites

Thanks this helped a lot, but how can I convert an int to a double?

 

You can declare x as int.

 

x as Double, was just as an example.

It can be int as well, but the result of square root could be double at times.

Desktop:

CPU : i5 4440 | Motherboard : Gigabyte B85M-D3H | RAM : Kingstone HyperX blu 4GB x2 | GPU : Asus R9 280X DC II Top [RIP 2017] | PSU : Corsair VS 550W | Display(s) : Dell S2240L | Mouse : Logitech G400s | Operating System : Windows 7 64bit

 

Laptop:

Acer Predator Helios 300 (CPU: Intel Core i5 7300HQ | GPU: GTX 1050ti | RAM: 16GB RAM | Operating System: Windows 10 64bit)

Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5109239
Share on other sites

Link to post
Share on other sites

@MaximusX

 

It's good to learn to use the documentation (and Google). Most programming languages have a basic Math library and C# is no different. Here is the documentation page for it which contains information on all the methods available in it. You can then click on the function you need for even more information if necessary. For example, here's the Sqrt function. Sqrt takes a double as a parameter and returns a double.

double d = Math.Sqrt(4.0);

Here is some information about casting and type conversions. You'll do it a lot when programming so it's good to learn more about it. int to double is an implicit cast, so you don't have to do anything special.

// this is valid and is an implicit castint i = 100;double d = i;// the other way around requires an explicit castdouble d = 10.8;int i = (int) d; // i contains 10 because the decimal place is truncated (ie: removed without any rounding)

So you can do something like this

int value = 100;int result = (int) Math.Sqrt(value); 
Link to comment
https://linustechtips.com/topic/378013-c-square-root-help/#findComment-5109624
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×