/usr/share/scheme48-1.9/rts/xnum.scm is in scheme48 1.9-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | ; Part of Scheme 48 1.9. See file COPYING for notices and license.
; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
; This is file xnum.scm.
;;;; Extended number support
(define-simple-type <extended-number> (<number>) extended-number?)
(define-record-type extended-number-type :extended-number-type
(really-make-extended-number-type field-names supers priority predicate id)
extended-number-type?
(field-names extended-number-type-field-names)
(supers extended-number-type-supers)
(priority extended-number-type-priority)
(predicate extended-number-predicate)
(id extended-number-type-identity))
(define-record-discloser :extended-number-type
(lambda (e-n-t)
(list 'extended-number-type (extended-number-type-identity e-n-t))))
(define (make-extended-number-type field-names supers id)
(letrec ((t (really-make-extended-number-type
field-names
supers
(+ (apply max
(map type-priority
(cons <extended-number> supers)))
10)
(lambda (x)
(and (extended-number? x)
(eq? (extended-number-type x) t)))
id)))
t))
(define (extended-number-type x) (extended-number-ref x 0))
; DEFINE-EXTENDED-NUMBER-TYPE macro
(define-syntax define-extended-number-type
(syntax-rules ()
((define-extended-number-type ?type (?super ...)
(?constructor ?arg1 ?arg ...)
?predicate
(?field ?accessor)
...)
(begin (define ?type
(make-extended-number-type '(?field ...)
(list ?super ...)
'?type))
(define ?constructor
(let ((args '(?arg1 ?arg ...)))
(if (equal? args
(extended-number-type-field-names ?type))
(let ((k (+ (length args) 1)))
(lambda (?arg1 ?arg ...)
(let ((n (make-extended-number k #f))
(i 1))
(extended-number-set! n 0 ?type)
(extended-number-set! n 1 ?arg1)
(begin (set! i (+ i 1))
(extended-number-set! n i ?arg))
...
n)))
(assertion-violation 'define-extended-number-type
"ill-formed DEFINE-EXTENDED-NUMBER-TYPE" '?type))))
(define (?predicate x)
(and (extended-number? x)
(eq? (extended-number-type x) ?type)))
(define-extended-number-accessors ?accessor ...)))))
(define-syntax define-extended-number-accessors
(syntax-rules ()
((define-extended-number-accessors ?accessor)
(define (?accessor n) (extended-number-ref n 1)))
((define-extended-number-accessors ?accessor1 ?accessor2)
(begin (define (?accessor1 n) (extended-number-ref n 1))
(define (?accessor2 n) (extended-number-ref n 2))))
((define-extended-number-accessors ?accessor1 ?accessor2 ?accessor3)
(begin (define (?accessor1 n) (extended-number-ref n 1))
(define (?accessor2 n) (extended-number-ref n 2))
(define (?accessor3 n) (extended-number-ref n 3))))))
(define-method &type-priority ((t :extended-number-type))
(extended-number-type-priority t))
(define-method &type-predicate ((t :extended-number-type))
(extended-number-predicate t))
; Make all the numeric instructions be extensible.
(define (make-opcode-generic! opcode mtable)
(let ((perform (method-table-get-perform mtable)))
(extend-opcode! opcode
(lambda (lose)
(set-final-method! mtable
(lambda (next-method . args)
(apply lose (enum exception wrong-type-argument) args)))
(lambda args
((perform) args))))))
(define-syntax define-opcode-extension
(syntax-rules ()
((define-opcode-extension ?name ?table-name)
(begin (define ?table-name (make-method-table '?name))
(make-opcode-generic! (enum op ?name) ?table-name)))))
(define-opcode-extension + &+)
(define-opcode-extension - &-)
(define-opcode-extension * &*)
(define-opcode-extension / &/)
(define-opcode-extension = &=)
(define-opcode-extension < &<)
(define-opcode-extension quotient "ient)
(define-opcode-extension remainder &remainder)
(define-opcode-extension integer? &integer?)
(define-opcode-extension rational? &rational?)
(define-opcode-extension real? &real?)
(define-opcode-extension complex? &complex?)
(define-opcode-extension number? &number?)
(define-opcode-extension exact? &exact?)
(define-opcode-extension exact->inexact &exact->inexact)
(define-opcode-extension inexact->exact &inexact->exact)
(define-opcode-extension real-part &real-part)
(define-opcode-extension imag-part &imag-part)
(define-opcode-extension angle &angle)
(define-opcode-extension magnitude &magnitude)
(define-opcode-extension floor &floor)
(define-opcode-extension numerator &numerator)
(define-opcode-extension denominator &denominator)
(define-opcode-extension make-rectangular &make-rectangular)
(define-opcode-extension make-polar &make-polar)
(define-opcode-extension exp &exp)
(define-opcode-extension log &log)
(define-opcode-extension sin &sin)
(define-opcode-extension cos &cos)
(define-opcode-extension tan &tan)
(define-opcode-extension asin &asin)
(define-opcode-extension acos &acos)
(define-opcode-extension atan1 &atan1)
(define-opcode-extension atan2 &atan2)
(define-opcode-extension sqrt &sqrt)
; >, <=, and >= are all extended using the table for <.
(let ((perform (method-table-get-perform &<)))
(extend-opcode! (enum op >)
(lambda (lose)
(lambda (x y)
((perform) (list y x)))))
(extend-opcode! (enum op <=)
(lambda (lose)
(lambda (x y)
(not ((perform) (list y x))))))
(extend-opcode! (enum op >=)
(lambda (lose)
(lambda (x y)
(not ((perform) (list x y)))))))
; Default methods.
(define-method &integer? (x) #f)
(define-method &rational? (x) (integer? x))
(define-method &real? (x) (rational? x))
(define-method &complex? (x) (real? x))
(define-method &number? (x) (complex? x))
(define-method &real-part ((x <real>)) x)
(define-method &imag-part ((x <real>))
(if (exact? x) 0 (exact->inexact 0)))
(define-method &magnitude ((x <real>))
(abs x))
(define pi (delay (* 2 (asin 1)))) ; can't compute at build time
(define-method &angle ((x <real>))
(cond
((positive? x)
(if (exact? x)
0
(exact->inexact 0)))
((negative? x) (force pi))
((exact? x) (assertion-violation 'angle "invalid argument to angle" x))
(else x)))
(define-method &floor ((n <integer>)) n)
(define-method &numerator ((n <integer>)) n)
(define-method &denominator ((n <integer>))
(if (exact? n) 1 (exact->inexact 1)))
; Make sure this has very low priority, so that it's only tried as a
; last resort.
(define-method &/ (m n)
(if (and (integer? m) (integer? n))
(if (= 0 (remainder m n))
(quotient m n)
(let ((z (abs (quotient n 2))))
(set-exactness (quotient (if (< m 0)
(- m z)
(+ m z))
n)
#f)))
(next-method)))
(define-method &sqrt (n)
(if (and (integer? n)
(>= n 0))
(non-negative-integer-sqrt n)
(next-method)))
(define (non-negative-integer-sqrt n)
(cond ((<= n 1) ; for both 0 and 1
n)
;; ((< n 0)
;; (make-rectangular 0 (integer-sqrt (- 0 n))))
(else
(let loop ((m (quotient n 2)))
(let ((m1 (quotient n m)))
(cond ((< m1 m)
(loop (quotient (+ m m1) 2)))
((= n (* m m))
m)
((exact? m)
(exact->inexact m))
(else m)))))))
(define-simple-type <exact> (<number>)
(lambda (n) (and (number? n) (exact? n))))
(define-method &inexact->exact ((n <exact>)) n)
(define-simple-type <inexact> (<number>)
(lambda (n) (and (number? n) (inexact? n))))
(define-method &exact->inexact ((n <inexact>)) n)
; Whattakludge.
; Replace the default method (which in the initial image always returns #f).
(define-method &really-string->number (s radix xact?)
(let ((len (string-length s)))
(cond ((<= len 1) #f)
((char=? (string-ref s (- len 1)) #\i)
(parse-rectangular s radix xact?))
((string-position #\@ s)
=> (lambda (at)
(let ((r (really-string->number (substring s 0 at)
radix xact?))
(theta (really-string->number (substring s (+ at 1) len)
radix xact?)))
(if (and (real? r) (real? theta))
(make-polar r theta)))))
((string-position #\/ s)
=> (lambda (slash)
(let ((m (string->integer (substring s 0 slash) radix))
(n (string->integer (substring s (+ slash 1) len)
radix)))
(if (and m n (not (zero? n)))
(set-exactness (/ m n) xact?)
#f))))
((string-position #\# s)
(if xact?
#f
(really-string->number
(list->string (map (lambda (c) (if (char=? c #\#) #\5 c))
(string->list s)))
radix
xact?)))
((and (= radix 10)
(string-position #\e s))
=> (lambda (e)
(parse-with-exponent s xact? e)))
((string-position #\. s)
=> (lambda (dot)
(parse-decimal s radix xact? dot)))
(else #f))))
(define (parse-decimal s radix xact? dot)
;; Talk about kludges. This is REALLY kludgey.
(let* ((len (string-length s))
(j (if (or (char=? (string-ref s 0) #\+)
(char=? (string-ref s 0) #\-))
1
0))
(m (if (= dot j)
0
(string->integer (substring s j dot)
radix)))
(n (if (= dot (- len 1))
0
(string->integer (substring s (+ dot 1) len)
radix))))
(if (and m n)
(let ((n (+ m (/ n (expt radix
(- len (+ dot 1)))))))
(set-exactness (if (char=? (string-ref s 0) #\-)
(- 0 n)
n)
xact?))
#f)))
(define (parse-with-exponent s xact? e)
(let ((len (string-length s)))
(cond
((string->integer (substring s (+ e 1) len) 10)
=> (lambda (exp)
(cond
((really-string->number (substring s 0 e) 10 xact?)
=> (lambda (significand)
(* significand
(expt 10 exp))))
(else #f))))
(else #f))))
(define (parse-rectangular s radix xact?)
(let ((len (string-length s)))
(let loop ((i (- len 2)))
(if (< i 0)
#f
(let ((c (string-ref s i)))
(if (or (char=? c #\+)
(char=? c #\-))
(let ((x (if (= i 0)
0
(really-string->number (substring s 0 i)
radix xact?)))
(y (if (= i (- len 2))
(if (char=? c #\+) 1 -1)
(really-string->number (substring s i (- len 1))
radix xact?))))
(if (and (real? x) (real? y))
(make-rectangular x y)
#f))
(loop (- i 1))))))))
(define (set-exactness n xact?)
(if (exact? n)
(if xact? n (exact->inexact n))
;; ?what to do? (if xact? (inexact->exact n) n)
n))
; Utility
(define (string-position c s)
(let loop ((i 0))
(if (>= i (string-length s))
#f
(if (char=? c (string-ref s i))
i
(loop (+ i 1))))))
|