- Timestamp:
- 03/21/08 19:00:24 (8 months ago)
- Files:
-
- 1 modified
-
trunk/floating-point/floating-point.lisp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/floating-point/floating-point.lisp
r26 r42 1 ;; Comparison of floating point numbers 1 ;; Comparison of floating point numbers using sequence integers 2 2 ;; Liam Healy 2008-01-22 19:00:17EST floating-point.lisp 3 ;; Time-stamp: <2008-0 1-29 21:34:52EST floating-point.lisp>3 ;; Time-stamp: <2008-03-21 15:04:15EDT floating-point.lisp> 4 4 ;; $Id$ 5 6 ;;; All floats can be represented by integers. There are two slightly 7 ;;; different ways of doing this. The sequence integer 8 ;;; has the same sign as the float; the IEEE754 integer is the 9 ;;; unsigned version of the sequence integer, with the sign marked in 10 ;;; the high order bit. The sequence integer is used for comparing 11 ;;; numbers, where order preservation is important; the IEEE754 integer 12 ;;; is used when reading a float storage location as an integer. 5 13 6 14 (in-package :gsl) … … 47 55 (if (minusp sign) 1 0) expbits exp sigbits mant))) 48 56 49 (defun float-as-integer (float) 50 "An integer corresponding to the float which satisfies three properties: 57 (defun ieee754-sign-bit (float-type) 58 "The bytespec to access the sign bit in the IEEE754 specification." 59 (byte 1 (if (eq float-type 'double-float) 63 31))) 60 61 (defun float-as-integer (float &optional ieee754) 62 "The sequence integer corresponding to the float 63 which satisfies three properties: 51 64 1) For two floats (< a b), then 52 65 (< (float-as-integer a) (float-as-integer b)). … … 56 69 The absolute value of the integer is the integer of the 57 70 IEEE754 representation without the sign bit, and the 58 sign of the integer agrees with the sign of the float." 71 sign of the integer agrees with the sign of the float. 72 To get the IEEE754 integer, specify 'single-float or 73 'double-float to ieee754." 59 74 (multiple-value-bind (mant exp sign sigbits expbits) 60 75 (decode-ieee754 float) 61 76 (declare (ignore expbits)) 62 (* sign (+ (ash exp sigbits) mant)))) 77 (if ieee754 78 (* (dpb 1 (ieee754-sign-bit ieee754) (+ (ash exp sigbits) mant))) 79 (* sign (+ (ash exp sigbits) mant))))) 63 80 64 81 ;; (float-as-integer most-negative-single-float) … … 80 97 81 98 (defun integer-as-float (integer float-type) 82 "Construct the floating point number from its integer representation. 83 Also return the number in rational form." 99 "Construct the floating point number from its integer representation, 100 either sequence or IEEE754. 101 Also return the number in rational form. 102 The integer may be either a signed integer produced by 103 float-as-integer, or an IEEE754 integer. Acceptable 104 float-type is either 'double-float or 'single-float." 84 105 (let ((expbits (if (eq float-type 'double-float) 11 8)) 85 106 (sigbits (if (eq float-type 'double-float) 52 23))) … … 97 118 0))) 98 119 (let ((result 99 (* (signum integer) ; preserve the sign 120 (* (signum integer) ; accept negative integer 121 (if ; accept IEEE754 negative 122 (ldb-test 123 (ieee754-sign-bit float-type) 124 pinteger) 125 -1 1) 100 126 (expt 2 (- bexponent (1- (expt 2 (1- expbits))))) 101 127 frac)))
