Welcome to Dream.In.Code
Getting Help is Easy!

Join 135,961 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,521 people online right now. Registration is fast and FREE... Join Now!




Compute Square Root - using Scheme

 
Reply to this topicStart new topic

Compute Square Root - using Scheme, Cannot compute square root

LionessHeadstrong
9 Mar, 2007 - 01:21 AM
Post #1

New D.I.C Head
*

Joined: 23 Jun, 2006
Posts: 2


My Contributions

Hello

I am trying to write a program in Dr Scheme (Lisp - scheme) that will compute a square root for me. This is what I have so far:

(define value (lambda (x) (- x 0.00000001) (* x x))
(if (lambda (= x value) x)))

But this doesn't work, anyone have any ideas?

Thanks for your help

Linda

User is offlineProfile CardPM
+Quote Post

Programmist
RE: Compute Square Root - Using Scheme
9 Mar, 2007 - 09:47 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,179



Thanked: 6 times
Dream Kudos: 100
Expert In: Java

My Contributions
http://www.google.com/search?hl=en&q=s...G=Google+Search
User is offlineProfile CardPM
+Quote Post

numerical_jerome
RE: Compute Square Root - Using Scheme
15 Aug, 2008 - 07:14 PM
Post #3

New D.I.C Head
*

Joined: 16 Sep, 2007
Posts: 16



Thanked: 1 times
My Contributions
if this was a school assignment, this might be a little far from what your professor wanted, but

[math]
sqrt(x) == x^0.5
x == e^(ln(x))
x^0.5 == e^(0.5*ln(x))
sqrt(x) == e^(0.5*ln(x))
[/math]

this is actually very fast, as your floating point unit has an optimized natural log / natural exponent

in common lisp this looks like:

CODE

(defun my_sqrt (a)
    (exp (* 0.5 (log a))))


in scheme this would be something like:

CODE

(define my_sqrt (lambda (a)
    (exp (* 0.5 (log a)))))


If your instructor complains, demonstrate the great speed at which your function evaluates, and point out how the fancier bits of the FPU can be developed from ln, e using a similar technique

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Compute Square Root - Using Scheme
15 Aug, 2008 - 07:55 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 48 times
Dream Kudos: 550
My Contributions
The posts are old but thanks for the responses. I used to try to answer scheme and lisp questions but it really hurts my head... welcome to DIC!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:33AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month