# Square Root

• (2 Pages)
• 1
• 2

## 18 Replies - 98817 Views - Last Post: 31 July 2009 - 10:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=15349&amp;s=22f183e6e8d805a2359e64818302f583&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pauline

Reputation: 0
• Posts: 13
• Joined: 04-February 06

# Square Root

Posted 26 March 2006 - 11:29 AM

Hello...I have to write a function that returns the square root of a number. Does anyone know the formula for this? Thanks.
Is This A Good Question/Topic? 0

## Replies To: Square Root

### #2 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: Square Root

Posted 26 March 2006 - 11:57 AM

There are many different ways of computing the square root, some more accurate than others.

Here is a link that will show you several methods.

### #3 Xenon

• Connection failed

Reputation: 9
• Posts: 1,595
• Joined: 12-September 05

## Re: Square Root

Posted 26 March 2006 - 12:57 PM

best and simplest thing would be to use the direct method.

Quote

Private Sub Command1_Click()
Dim Num as Integer
'// Use any input, eg via a textbox or the inputbox method //
Num = InputBox("Enter")

'// now just raise it to half the power,
' the symbol "^" denotes the power symbol
Print Num ^ (1 / 2)
End Sub

EDIT

here's how to make a Sqrt function and successfully using it

Quote

Function Sqrt(x As Integer)
Dim root As Integer
root = x
root = root ^ (1 / 2)

'// One method would be print the output
Print root

'//second method would be to send
'// input to a textbox on a particular form
Form1.text1.Text = root

End Function

Private Sub Command1_Click()
Num = InputBox("Enter")

'// Calling the custom function
Call Sqrt(Num)
End Sub

This post has been edited by Xenon: 26 March 2006 - 01:09 PM

### #4 pauline

Reputation: 0
• Posts: 13
• Joined: 04-February 06

## Re: Square Root

Posted 26 March 2006 - 03:11 PM

Thank you. I was able to complete my project and it works perfect!

### #5 Xenon

• Connection failed

Reputation: 9
• Posts: 1,595
• Joined: 12-September 05

## Re: Square Root

Posted 26 March 2006 - 03:14 PM

could you just describe what your project was and what coding you did it would benefit us all

This post has been edited by Xenon: 26 March 2006 - 03:14 PM

### #6 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: Square Root

Posted 26 March 2006 - 11:03 PM

Xenon, on 27 Mar, 2006 - 12:19 AM, said:

best and simplest thing would be to use the direct method.

Quote

Private Sub Command1_Click()
Dim Num as Integer
'// Use any input, eg via a textbox or the inputbox method //
Num = InputBox("Enter")

'// now just raise it to half the power,
' the symbol "^" denotes the power symbol
Print Num ^ (1 / 2)
End Sub

EDIT

here's how to make a Sqrt function and successfully using it

Quote

Function Sqrt(x As Integer)
Dim root As Integer
root = x
root = root ^ (1 / 2)

'// One method would be print the output
Print root

'//second method would be to send
'// input to a textbox on a particular form
Form1.text1.Text = root

End Function

Private Sub Command1_Click()
Num = InputBox("Enter")

'// Calling the custom function
Call Sqrt(Num)
End Sub

Not really Xenon.
Instead use the library function SQR() to calculate the Square Root and save precious lines of code and improve readability.
```Sqr(25) returns 5
```

### #7 Xenon

• Connection failed

Reputation: 9
• Posts: 1,595
• Joined: 12-September 05

## Re: Square Root

Posted 27 March 2006 - 12:21 PM

well i thought he wanted a customised function .....

### #8 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: Square Root

Posted 28 March 2006 - 03:28 AM

But it still depends on the situation where the function is used.

### #9 NeoGreen

Reputation: 1
• Posts: 245
• Joined: 28-April 06

## Re: Square Root

Posted 28 April 2006 - 09:54 PM

I was just wanting to know if this was program is used for visual basic. If I was wanting to try this program could I just cut and paste to a notepad and save under .vbs file or would I have to do this under the visual basic program. Sorry I am new and as I was googling visual basic I stumbled upon this forum.

### #10 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: Square Root

Posted 29 April 2006 - 06:06 AM

Well...this is in the Visual Basic Section.

You can copy-paste Xenon's code (posted above).

Since you want to give it an extension VBS, I'm guessing you are trying to write the code in VBScript.

For the Square Root Function, the code will remain the same (I'm talking about Xenon's Square root function) so a simple copy-paste would do the job.

### #11 NeoGreen

Reputation: 1
• Posts: 245
• Joined: 28-April 06

## Re: Square Root

Posted 29 April 2006 - 09:39 AM

Which part do I use the first part or the second part? I tried the first quote my itself and got an eror message then I tried the second part by itself and got error message.

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Square Root

Posted 29 April 2006 - 01:16 PM

Where did you copy it to? An exisitng VB project? Are you using VB.NET. or VB6? Can you specify the exact error message you received?

### #13 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: Square Root

Posted 30 April 2006 - 04:04 AM

I think he's simply pasting it into Notepad and giving it the .VBS Extension.

### #14 NeoGreen

Reputation: 1
• Posts: 245
• Joined: 28-April 06

## Re: Square Root

Posted 30 April 2006 - 04:34 PM

Yeah, I am copying and pasting to notepad and saving it as a vbs file, here is what I copied and pasted:
Function Sqrt(x As Integer)
Dim root As Integer
root = x
root = root ^ (1 / 2)

'// One method would be print the output
Print root

'//second method would be to send
'// input to a textbox on a particular form
Form1.text1.Text = root

End Function

Private Sub Command1_Click()
Num = InputBox("Enter")

'// Calling the custom function
Call Sqrt(Num)
End Sub

and here is the error message I am getting:
Line: 1
Char: 17
Error: Expected ')'
Code: 800A03EE
Source: Microsoft VBScript compilation error

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Square Root

Posted 30 April 2006 - 06:46 PM

The error is generated because the function header specifies a variant type...it is a vb script:
``` <%
Function Sqrt(x)
Dim root
root = x
root = root ^ (1 / 2)
response.write "The root is " & root
End Function

'// Calling the custom function
Call Sqrt(25)
%>

```

Note that variables are not generally specified as being of one type of variant or another, as VBScript is a loosely typed language.

You may wish to review the differences between VBScript and actual VB. They are not the same. VBscript is a subset of VB, and is an interpreted language, not a compiled one.

Finally, VBscript, like VB, has a built in square root function.