@ Pohon BBS
SICP : Let's Read! (22 replies)

■ 🕑 19. Excercise 1.7
│  The full code is VERY long and has multiple things needing to be defined so ill only post what was changed from the procedure defined in SICP. My thought process was:
│  need to compare guesses, so need to know guess-1 and guess-2
│  modify the improve definition to "save" the old guess? (oh no...)
│  OR
│  just use (improve guess x) itself as the guess 2
│  
│  then
│  ;modified from 1.1.7 for Exercise 1.7
│  (define (good-enough? guess x)
│  ;(< (abs (- (square guess) x)) 0.001));old/original code [from SICP]
│  (< ;check if guess is within tolerance
│   (abs ; stops it from breaking if it gives a negative number
│   (- ;small difference between subsequent guesses => fraction ~1, so fraction-1 ~0
│   (/
│   (abs (square guess))
│   (abs (square (improve guess x)))) 1)) 0.001))
│  ...
│  (sqrt 0.00000000000001) ;very small number test
│  (sqrt 1000000000000000) ;very large number test
│  
│  answers were correct
│   
├─■ 🕑 20. 
│   where 0.001 was the tolerance to see if the guess was good enough
│    
└─■ 🕑 21. PHATCATTTT
    the tolerance I specified was kinda wide, which made the big number calculation off by a good amount (oops!). After reducing the tolerance to 0.000000000001 (rest of the code kept the same) i get the same result as my calculator (Casio fx-991EX). Dr racket seems to have rounded the small square roots output to 1e-7, im not sure if this is a fault in my code or just computer stuff (can anyone confirm?)
    
     also i noticed that if im just comparing the guesses,i dont need to square them and then divide. I also had redundant (abs ) functions in the code so heres an improvement
    
    (define (good-enough? guess x)
    (<
     (abs
     (-
     (/
     guess
     (improve guess x)) 1)) 0.000000000001))
    
    (sqrt 0.00000000000001) ;very small number test
    (sqrt 1000000000000000) ;very large number test
    
    outputs:
    1e-7
    31622776.601683907
     

Pohon BBS