Test failure in Pizza Pi

What am I supposed to do about this?

FAILED
Test 9
Calculates the diameter of a pizza from the amount of sauce applied (4/5)

CODE RUN
(is (= 32.573500793528d0 (size-from-sauce 250)))
(is (= 20.601290774570113d0 (size-from-sauce 100)))
(is (= 37.424103185095554d0 (size-from-sauce 330)))
(is (= 46.52426491681278d0 (size-from-sauce 510)))
(is (= 53.72159374714264d0 (size-from-sauce 680)))
TEST FAILURE

(SIZE-FROM-SAUCE 100)

 evaluated to 

20.60129077457011d0

 which is not 

=

 to 

20.601290774570113d0

Iā€™m working in the web editor.

Not sure right now, I submitted my solution again to check if itā€™s still passing and it passed.
Can you please share your code?


This is off topic but since you wrote in another thread that you had a difficult time getting into lispā€¦ I would have a hard time getting into Lisp as well if I would try it without good editor support and a REPL. And I already had a hard time getting into Lisp when I started out. :smiley:
Feel free to hit me up on slack if you need help with setting up an environment.

Since youā€™re an experienced Exercism user I assume you know but maybe someone else comes across this post and doesnā€™t know: You can download the exercise and submit a failing solution through the CLI. That way you can ask for mentorship.

Hereā€™s my code. Perhaps the tests donā€™t need to compare to the 15th decimal place thoughā€¦

(defpackage :pizza-pi
  (:use :cl)
  (:export :dough-calculator :pizzas-per-cube
           :size-from-sauce :fair-share-p))

(in-package :pizza-pi)

(defvar base-dough 200) ; amount of dough in grams for the "base" (i.e. non-crust) of the pizza
(defvar dough-per-length-of-crust 45) ; amount of dough in grams per length of crust
(defvar len 20)   ; size in cm of a "length of crust"

(defun perimeter (diameter) (* pi diameter))

(defun lengths-of-crust (diameter)
  (/ (perimeter diameter) len)
)
(defun crust-dough (diameter)
  (* (lengths-of-crust diameter) dough-per-length-of-crust)
)

(defun dough-calculator (pizzas diameter)
  (round (* pizzas (+ (crust-dough diameter) base-dough)))
)

(defvar sauce-unit 3/10) ; amount of sauce in mL per cm^2 of pizza

;; given an area of a circle, return the diameter
(defun diameter-from-area (area)
  ; a = pi r^2 => r = sqrt(a / pi) => d = 2 * sqrt(a/pi)
  (* 2 (sqrt (/ area pi)))
)

(defun size-from-sauce (sauce)
  (diameter-from-area (/ sauce sauce-unit))
)

;; n = (2 * (l^3))/(3 * pi * (d^2))
(defun pizzas-per-cube (cube-size diameter)
  (print (list 'numerator (* 2 cube-size cube-size cube-size)))
  (print (list 'denominator (* 3 * pi * diameter * diameter)))
  (/ (* 2 cube-size cube-size cube-size)
     (* 3 pi diameter diameter)
  )
)

(defun fair-share-p (pizzas friends))

Yeah, youā€™re running into a type conversion issue. My theory was that itā€™s a problem with defining the sauce-unit as a fraction but converting it to a float didnā€™t help.

+1 for not matching floating point numbers to the 15th decimal. :smiley: I learned that you should always only match up to a certain precision when testing floating-point numbers.

If you transfer the formula like itā€™s written in the problem description, without transforming it, your test should pass: square-root of ((40 * sauce-applied) / (3 * pi))

Since community contributions are currently paused Iā€™ll leave my suggested fix here in the forum:

(defvar epsilon 0.00000001)

(defun compare-floats (float1 float2)
  (< (abs (- float1 float2)) epsilon))

(test splash-of-sauces "Calculates the diameter of a pizza from the amount of sauce applied"
  (is (compare-floats 32.573500793528d0 (size-from-sauce 250)
       ))
  (is (compare-floats 20.601290774570113d0 (size-from-sauce 100)))
  (is (compare-floats 37.424103185095554d0 (size-from-sauce 330)))
  (is (compare-floats 46.52426491681278d0 (size-from-sauce 510)))
  (is (compare-floats 53.72159374714264d0 (size-from-sauce 680))))
1 Like

Looks like a problem with floating point equality. Thought I had those solved in the exercises but it seems that I have not.

Sorry about this.

1 Like

Looking at another exercise that leans heavily on floats: space-age I see it was solved with:

(defun rounds-to (expected actual)
  (flet ((to-2-places (n) (/ (round (* 100 n)) 100.0)))
    (is (= (to-2-places expected) (to-2-places actual)))))

So that is how Iā€™d likely fix it for pizza-pier (and should have been fixed with that in the first place :( )

2 Likes

PR opened and ready for review:

@glennj / @fap - would love either/both of your feedback on my proposed solution.

@glennj the fix to the exercise was merged so hopefully you should be seeing it on the web site. Has it fixed the problem? Can we mark this topic ā€˜solvedā€™?