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

Join 132,445 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,438 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
post 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

Go to the top of the page

Programmist
post 9 Mar, 2007 - 09:47 AM
Post #2


Four-letter word

Group Icon
Joined: 2 Jan, 2006
Posts: 1,177



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

Go to the top of the page

numerical_jerome
post 15 Aug, 2008 - 07:14 PM
Post #3


New D.I.C Head

*
Joined: 16 Sep, 2007
Posts: 16


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

Go to the top of the page

NickDMax
post 15 Aug, 2008 - 07:55 PM
Post #4


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 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

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 10:56AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month