| ... |
... |
@@ -2457,135 +2457,6 @@ radix-R. If you have a power-list then pass it in as PL." |
|
2457
|
2457
|
(declare (dynamic-extent #'writ-field))
|
|
2458
|
2458
|
(pad-overflow stream field-len w overflowchar padchar #'write-field))))
|
|
2459
|
2459
|
|
|
2460
|
|
-
|
|
2461
|
|
-#+nil
|
|
2462
|
|
-(defun reshape-fixed (raw-digits exponent)
|
|
2463
|
|
- ;; RAW-DIGITS is a string of digits with no sign or decimal point.
|
|
2464
|
|
- (declare (simple-string raw-digits)
|
|
2465
|
|
- (fixnu exponent))
|
|
2466
|
|
- (let* ((digit-count (length raw-digits))
|
|
2467
|
|
- (left-count (1+ exponent)))
|
|
2468
|
|
- (cond ((>= left-count digit-count)
|
|
2469
|
|
- ;; All digits go in the integer part; pad with trailing
|
|
2470
|
|
- ;; zeros
|
|
2471
|
|
- (concatenate 'string
|
|
2472
|
|
- raw-digits
|
|
2473
|
|
- (make-string (- left-count digit-count)
|
|
2474
|
|
- :initial-element #\0)
|
|
2475
|
|
- ".0"))
|
|
2476
|
|
- ((plusp left-count)
|
|
2477
|
|
- (concatenate 'string
|
|
2478
|
|
- (subseq raw-digits 0 left-count)
|
|
2479
|
|
- "."
|
|
2480
|
|
- (subseq raw-digits left-count)))
|
|
2481
|
|
- (t
|
|
2482
|
|
- ;; All digits go in the fractional part; prepend "0." and
|
|
2483
|
|
- ;; any needed leading zeroes.
|
|
2484
|
|
- (concatenate 'string
|
|
2485
|
|
- "0."
|
|
2486
|
|
- (make-string (- left-count) :initial-element #\0)
|
|
2487
|
|
- raw-digits)))))
|
|
2488
|
|
-
|
|
2489
|
|
-#+nil
|
|
2490
|
|
-(defun reshape-fixed (raw-digits exponent)
|
|
2491
|
|
- ;; RAW-DIGITS is a string of digits with no sign or decimal point.
|
|
2492
|
|
- (declare (simple-string raw-digits)
|
|
2493
|
|
- (fixnum exponent))
|
|
2494
|
|
- (let* ((digit-count (length raw-digits))
|
|
2495
|
|
- (left-count (1+ exponent)))
|
|
2496
|
|
- (with-output-to-string (s)
|
|
2497
|
|
- (cond ((>= left-count digit-count)
|
|
2498
|
|
- ;; All digits go in the integer part; pad with trailing
|
|
2499
|
|
- ;; zeros
|
|
2500
|
|
- (write-string raw-digits s)
|
|
2501
|
|
- (loop for k from 0 below (- left-count digit-count)
|
|
2502
|
|
- do (write-char #\0 s))
|
|
2503
|
|
- (write-string ".0" s))
|
|
2504
|
|
- ((plusp left-count)
|
|
2505
|
|
- (write-string raw-digits s :start 0 :end left-count)
|
|
2506
|
|
- (write-char #\. s)
|
|
2507
|
|
- (write-string raw-digits s :start left-count))
|
|
2508
|
|
- (t
|
|
2509
|
|
- ;; All digits go in the fractional part; prepend "0." and
|
|
2510
|
|
- ;; any needed leading zeroes.
|
|
2511
|
|
- (write-string "0." s)
|
|
2512
|
|
- (loop for k from 0 below (- left-count)
|
|
2513
|
|
- do (write-char #\0 s))
|
|
2514
|
|
- (write-string raw-digits s))))))
|
|
2515
|
|
-
|
|
2516
|
|
-#+nil
|
|
2517
|
|
-(defun reshape-fixed (mantissa-text exponent)
|
|
2518
|
|
- (declare (type simple-string mantissa-text)
|
|
2519
|
|
- (fixnum exponent))
|
|
2520
|
|
- (reshape-format-e mantissa-text (1+ exponent) nil))
|
|
2521
|
|
-
|
|
2522
|
|
-#+nil
|
|
2523
|
|
-(defun reshape-fixed (stream mantissa-text exponent)
|
|
2524
|
|
- "Adjust MANTISSA-TEXT is scaled exactly by 10^{EXPONENT} by moving the
|
|
2525
|
|
- decimal point. MANTISSA-TEXT is of the form 'd[.dddd]' from d2exp
|
|
2526
|
|
- or d2s. The scaled value is written to STREAM."
|
|
2527
|
|
- (declare (type simple-string mantissa-text)
|
|
2528
|
|
- (fixnum exponent))
|
|
2529
|
|
- (let ((k (1+ exponent)))
|
|
2530
|
|
- ;; If exponent = 0, the mantissa is already the right value, but
|
|
2531
|
|
- ;; we need to append ".0" if there's not dot in the mantissa.
|
|
2532
|
|
- ;; This happens when d2s is given a small exact integral value.
|
|
2533
|
|
- (when (zerop exponent)
|
|
2534
|
|
- (write-string mantissa-text stream)
|
|
2535
|
|
- (unless (find #\. mantissa-text)
|
|
2536
|
|
- (write-string ".0" stream))
|
|
2537
|
|
- (return-from reshape-fixed))
|
|
2538
|
|
- (let* ((has-dot (find #\. mantissa-text))
|
|
2539
|
|
- (lead-char (char mantissa-text 0))
|
|
2540
|
|
- ;; Where the stuff after the dot starts
|
|
2541
|
|
- (tail-start (if has-dot 2 1))
|
|
2542
|
|
- (tail-len (- (length mantissa-text) tail-start))
|
|
2543
|
|
- (digit-count (1+ tail-len)))
|
|
2544
|
|
- ;; Several cases to consider:
|
|
2545
|
|
- ;;
|
|
2546
|
|
- ;; * k is negative so the new dot preceeds the mantissa. Pad with
|
|
2547
|
|
- ;; zeroes.
|
|
2548
|
|
- ;; * k is so large that the dot is past the rightmost part of the
|
|
2549
|
|
- ;; mantissa. Append enough zeroes and then add ".0"
|
|
2550
|
|
- ;; * k is somewhere in the mantissa. Handling of this case
|
|
2551
|
|
- ;; depends on if the new dot is to the left or to the right of
|
|
2552
|
|
- ;; the original dot.
|
|
2553
|
|
- (cond
|
|
2554
|
|
- ;; Case 2: new dot at or before position 0.
|
|
2555
|
|
- ((<= k 0)
|
|
2556
|
|
- (write-string "0." stream)
|
|
2557
|
|
- ;; Insert zeros after the decimal but before the mantissa.
|
|
2558
|
|
- (loop repeat (- k)
|
|
2559
|
|
- do (write-char #\0 stream))
|
|
2560
|
|
- ;; Insert the mantissa, skipping the existing dot.
|
|
2561
|
|
- (write-char lead-char stream)
|
|
2562
|
|
- (when (plusp tail-len)
|
|
2563
|
|
- (write-string mantissa-text stream :start tail-start)))
|
|
2564
|
|
- ;; Case 1b: 1 < k < digit-count. Move dot right by (k-1).
|
|
2565
|
|
- ((< k digit-count)
|
|
2566
|
|
- ;; Output the original mantissa up to the original dot.
|
|
2567
|
|
- (write-char lead-char stream)
|
|
2568
|
|
- (write-string mantissa-text stream
|
|
2569
|
|
- :start tail-start
|
|
2570
|
|
- :end (+ tail-start k -1))
|
|
2571
|
|
- ;; Output the new dot.
|
|
2572
|
|
- (write-char #\. stream)
|
|
2573
|
|
- ;; Output everything after the original dot.
|
|
2574
|
|
- (write-string mantissa-text stream
|
|
2575
|
|
- :start (+ tail-start k -1)))
|
|
2576
|
|
- ;; Case 3: k >= digit-count. Pad with zeros, force ".0".
|
|
2577
|
|
- (t
|
|
2578
|
|
- ;; Output the original mantissa, then everything after the
|
|
2579
|
|
- ;; dot (if there was one).
|
|
2580
|
|
- (write-char lead-char stream)
|
|
2581
|
|
- (when (plusp tail-len)
|
|
2582
|
|
- (write-string mantissa-text stream :start tail-start))
|
|
2583
|
|
- ;; Pad with 0's
|
|
2584
|
|
- (loop repeat (- k digit-count)
|
|
2585
|
|
- do (write-char #\0 stream))
|
|
2586
|
|
- ;; Finish with ".0".
|
|
2587
|
|
- (write-string ".0" stream))))))
|
|
2588
|
|
-
|
|
2589
|
2460
|
(defun reshape-fixed (stream mantissa-text exponent)
|
|
2590
|
2461
|
"Adjust MANTISSA-TEXT is scaled exactly by 10^{EXPONENT} by moving the
|
|
2591
|
2462
|
decimal point. MANTISSA-TEXT is of the form 'd[.dddd]' from d2exp
|
| ... |
... |
@@ -2593,15 +2464,6 @@ radix-R. If you have a power-list then pass it in as PL." |
|
2593
|
2464
|
(scale-mantissa stream mantissa-text (1+ exponent) nil))
|
|
2594
|
2465
|
|
|
2595
|
2466
|
|
|
2596
|
|
-#+nil
|
|
2597
|
|
-(defun emit-shortest (mantissa-text exponent is-negative-p w overflowchar padchar at-sign-p)
|
|
2598
|
|
- (let* ((reshaped (reshape-fixed mantissa-text exponent))
|
|
2599
|
|
- (sign-text (cond (is-negative-p "-")
|
|
2600
|
|
- (at-sign-p "+")
|
|
2601
|
|
- (t "")))
|
|
2602
|
|
- (field (concatenate 'string sign-text reshaped)))
|
|
2603
|
|
- (format-f-pad-overflow field w overflowchar padchar)))
|
|
2604
|
|
-
|
|
2605
|
2467
|
(defun reshape-fixed-length (digit-count exponent)
|
|
2606
|
2468
|
(let ((k (1+ exponent)))
|
|
2607
|
2469
|
(cond ((zerop exponent)
|