Re: How to square root in 4gl
Posted in 1997
Hi! We had to use this sometime ago. We chose to resort to a mathematical method learnt in college. I call it "Divide and Conquer," but the real name is the Newton-Raphson Method (or is it?.. not sure of the name or its spelling). Basically it's an iterative method. Let's say you're looking for the square root of 27. You start with integers 0 (zero) and 1 (one). You square them and check to see if 27 is between the squares, i.e. is 27 between zero and one? If no, then is it greater than one? If yes, then you repeat with one and two. So your new test is: is 27 between one and four? No, so is it greater than four? Yes, so increase to check between two-squared (i.e. four) and three-squared (i.e. nine). You get the point. Eventually, you realize that the square-root of 27 lies somewhere between five and six, since 27 lies between five-squared (i.e. 25) and six-squared (i.e. 36). So, now you repeat the test with five squared and five-point-five (5.5), or: new lower limit for test = five new upper limit for test = 5 + ((6 - 5) / 2) i.e. new upper limit for test = old lower + ((old upper - old lower) / 2 You already know the number is greater that five, so you test now to see if 27 is greater than or less than (5.5)-squared (i.e. 30.25). You see it's less, so five remains your lower test limit, and your upper limit becomes: upper limit for test = 5 + ((5.5 - 5) / 2) = 5.25 If it so happened that it was greater, then your new lower limit would be the old upper limit (i.e. 5.5) and the new upper limit would be 5.75 (5+((6-5.5)/2). Your repeat this until you get your answer, but you'd have to know when to stop repeating the test, since you quite likely would not get an exact figure. Hence you'd have to establish some amount of significant figures for your limits so that when it reaches the point where further subdivisions of testing ranges would not impact on your answer to some significant figure after the decimal point, your processing stops and you have an acceptable answer. I don't have the exact code my fellow programmer produced, so I can't pass it on to you. Besides, this would give you an opportunity to try out some 4GL library functions like trunc or even round, and get into loops like you never did before! :) When you do come up with some code, perhaps you could take it one step further and convert it to SPL (stored procedure), which would make it available to all your developers. Have fun. Let me know if you get a better solution. At 11:05 PM 12/10/97 EDT, Catalin Dumitrache wrote: > >Is there a way to find the square root in 4gl ? >I do not want to call a C function. > >Thanks >Catalin Dumitrache (EXE Technologies - Melbourne). > Best regards, Nigel +-------------------------------------------------------------+ |Name : Edmund Nigel Gall Tel: (868) 636 3153 | |Title : Information Systems Specialist Fax: (868) 679 3770 | |Company: Process Plant Services Limited | |Address: Atlantic Avenue, Point Lisas Industrial Estate | | Point Lisas, Couva, Trinidad & Tobago, W.I. | +----- mailto:nigelg@ppsl.com ------ http://www.ppsl.com -----+