aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuanma Barranquero2002-09-06 07:13:53 +0000
committerJuanma Barranquero2002-09-06 07:13:53 +0000
commit05619b997817aa6042705b44ec4b577e97514174 (patch)
tree0d2ed4660bf3997900b3ca01cbba730c3755be38
parent2251e3edaf3cbb168b987d3c4541b185fc650955 (diff)
downloademacs-05619b997817aa6042705b44ec4b577e97514174.tar.gz
emacs-05619b997817aa6042705b44ec4b577e97514174.zip
(string-to-float): Fix spacing.
-rw-r--r--lisp/emacs-lisp/float.el42
1 files changed, 21 insertions, 21 deletions
diff --git a/lisp/emacs-lisp/float.el b/lisp/emacs-lisp/float.el
index 53d31c6e33a..e5d71abb69b 100644
--- a/lisp/emacs-lisp/float.el
+++ b/lisp/emacs-lisp/float.el
@@ -39,14 +39,14 @@
39;; function string-to-float converts from string to floating point 39;; function string-to-float converts from string to floating point
40;; function fint converts a floating point to integer (with truncation) 40;; function fint converts a floating point to integer (with truncation)
41;; function float-to-string converts from floating point to string 41;; function float-to-string converts from floating point to string
42;; 42;;
43;; Caveats: 43;; Caveats:
44;; - Exponents outside of the range of +/-100 or so will cause certain 44;; - Exponents outside of the range of +/-100 or so will cause certain
45;; functions (especially conversion routines) to take forever. 45;; functions (especially conversion routines) to take forever.
46;; - Very little checking is done for fixed point overflow/underflow. 46;; - Very little checking is done for fixed point overflow/underflow.
47;; - No checking is done for over/underflow of the exponent 47;; - No checking is done for over/underflow of the exponent
48;; (hardly necessary when exponent can be 2**23). 48;; (hardly necessary when exponent can be 2**23).
49;; 49;;
50;; 50;;
51;; Bill Rosenblatt 51;; Bill Rosenblatt
52;; June 20, 1986 52;; June 20, 1986
@@ -131,7 +131,7 @@
131 (setq fnum (fashl fnum))) 131 (setq fnum (fashl fnum)))
132 (setq fnum _f0))) ; "standard 0" 132 (setq fnum _f0))) ; "standard 0"
133 fnum) 133 fnum)
134 134
135(defun abs (n) ; integer absolute value 135(defun abs (n) ; integer absolute value
136 (if (>= n 0) n (- n))) 136 (if (>= n 0) n (- n)))
137 137
@@ -204,7 +204,7 @@
204 (if (zerop (car a2)) ; if divide by 0 204 (if (zerop (car a2)) ; if divide by 0
205 (signal 'arith-error (list "attempt to divide by zero" a1 a2)) 205 (signal 'arith-error (list "attempt to divide by zero" a1 a2))
206 (let ((bits (1- maxbit)) 206 (let ((bits (1- maxbit))
207 (quotient 0) 207 (quotient 0)
208 (dividend (car (fabs a1))) 208 (dividend (car (fabs a1)))
209 (divisor (car (fabs a2))) 209 (divisor (car (fabs a2)))
210 (sign (not (same-sign a1 a2)))) 210 (sign (not (same-sign a1 a2))))
@@ -218,11 +218,11 @@
218 (normalize 218 (normalize
219 (cons (if sign (- quotient) quotient) 219 (cons (if sign (- quotient) quotient)
220 (- (cdr (fabs a1)) (cdr (fabs a2)) (1- maxbit))))))) 220 (- (cdr (fabs a1)) (cdr (fabs a2)) (1- maxbit)))))))
221 221
222(defun f% (a1 a2) 222(defun f% (a1 a2)
223 "Returns the remainder of first floating point number divided by second." 223 "Returns the remainder of first floating point number divided by second."
224 (f- a1 (f* (ftrunc (f/ a1 a2)) a2))) 224 (f- a1 (f* (ftrunc (f/ a1 a2)) a2)))
225 225
226 226
227;; Comparison functions 227;; Comparison functions
228(defun f= (a1 a2) 228(defun f= (a1 a2)
@@ -232,7 +232,7 @@
232(defun f> (a1 a2) 232(defun f> (a1 a2)
233 "Returns t if first floating point number is greater than second, 233 "Returns t if first floating point number is greater than second,
234nil otherwise." 234nil otherwise."
235 (cond ((and (natnump (car a1)) (< (car a2) 0)) 235 (cond ((and (natnump (car a1)) (< (car a2) 0))
236 t) ; a1 nonnegative, a2 negative 236 t) ; a1 nonnegative, a2 negative
237 ((and (> (car a1) 0) (<= (car a2) 0)) 237 ((and (> (car a1) 0) (<= (car a2) 0))
238 t) ; a1 positive, a2 nonpositive 238 t) ; a1 positive, a2 nonpositive
@@ -244,7 +244,7 @@ nil otherwise."
244 (> (car a1) (car a2))))) ; same exponents. 244 (> (car a1) (car a2))))) ; same exponents.
245 245
246(defun f>= (a1 a2) 246(defun f>= (a1 a2)
247 "Returns t if first floating point number is greater than or equal to 247 "Returns t if first floating point number is greater than or equal to
248second, nil otherwise." 248second, nil otherwise."
249 (or (f> a1 a2) (f= a1 a2))) 249 (or (f> a1 a2) (f= a1 a2)))
250 250
@@ -270,7 +270,7 @@ nil otherwise."
270(defun fmax (a1 a2) 270(defun fmax (a1 a2)
271 "Returns the maximum of two floating point numbers." 271 "Returns the maximum of two floating point numbers."
272 (if (f> a1 a2) a1 a2)) 272 (if (f> a1 a2) a1 a2))
273 273
274(defun fzerop (fnum) 274(defun fzerop (fnum)
275 "Returns t if the floating point number is zero, nil otherwise." 275 "Returns t if the floating point number is zero, nil otherwise."
276 (= (car fnum) 0)) 276 (= (car fnum) 0))
@@ -290,7 +290,7 @@ nil otherwise."
290 (str "0x") 290 (str "0x")
291 (hex-chars "0123456789ABCDEF")) 291 (hex-chars "0123456789ABCDEF"))
292 (while (<= shiftval 0) 292 (while (<= shiftval 0)
293 (setq str (concat str (char-to-string 293 (setq str (concat str (char-to-string
294 (aref hex-chars 294 (aref hex-chars
295 (logand (lsh int shiftval) 15)))) 295 (logand (lsh int shiftval) 15))))
296 shiftval (+ shiftval 4))) 296 shiftval (+ shiftval 4)))
@@ -304,14 +304,14 @@ nil otherwise."
304 '(0 . 1)) 304 '(0 . 1))
305 (t ; otherwise mask out fractional bits 305 (t ; otherwise mask out fractional bits
306 (let ((mant (car fnum)) (exp (cdr fnum))) 306 (let ((mant (car fnum)) (exp (cdr fnum)))
307 (normalize 307 (normalize
308 (cons (if (natnump mant) ; if negative, use absolute value 308 (cons (if (natnump mant) ; if negative, use absolute value
309 (ash (ash mant exp) (- exp)) 309 (ash (ash mant exp) (- exp))
310 (- (ash (ash (- mant) exp) (- exp)))) 310 (- (ash (ash (- mant) exp) (- exp))))
311 exp)))))) 311 exp))))))
312 312
313(defun fint (fnum) ; truncate and convert to integer 313(defun fint (fnum) ; truncate and convert to integer
314 "Convert the floating point number to integer, with truncation, 314 "Convert the floating point number to integer, with truncation,
315like a C cast operator." 315like a C cast operator."
316 (let* ((tf (ftrunc fnum)) (tint (car tf)) (texp (cdr tf))) 316 (let* ((tf (ftrunc fnum)) (tint (car tf)) (texp (cdr tf)))
317 (cond ((>= texp mantissa-bits) ; too high, return "maxint" 317 (cond ((>= texp mantissa-bits) ; too high, return "maxint"
@@ -325,7 +325,7 @@ like a C cast operator."
325 "Convert the floating point number to a decimal string. 325 "Convert the floating point number to a decimal string.
326Optional second argument non-nil means use scientific notation." 326Optional second argument non-nil means use scientific notation."
327 (let* ((value (fabs fnum)) (sign (< (car fnum) 0)) 327 (let* ((value (fabs fnum)) (sign (< (car fnum) 0))
328 (power 0) (result 0) (str "") 328 (power 0) (result 0) (str "")
329 (temp 0) (pow10 _f1)) 329 (temp 0) (pow10 _f1))
330 330
331 (if (f= fnum _f0) 331 (if (f= fnum _f0)
@@ -386,13 +386,13 @@ Optional second argument non-nil means use scientific notation."
386 (concat "-" str) 386 (concat "-" str)
387 str)))) 387 str))))
388 388
389 389
390;; string to float conversion. 390;; string to float conversion.
391;; accepts scientific notation, but ignores anything after the first two 391;; accepts scientific notation, but ignores anything after the first two
392;; digits of the exponent. 392;; digits of the exponent.
393(defun string-to-float (str) 393(defun string-to-float (str)
394 "Convert the string to a floating point number. 394 "Convert the string to a floating point number.
395Accepts a decimal string in scientific notation, with exponent preceded 395Accepts a decimal string in scientific notation, with exponent preceded
396by either E or e. Only the six most significant digits of the integer 396by either E or e. Only the six most significant digits of the integer
397and fractional parts are used; only the first two digits of the exponent 397and fractional parts are used; only the first two digits of the exponent
398are used. Negative signs preceding both the decimal number and the exponent 398are used. Negative signs preceding both the decimal number and the exponent
@@ -415,7 +415,7 @@ are recognized."
415 (setq leading-0s (1+ leading-0s))) 415 (setq leading-0s (1+ leading-0s)))
416 (setq power (- power leading-0s) 416 (setq power (- power leading-0s)
417 digit-string (substring digit-string leading-0s)) 417 digit-string (substring digit-string leading-0s))
418 418
419 ; if more than 6 digits, round off 419 ; if more than 6 digits, round off
420 (if (> (length digit-string) decimal-digits) 420 (if (> (length digit-string) decimal-digits)
421 (setq round-up (>= (aref digit-string decimal-digits) ?5) 421 (setq round-up (>= (aref digit-string decimal-digits) ?5)
@@ -426,13 +426,13 @@ are recognized."
426 (f (* (+ (string-to-int digit-string) 426 (f (* (+ (string-to-int digit-string)
427 (if round-up 1 0)) 427 (if round-up 1 0))
428 (if mant-sign -1 1)))) 428 (if mant-sign -1 1))))
429 429
430 ; calculate the exponent (power of ten) 430 ; calculate the exponent (power of ten)
431 (let* ((expt-subst (extract-match str 9)) 431 (let* ((expt-subst (extract-match str 9))
432 (expt-sign (equal (extract-match str 8) "-")) 432 (expt-sign (equal (extract-match str 8) "-"))
433 (expt 0) (chunks 0) (tens 0) (exponent _f1) 433 (expt 0) (chunks 0) (tens 0) (exponent _f1)
434 (func 'f*)) 434 (func 'f*))
435 435
436 (setq expt (+ (* (string-to-int 436 (setq expt (+ (* (string-to-int
437 (substring expt-subst 0 437 (substring expt-subst 0
438 (min expt-digits (length expt-subst)))) 438 (min expt-digits (length expt-subst))))
@@ -445,12 +445,12 @@ are recognized."
445 (setq chunks (/ expt decimal-digits) 445 (setq chunks (/ expt decimal-digits)
446 tens (% expt decimal-digits)) 446 tens (% expt decimal-digits))
447 ; divide or multiply by "chunks" of 10**6 447 ; divide or multiply by "chunks" of 10**6
448 (while (> chunks 0) 448 (while (> chunks 0)
449 (setq exponent (funcall func exponent highest-power-of-10) 449 (setq exponent (funcall func exponent highest-power-of-10)
450 chunks (1- chunks))) 450 chunks (1- chunks)))
451 ; divide or multiply by remaining power of ten 451 ; divide or multiply by remaining power of ten
452 (funcall func exponent (aref powers-of-10 tens))))) 452 (funcall func exponent (aref powers-of-10 tens)))))
453 453
454 _f0)) ; if invalid, return 0 454 _f0)) ; if invalid, return 0
455 455
456(provide 'float) 456(provide 'float)