Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; The original problem:
- ; I want to find out the fn that satisfies this test for any x > 0 and y > 0, supposing x are generally (but not guaranteed) aprox. y.
- (define ok (lambda (fn x y)
- (let ((result1 (fn x y))
- (result2 (/ 1 (fn (/ 1 x)
- (/ 1 y)))))
- (and (= result1 result2)
- (< (min x y) result1)
- (> (max x y) result1)))))
- ; So I created this question: http://math.stackexchange.com/questions/985506/how-to-find-the-function-f-that-satisfies-fx-y-fx-1-y-1-1-a
- ; Then I defined:
- (define fn-solution (lambda (x y)
- (sqrt (* x y))))
- ; But:
- (ok fn-solution 5 8) => #f
- ; for example. Why did not work? Because sqrt is not precise, it is a irrational number.
- ; So, if x and y are exact numbers, then this solution works:
- (define fn-final-solution
- (lambda (x y)
- (let ((use-inverse (< (min x y)
- (min (/ 1 x) (/ 1 y)))))
- (if (not use-inverse)
- (inexact->exact (sqrt (* x y)))
- (/ 1 (inexact->exact (sqrt (* (/ 1 x)
- (/ 1 y)))))))))
- ; The final test:
- (define nice-random
- (lambda ()
- (inexact->exact (/ (random) (random)))))
- (let next ((x (nice-random))
- (y (nice-random))
- (counter 0))
- (or (> counter 100000)
- (let ((result (ok fn-final-solution x y)))
- (and result
- (next (nice-random)
- (nice-random)
- (+ counter 1))))))
- ; Nice random is just to pick a random number sometimes less than 0 and sometimes bigger. Also it is exact number.
- This test returns #t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement