Thinking outside the box You are trying to fit a sphere into a 5-sided box, but sometimes it does not fit completely. Write a function to calculate how much of the sphere is outside (above the rim of) the box.

There are 3 possible situations:


The sphere fits completely in the box. The answer will be 0.
The sphere sits on the rim of the box. The answer will be more than half of the total volume.
The sphere sits on the bottom of the box.


You can see each situation here:



You must write a program or function to compute this value to at least 4 significant digits.

Input: 4 non-negative real numbers in whatever format is convenient* - width, length, depth of the box (interior measurements), and diameter of the sphere.

Output: 1 non-negative real number in a usable format* - the total volume (not the percentage) of the sphere outside the box.

* must be convertible to/from a decimal string

You are encouraged to limit your use of trigonometry as much as possible.

This is a popularity contest, so think outside the box!
Forth

Please find, below, a sphere outside the box.  

The "sphere" is the volume-computing function f. The reference test cases compose the "box".

                     ( x y z d -- v )
                 : f { F: z F: d } d f2/ 
              { F: r } fmin { F: m } m f2/ {
             F: b } d m f&lt;= d z f&lt;= and if 0e
             else r r r f* b b f* f- fsqrt f-
              { F: t } d m f&lt;= t z f&gt; or if d 
               z f- else d t f- then r 3e f* 
                  fover f- pi f* fover f*
                      f* 3e f/ then ;

                     1e                 1e      
                     1e                 1e 
                     f                  f. 
            cr       1e        1e       0e      
            1e       f         f.       cr 
            1e       1e 0.5e 1e f f. cr 1e 
            0.999e 1e          1e     f  
            f.  cr            0.1e 1e   
            1.000e 0.500e f f. cr


Output:

0. 
0.523598775598299 
0.261799387799149 
0.279345334323962 
0.0654299441440212