Square Root

Square Root

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 71303 Views - Last Post: 31 July 2009 - 10:19 PM Rate Topic: -----

#1 pauline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 1

#3 Xenon  Icon User is offline

  • Connection failed
  • member icon

Reputation: 8
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 pauline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 Xenon  Icon User is offline

  • Connection failed
  • member icon

Reputation: 8
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • Posts: 4,667
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Xenon  Icon User is offline

  • Connection failed
  • member icon

Reputation: 8
  • View blog
  • 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 .....
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Square Root

Posted 28 March 2006 - 03:28 AM

Yea...you're right. *my bad*
But it still depends on the situation where the function is used.
Was This Post Helpful? 0
  • +
  • -

#9 NeoGreen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 243
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • Posts: 4,667
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 NeoGreen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 243
  • 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. :unsure:
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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?
Was This Post Helpful? 0
  • +
  • -

#13 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • Posts: 4,667
  • 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.

We'll need the error message to help you out, NeoGreen.
Was This Post Helpful? 0
  • +
  • -

#14 NeoGreen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 243
  • 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:
Script C:\Document and Settings HP_Admin
Line: 1
Char: 17
Error: Expected ')'
Code: 800A03EE
Source: Microsoft VBScript compilation error

Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2