0
package week1
import math.abs
object newton {
    def abs(x:Double) = if (x < 0) -x else x

    def sqrtIter(guess: Double, x: Double): Double =
        if (isGoodEnough(guess, x)) guess
        else sqrtIter(improve(guess, x), x)

    def isGoodEnough(guess: Double, x: Double)=
        abs(guess*guess - x  < 0.001)

    def improve(guess: Double, x: Double) =
        (guess + x/guess)/2

    def sqrt (x:Double)= sqrtIter(1.0,x)

}

at line

abs (guess*guess - x <0.001)

eclipse shows the following error

type mismatch; found : Boolean required: Double newton.sc /progfun/src/week1 line 10 Scala Problem

How do I solve this? It's my first time running scala and I'm using the exact code from Functional Programmming class currently going on in Coursera.

1
  • Also not sure why you import math.abs only to define your own abs function later... Commented May 13, 2014 at 9:33

1 Answer 1

3

This line

abs (guess*guess - x <0.001)

returns a boolean, since it first evaluates guess*guess - x, and than compares it to 0.001.

You should do this

abs (guess*guess - x) < 0.001
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.