- Home
- Products
- Articles
- 4 Axis Robot Arm
- A Stepper Motor Driven Ferris Wheel (Version 3 Compiler required)
- An Update on Keith Cameron's Lift
- Automatic Tram Layout
- Bug in Increment Word Instruction
- Compiler Version Releases
- Industrial Arc Welding Robot
- MotorVating the Speed Play Robot
- Reproducing the Electronic Set Models
- Sensor Expansion Port Pinout Diagram
- Sheet 1 - Hardware from NZ 240 volt wall socket to...
- Sheet 3 Meccano Car model controlled by the MotorVator and Director.
- Sheet 2 - Hardware from Motor(s) to MotorVator®.
- User Input Options
- Version 3 Manual Released
- 16/32 Bit Maths Routines
- Build Your Own Photo Sensor
- Controlling Stepper Motors
- Magnetic reed switch as a non contact sensor
- MeccCompiler III Tutorial
- More Inputs and/or Motors: Port Replication
- More Inputs: the Sensor Expansion Port
- Square Root Function
- Tutorial: Rev Counter
- Use of the opto switch or opto interrupter as a ro...
- FAQ
Square Root Function
I'm working on a project that needs a square root function.
Here's what I've come up with, for others to use.
This method dating back to the Babylonians (1700 B.C.) is known as the Babylonian Algorithm.
If A > 0 is a guess of the square root of the positive number Q, then a better approximation to SQRT(Q) is given by
B = (A + Q/A)/2.
;;###############################
; square root function
;
; This method dating back to the Babylonians (1700 B.C.) is known as the Babylonian Algorithm.
; If A > 0 is a guess of the square root of the positive number Q,
; then a better approximation to ?Q is given by B = (A + Q/A)/2.
Declare Function sqroot(target As Word) Returns Word
Declare Word guess ; the [improving] estimate of the sqrot
Declare Word lastguess ; what we guessed last time around
Declare Word closeenough = 1 ; how close do we need to get
Declare Word change ; when checking guess against lastguess
Declare Word tempw
guess = target / 2 ; guess half way
lastguess = 0
change = guess - lastguess
While change > closeenough ; check how close this guess is to last one
tempw = target / guess ; newguess = (Target/Guess + Guess) / 2
guess = guess + tempw
guess = guess / 2
If guess > lastguess Then
change = guess - lastguess
Else
change = lastguess - guess
End If
lastguess = guess ; remember for next time around
End While
Return guess
End Function
;###############################
Begin Program
Declare Word answer
Declare Byte answerb
answer = sqroot(2906)
answerb = LowByte(answer) ; so we can display it
DisplayNumber answerb
Wait 500
End Program