Sunday, May 5, 2019

Numerical derivative of analytic functions

I  recently learned about a neat trick to compute the derivative of an analytic function.

Others [1] have described it better than I could. So I just keep this here as a reminder.


[1] https://blogs.mathworks.com/cleve/2013/10/14/complex-step-differentiation/


Exact representation of floating point constants

C99 allows to represent the significand of floating a point value in a hexadecimal notation [2]. This format captures the exact number in a finite number of characters.


I plan to use this with my C code generator [4] to represent unique twiddle factors of the fast Fourier transform [5].

Here is Common Lisp code that will generate the C string to represent a single float:


(defun single-float-to-c-hex-string (f)
  (declare (type (single-float 0) f))
  (multiple-value-bind (a b c) (integer-decode-float f)
  (let ((significand (ash a 1)))
    (format nil "0x~x.~xp~d"
        (ldb (byte 4 (* 6 4)) significand)
        (ldb (byte (* 6 4) 0) significand)
       (+ 23 b)))))


(single-float-to-c-hex-string .1s0) ;; => "0x1.99999Ap-4"



[1] http://clhs.lisp.se/Body/f_dec_fl.htm    decode-float float => significand, exponent, sign
[2] https://www.exploringbinary.com/hexadecimal-floating-point-constants/ examples of c hex notation
[3] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf page 57, parsing
[4] https://github.com/plops/cl-cpp-generator
[5] https://github.com/plops/cl-gen-cuda-try/blob/master/gen-simd.lisp