
1: @node Core library, Object system, Macros, Top 2: @chapter Core library 3: @c NODE ??ß¹??ߥ饤?Ö¥? 4: 5: @c ---------------------------------------------------------------------- 6: @menu 7: * Equivalence:: 8: * Numbers:: 9: * Booleans:: 10: * Pairs and Lists:: 11: * Symbols:: 12: * Keywords:: 13: * Identifiers:: 14: * Characters:: 15: * Character set:: 16: * Strings:: 17: * Regular expression:: 18: * Vectors:: 19: * Hashtables:: 20: * Treemaps:: 21: * Weak pointers:: 22: * Control features:: 23: * Exceptions:: 24: * Eval and repl:: 25: * Input and output:: 26: * Loading Programs:: 27: * Comparison and sorting:: 28: * System interface:: 29: * Development helper API:: 30: @end menu 31: 32: @node Equivalence, Numbers, Core library, Core library 33: @section Equivalence 34: @c NODE ??? 35: @c EN 36: Scheme has three different general equivalence test predicates. 37: Other than these, some types have their own comparison predicates. 38: @c JP 39: Scheme?ˤ????ꤹ???Ū?ʽҸ줬3?Ĥ??????? 40: ?Þ¤???????????Ë¡??????Ĥ??η??Ϥ??η?Ʊ?ΤǻȤ????Ó¼?????äƤ??Þ¤??? 41: @c COMMON 42: 43: @defun eq? obj1 obj2 44: [R5RS] 45: @c EN 46: This is the fastest and finest predicate. 47: Returns @code{#t} if @var{obj1} and @var{obj2} are allocated objects of 48: the same types, and denote the same location. Returns @code{#t} 49: if both objects are @code{#f}, @code{#t}, or @code{()}. 50: You can think @var{eq?} as a pointer comparison. 51: Note that the result is unspecified in Scheme standard when 52: both objects are characters or numbers. 53: @c JP 54: ?Ǥ?®?Ç¡??Ù¤????????Ǥ????????? 55: @var{obj1}??var{obj2}?????í¥±???Ȥ???Ʊ?????Î¥??Ö¥??????Ȥǡ? 56: ??????Ô¤??????????Ʊ??????????Ö¥??????Ȥ???Ƥ????? 57: @code{#t}????Þ¤????Þ¤???@var{obj1}??var{obj2}???Ȥ? 58: @code{#f}, @code{#t}???뤤??code{()}?Ǥ?????@code{#t}????Þ¤??? 59: ?Ý¥?????ȹͤ??Ƥ????Ǥ??礦?? 60: @var{obj1}??var{obj2}???Ȥ?ʸ?ú¡¢¤¢?뤤?Ï¿?Ǥ??ä????ο???????Scheme???????????Ƥ??Þ¤???@c COMMON 61: 62: @example 63: (eq? #t #t) @result{} #t 64: (eq? #t #f) @result{} #f 65: (eq? 'a 'a) @result{} #t 66: (eq? 'a 'b) @result{} #f 67: (eq? (list 'a) (list 'a)) @result{} #f 68: (let ((x (list 'a))) 69: (eq? x x)) @result{} #t 70: @end example 71: @end defun 72: 73: @defun eqv? obj1 obj2 74: [R5RS] 75: @c EN 76: When @var{obj1} and @var{obj2} are both exact or both inexact numbers, 77: @var{eqv?} returns @code{#t} iff @code{(= @var{obj1} @var{obj2})} is true. 78: When @var{obj1} and @var{obj2} are both characters, 79: @var{eqv?} returns @code{#t} iff @code{(char=? @var{obj1} @var{obj2})} is true. 80: Otherwise, @code{eqv?} is the same as @code{eq?} on Gauche. 81: @c JP 82: @var{obj1}??var{obj2}???Ȥ???Τʿ???⤷???ϤȤ????Τʿ?Ǥ????ç¡¢ 83: @code{(= @var{obj1} @var{obj2})}?????Ǥ???@code{#t}???????Ǥ???@code{#f}?? 84: ????????? 85: @var{obj1}??var{obj2}???Ȥ?ʸ?????ç¡¢ 86: @code{(char=? @var{obj1} @var{obj2})}?????Ǥ???@code{#t}???????Ǥ???@code{#f}?? 87: ????????? 88: ???????ξ??Ï¡?Gauche?Ǥ?code{eqv?}??code{eq?}??????Ǥ??? 89: @c COMMON 90: 91: @example 92: (eqv? #\a #\a) @result{} #t 93: (eqv? #\a #\b) @result{} #f 94: (eqv? 1.0 1.0) @result{} #t 95: (eqv? 1 1) @result{} #t 96: (eqv? 1 1.0) @result{} #f 97: (eqv? (list 'a) (list 'a)) @result{} #f 98: (let ((x (list 'a))) 99: (eqv? x x)) @result{} #t 100: @end example 101: @end defun 102: 103: @defun equal? obj1 obj2 104: [R5RS+] 105: @c EN 106: If @var{obj1} and @var{obj2} are both aggregate types, 107: @code{equal?} compares its elements recursively. 108: Otherwise, @code{equal?} behaves the same as @code{eqv?}. 109: 110: If @var{obj1} and @var{obj2} are other than booleans, numbers, 111: characters, pairs, strings and vectors, and the class of both 112: objects are the same, @code{equal?} calls the generic 113: function @code{object-equal?}. 114: By defining the method, users can extend the behavior of @code{equal?} 115: for user-defined classes. 116: @c JP 117: @var{obj1}??var{obj2}???ꥹ?Ȥ??????ʤɤ???ç·¿?Ǥ????ç¡¢ 118: @code{equal?}?ϺƵ?Ū??б??????Ʊ?Τ?de{equal?}??????Ƥæ¤?Þ¤??? 119: ?????Ǥʤ?????@code{equal?}??code{eqv?}??????褦?ˤդ????Þ¤??? 120: 121: ?⤷@code{obj1}??var{obj2}??????????ʸ??????ʸ?? 122: ?Ù¥????Τ?????????????????ԤΥ??饹???????????@code{equal?}???????Í¥????Õ¥??󥯥???@code{object-equal?}??Ó¤Þ¤??? 123: @code{object-equal?}?˥᥽?åɤ????뤳?Ȥˤ??? 124: ?桼?????Υǡ???????Ф???ode{equal?}?ο???????????뤳?Ȥ??Ǥ??Þ¤??? 125: @c COMMON 126: 127: @example 128: (equal? (list 1 2) (list 1 2)) @result{} #t 129: (equal? "abc" "abc") @result{} #t 130: (equal? 100 100) @result{} #t 131: (equal? 100 100.0) @result{} #f 132: @end example 133: 134: @c EN 135: Note: If both @var{obj1} and @var{obj2} have circular structure, 136: @code{equal?} may diverge. You might want to use @code{isomorph?} 137: instead (see @ref{Determine isomorphism}). 138: @c JP 139: ?: @var{obj1}??var{obj2}???Ȥ??۴Ĺ?¤??äƤ????ç¡¢ 140: @code{equal?}?????????ǽ??????????? 141: ??????code{isomorph?} (@ref{Determine isomorphism}???? ???Ȥ?????⤷??????@c COMMON 142: @end defun 143: 144: @deffn {Generic Function} object-equal? obj1 obj2 145: @c EN 146: This generic function is called when @code{equal?} is called on the objects 147: it doesn't know about. You can define this method on your class 148: so that @code{equal?} can check equivalence. This method is supposed 149: to return @code{#t} if @var{obj1} is equal to @var{obj2}, @code{#f} 150: otherwise. If you want to check equivalence of elements recursively, 151: do not call @code{object-equal?} directly; call @code{equal?} on each element. 152: @c JP 153: @code{equal?}??̤??Î¥??Ö¥??????Ȥ?Ф??ƸƤФ줿???? 154: ???Î¥????Í¥????Õ¥??󥯥??????ƤФ???????ʬ??????????饹??Ф??????Υ᥽?åɤ????뤳?Ȥˤ???@code{equal?}???????Ô¤????????ʤ??????᥽?åɤϡ?@var{obj1}??var{obj2}?????ʤ?@code{#t}???????Ǥʤ???@code{#f}????ͤФʤ????????Ö¥??????Ȥγ??Ǥ?Ф??ƺƵ?Ū??????????????Ï¡? 155: @code{object-equal?}??ܸƤ֤ΤǤϤʤ???@code{equal?}???Ǥ?Ф????Ƥ֤褦?ˤ??Ʋ??????? 156: @c COMMON 157: 158: @example 159: (define-class <foo> () 160: ((x :init-keyword :x) 161: (y :init-keyword :y))) 162: 163: (define-method object-equal? ((a <foo>) (b <foo>)) 164: (and (equal? (slot-ref a 'x) (slot-ref b 'x)) 165: (equal? (slot-ref a 'y) (slot-ref b 'y)))) 166: 167: (equal? (make <foo> :x 1 :y (list 'a 'b)) 168: (make <foo> :x 1 :y (list 'a 'b))) 169: @result{} #t 170: 171: (equal? (make <foo> :x 1 :y (make <foo> :x 3 :y 4)) 172: (make <foo> :x 1 :y (make <foo> :x 3 :y 4))) 173: @result{} #t 174: @end example 175: @end deffn 176: 177: @c EN 178: Sometimes you want to test if two aggregate structures 179: are topologically equal, i.e., if one has a shared substructure, 180: the other has a shared substructure in the same way. 181: @code{Equal?} can't handle it; module @code{util.isomorph} 182: provides a procedure @code{isomorphic?} which does the job 183: (@xref{Determine isomorphism}). 184: @c JP 185: ???Ф??С??Õ¤??Ĥ???ç·¿???Ö¥??????Ȥ˴ؤ??Æ¡?ξ?Ô¤??È¥Ý¥í¥¸??Ū??ù¤·¤¤???È¡? 186: ???ʤ???????Í????ʬ??¤??äƤ??????⤦???????褦????¤??Í???Ƥ??뤫?ɤ?????Ù¤??????Ȥ?????????@code{equal?}?Ϥ??????ˤ??Ȥ??Þ¤??󡣥⥸?塼??ode{util.isomorph}??󶡤???ode{isomorphic?}?? 187: ???????˻Ȥ??Þ¤???(@ref{Determine isomorphism}?????? 188: @c COMMON 189: 190: @c ---------------------------------------------------------------------- 191: @node Numbers, Booleans, Equivalence, Core library 192: @section Numbers 193: @c NODE ?? 194: @c EN 195: Gauche supports the following types of numbers 196: @table @asis 197: @item multi-precision exact integer 198: There's no limit of the size of number except the memory of the machine. 199: @item multi-precision exact non-integral rational numbers. 200: Both denominator and numerator are represented by exact integers. 201: There's no limit of the size of number except the memory of the machine. 202: @item inexact floating-point real numbers 203: Using @code{double}-type of underlying C compiler, usually IEEE 64-bit 204: floating point number. 205: @item inexact floating-point complex numbers 206: Real part and imaginary part are represented by inexact floating-point 207: real numbers. 208: @end table 209: @c JP 210: Gauche?ϼ??Î¥????פο?ò¥µ¥Ý¡??Ȥ??Ƥ??Þ¤??? 211: @table @asis 212: @item ¿?Ĺ????Τ???????εö¤¹¸Â¤??????????Þ¤??? 213: @item ¿?Ĺ????Τ????????????Ò¤???Τ??????????????? 214: ?????ö¤¹¸Â¤??????????Þ¤??? 215: @item ?ư????????Τʼ¿???˻Ȥ?????@code{double}??????????????????EE 64bit?ư???????? 216: @item ?ư????????Τ???????ȵ?Ϥ??줾??ư????????Τʼ¿???????????? 217: @end table 218: @c COMMON 219: 220: @menu 221: * Number classes:: 222: * Numerical predicates:: 223: * Numerical comparison:: 224: * Arithmetics:: 225: * Numerical conversions:: 226: * Bitwise operations:: 227: @end menu 228: 229: @node Number classes, Numerical predicates, Numbers, Numbers 230: @subsection Number classes 231: @c NODE ????饹 232: 233: @deftp {Builtin Class} <number> 234: @deftpx {Builtin Class} <complex> 235: @deftpx {Builtin Class} <real> 236: @deftpx {Builtin Class} <rational> 237: @deftpx {Builtin Class} <integer> 238: @clindex number 239: @clindex complex 240: @clindex real 241: @clindex rational 242: @clindex integer 243: @c EN 244: These classes consist a class hierarchy of number objects. 245: @code{<complex>} inherits @code{<number>}, @code{<real>} inherits 246: @code{<complex>},@code{<rational>} inherits @code{<real>} 247: and @code{<integer>} inherits @code{<rational>}. 248: 249: Note that these classes does not exactly corresponds to the 250: number hierarchy defined in R5RS. Especially, 251: only exact integers are the instances of the @code{<integer>} 252: class. That is, 253: @c JP 254: ????Ö¥??????ȤΥ??饹?????????Þ¤???@code{<complex>}??@code{<number>}???????@code{<real>}??code{<complex>}??????? 255: @code{<rational>}??code{<real>}??????? 256: @code{<integer>}??code{<rational>}??????Þ¤??? 257: 258: ?????Î¥??饹??5RS?????????????γ???Ȥ?????????????Þ¤?????Ë¡?@code{<integer>}???饹?Î¥??ó¥¹¥??󥹤?5RS?Ǥ???exact integer?Τߤˤʤ????? 259: @c COMMON 260: @lisp 261: (integer? 1) @result{} #t 262: (is-a? 1 <integer>) @result{} #t 263: (is-a? 1 <real>) @result{} #t 264: 265: (integer? 1.0) @result{} #t 266: (is-a? 1.0 <integer>) @result{} #f 267: (is-a? 1.0 <real>) @result{} #t 268: 269: (class-of (expt 2 100)) @result{} #<class <integer>> 270: (class-of (sqrt -3)) @result{} #<class <complex>> 271: @end lisp 272: @end deftp 273: 274: @node Numerical predicates, Numerical comparison, Number classes, Numbers 275: @subsection Numerical predicates 276: @c NODE ??˴ؤ?????@defun number? obj 277: @defunx complex? obj 278: @defunx real? obj 279: @defunx rational? obj 280: @defunx integer? obj 281: [R5RS] 282: @c EN 283: Returns @code{#t} if @var{obj} is a number, a complex number, a real number, 284: a rational number or an integer, respectively. In Gauche, a set of 285: numbers is the same as a set of complex numbers, and a set of 286: rational numbers is the same as a set of real numbers 287: (since we have only limited-precision floating numbers). 288: @c JP 289: @var{obj}?????줾????Ê£??ô¡¢¼Â¿?????????@code{#t}????Þ¤??? 290: Gauche?Ǥϡ??????Ê£??????Ʊ?????ê¡¢Í???????¿????Ʊ???ư???????????ΤǤ??뤿??Ǥ??? 291: @c COMMON 292: 293: @example 294: (complex? 3+4i) @result{} #t 295: (complex? 3) @result{} #t 296: (real? 3) @result{} #t 297: (real? -2.5+0.0i) @result{} #t 298: (real? #e1e10) @result{} #t 299: (integer? 3+0i) @result{} #t 300: (integer? 3.0) @result{} #t 301: @end example 302: @end defun 303: 304: @defun exact? obj 305: @defunx inexact? obj 306: [R5RS] 307: @c EN 308: Returns @code{#t} if @var{obj} is an exact number and an inexact number, 309: respectively. 310: @c JP 311: @var{obj}?????줾???Τʿ????Τʿ??@code{#t}????Þ¤??? 312: @c COMMON 313: 314: @example 315: (exact? 1) @result{} #t 316: (exact? 1.0) @result{} #f 317: (inexact? 1) @result{} #f 318: (inexact? 1.0) @result{} #t 319: 320: (exact? (modulo 5 3)) @result{} #t 321: (inexact? (modulo 5 3.0)) @result{} #f 322: @end example 323: @end defun 324: 325: @defun zero? z 326: [R5RS] 327: @c EN 328: Returns @code{#t} if a number @var{z} equals to zero. 329: @c JP 330: ??var{z}?????????????@code{#t}????Þ¤??? 331: @c COMMON 332: 333: @example 334: (zero? 1) @result{} #f 335: (zero? 0) @result{} #t 336: (zero? 0.0) @result{} #t 337: (zero? 0.0+0.0i) @result{} #t 338: @end example 339: @end defun 340: 341: @defun positive? x 342: @defunx negative? x 343: [R5RS] 344: @c EN 345: Returns @code{#t} if a real number @var{x} is positive and negative, 346: respectively. It is an error to pass a non-real number. 347: @c JP 348: ?¿?r{x}?????줾???Þ¤??????ode{#t}????Þ¤??? 349: ??¿?Ϥ??È¥??顼?ˤʤ????? 350: @c COMMON 351: @end defun 352: 353: @defun odd? n 354: @defunx even? n 355: [R5RS] 356: @c EN 357: Returns @code{#t} if an integer @var{n} is odd and even, 358: respectively. It is an error to pass a non-integral number. 359: @c JP 360: ???r{n}?????줾??????϶?ʤ?ode{#t}????Þ¤??? 361: ????Ϥ??È¥??顼?ˤʤ????? 362: @c COMMON 363: 364: @example 365: (odd? 3) @result{} #t 366: (even? 3) @result{} #f 367: (odd? 3.0) @result{} #t 368: @end example 369: @end defun 370: 371: @defun fixnum? n 372: @defunx bignum? n 373: @c EN 374: Returns @code{#t} iff @var{n} is an exact integer whose internal 375: representation is @var{fixnum} and @var{bignum}, respectively. 376: Portable Scheme programs don't need to care about the internal 377: representation of integer. These are for certain low-level 378: routines that does particular optimization. 379: @c JP 380: @var{n}????Τ??????????Ū?ˤ??줾??ar{fixnum}?⤷????var{bignum} 381: ?????????????????code{#t}????Þ¤??? 382: ???Scheme?×¥í¥°????????ɽ???òµ¤¤Ë¤?????Ϥ????????????μ????Ï¡??????κ????????????³???Τ?????Õ¤??????Þ¤??? 383: @c COMMON 384: @end defun 385: 386: @node Numerical comparison, Arithmetics, Numerical predicates, Numbers 387: @subsection Numerical comparison 388: @c NODE ???? 389: 390: @defun @code{=} z1 z2 z3 @dots{} 391: [R5RS] 392: @c EN 393: If all the numbers @var{z} are equal, returns @code{#t}. 394: @c JP 395: Í¿????????Ƥο?var{z}?????????@code{#t}????Þ¤??? 396: @c COMMON 397: 398: @example 399: (= 2 2) @result{} #t 400: (= 2 3) @result{} #f 401: (= 2 2.0) @result{} #t 402: (= 2 2.0 2.0+0i) @result{} #t 403: (= 2/4 1/2) @result{} #t 404: @end example 405: @end defun 406: 407: @defun @code{<} x1 x2 x3 @dots{} 408: @defunx @code{<=} x1 x2 x3 @dots{} 409: @defunx @code{>} x1 x2 x3 @dots{} 410: @defunx @code{>=} x1 x2 x3 @dots{} 411: [R5RS] 412: @c EN 413: Returns @code{#t} If all the real numbers @var{x} are 414: monotonically increasing, 415: monotonically nondecreasing, monotonically decreasing, or monotonically 416: nonincreasing, respectively. 417: @c JP 418: Í¿????????Ƥμ¿?r{x}?????줾??Ä´??á?ñĴ???????ñĴ??????ñĴ???ä??Ƥ??????code{#t}????Þ¤??? 419: @c COMMON 420: @end defun 421: 422: 423: @defun max x1 x2 @dots{} 424: @defunx min x1 x2 @dots{} 425: [R5RS] 426: @c EN 427: Returns a maximum or minimum number in the given real numbers, respectively. 428: @c JP 429: Í¿???????¿????????줾????ȺǾ??Τ?????Þ¤??? 430: @c COMMON 431: @end defun 432: 433: @defun min&max x1 x2 @dots{} 434: @c EN 435: Returns a maximum and minimum number in the given real numbers. 436: @c JP 437: Í¿???????¿ô¤«¤é¡¢????ȺǾ??????ͤ???Þ¤??? 438: @c COMMON 439: @end defun 440: 441: @node Arithmetics, Numerical conversions, Numerical comparison, Numbers 442: @subsection Arithmetics 443: @c NODE ??α黻 444: 445: @defun @code{+} z @dots{} 446: @defunx @code{*} z @dots{} 447: [R5RS] 448: @c EN 449: Returns the sum or the product of given numbers, respectively. 450: If no argument is given, @code{(+)} yields 0 and @code{(*)} yields 1. 451: @c JP 452: Í¿????????¤?Ѥò¤½¤ì¤¾?????Þ¤?????Í¿?????ʤ????? 453: @code{(+)} ??0?? @code{(*)} ??1 ?Ȥʤ????? 454: @c COMMON 455: @end defun 456: 457: @defun @code{-} z1 z2 @dots{} 458: @defunx @code{/} z1 z2 @dots{} 459: [R5RS] 460: @c EN 461: If only one number @var{z1} is given, returns its 462: negation and reciprocal, respectively. 463: 464: If more than one number are given, returns: 465: @example 466: @var{z1} - @var{z2} - @var{z3} @dots{} 467: @var{z1} / @var{z2} / @var{z3} @dots{} 468: @end example 469: respectively. 470: @c JP 471: ????????Í¿?????????????줾??gation?ȵտ?Ö¤??Þ¤??? 472: 473: 2?İʾ????Í¿?????????????줾?켡?η?????Þ¤??? 474: @example 475: @var{z1} - @var{z2} - @var{z3} @dots{} 476: @var{z1} / @var{z2} / @var{z3} @dots{} 477: @end example 478: @c COMMON 479: 480: @example 481: (- 3) @result{} -3 482: (- -3.0) @result{} 3.0 483: (- 5+2i) @result{} -5.0-2.0i 484: (/ 3) @result{} 1/3 485: (/ 5+2i) @result{} 0.172413793103448-0.0689655172413793i 486: 487: (- 5 2 1) @result{} 2 488: (- 5 2.0 1) @result{} 2.0 489: (- 5+3i -i) @result{} 5.0+2.0i 490: (/ 14 6) @result{} 7/3 491: (/ 6+2i 2) @result{} 3.0+1.0i 492: @end example 493: 494: @c EN 495: Note: Gauche didn't have exact rational number support until 0.8.8; 496: before that, @code{/} coerced the result to inexact even if both 497: divisor and dividend were exact numbers, when the result wasn't 498: a whole number. It is not the case anymore. 499: 500: If the existing code relies on the old behavior, it runs 501: very slowly on the newer versions of Gauche, since the calculation 502: proceeds with exact rational arithmetics that is much slower than 503: floating point arithmetics. You want to use @code{/.} below 504: to use fast inexact arithmetics (unless you 505: need exact results). 506: @c JP 507: ???0.8.8?Þ¤?auche????Τ???????Ý¡??Ȥ??Ƥ??餺?? 508: ??????Ͻ???ô¤¬¤È¤???Τʿ???äƤ⾦?????ʤ????? 509: ?????Τʿ??Ñ´????????Þ¤?????????auche?Ϥ????ǤϤ??????? 510: ???????ɤ??????auche?Τդ?????Ƥˤ??Ƥ??????? 511: ®???????????????ǽ?????????????Τ?????é»»???ư?????é»»???Ϥ뤫??Ù¤????????? 512: (??Τʷ???????ΤǤʤ????????˽Ҥ٤?ode{/.}?????ɤ??Ǥ??礦?? 513: @c COMMON 514: @end defun 515: 516: @defun +. z @dots{} 517: @defunx *. z @dots{} 518: @defunx -. z1 z2 @dots{} 519: @defunx /. z1 z2 @dots{} 520: @c EN 521: Like @code{+}, @code{*}, @code{-}, and @code{/}, but the arguments 522: are coerced to inexact number. So they always return inexact number. 523: These are useful when you know you don't need exact calculation 524: and want to avoid accidental overhead of bignums and/or exact 525: rational numbers. 526: @c JP 527: @code{+}??@code{*}??@code{-}??@code{/}?????Ǥ??????????Τ????Ñ´????ì¡¢?????????Τʿ?ʤ????? 528: ??ο???×»??????Ç¡?bignum???Τ???????ä????äƤ??Þ¤? 529: ???Ȥˤ??????Хإåɤ?????????????? 530: @c COMMON 531: @end defun 532: 533: @defun abs z 534: [R5RS+] 535: @c EN 536: For real number @var{z}, returns an absolute value of it. 537: For complex number @var{z}, returns the magnitude of the number. 538: The complex part is Gauche extension. 539: @c JP 540: ?¿?var{z}??Ф??Ƥϡ???????????Þ¤??? 541: Ê£???var{z}??Ф??Ƥϡ?????agnitude????Þ¤??? 542: Ê£???????Τ?auche?γ???Ǥ??? 543: @c COMMON 544: @example 545: (abs -1) @result{} 1 546: (abs -1.0) @result{} 1.0 547: (abs 1+i) @result{} 1.4142135623731 548: @end example 549: @end defun 550: 551: @defun quotient n1 n2 552: @defunx remainder n1 n2 553: @defunx modulo n1 n2 554: [R5RS] 555: @c EN 556: Returns the quotient, remainder and modulo of dividing an integer @var{n1} 557: by an integer @var{n2}. The result is an exact number only if 558: both @var{n1} and @var{n2} are exact numbers. 559: 560: Remainder and modulo differ when either one of the arguments is negative. 561: Remainder @var{R} and quotient @var{Q} have the following relationship. 562: @example 563: @var{n1} = @var{Q} * @var{n2} + @var{R} 564: @end example 565: where @code{abs(@var{Q}) = floor(abs(@var{n1})/abs(@var{n2}))}. 566: Consequently, @var{R}'s sign is always the same as @var{n1}'s. 567: 568: On the other hand, modulo works as expected for positive @var{n2}, 569: regardless of the sign of @var{n1} 570: (e.g. @code{(modulo -1 @var{n2}) == @var{n2} - 1}). 571: If @var{n2} is negative, it is mapped to the positive case by 572: the following relationship. 573: @example 574: modulo(@var{n1}, @var{n2}) = @minus{}modulo(@minus{}@var{n1}, @minus{}@var{n2}) 575: @end example 576: Consequently, @var{modulo}'s sign is always the same as @var{n2}'s. 577: @c JP 578: ???r{n1}???r{n2}?dz?????(quotient)????;??emainder, modulo) 579: ????Þ¤??? @var{n1}??var{n2}????????Τʿ?λ??Τߡ????????Τʿ?ˤʤ????? 580: 581: @code{remainder}??code{modulo} ?Ϥɤ?餫?ΰ???λ??˰ۤʤ?????Þ¤??? 582: Remainder @var{R} ?Ⱦ? @var{Q} ?Ȥϼ??δط??????????? 583: @example 584: @var{n1} = @var{Q} * @var{n2} + @var{R} 585: @end example 586: ?????Ǿ??ˤĤ???@code{abs(@var{Q}) = floor(abs(@var{n1})/abs(@var{n2}))} 587: ?Ǥ????é¡¢@var{R}????Ͼ?@var{n1}??????ˤʤ????? 588: 589: ????modulo??var{n2}????λ???var{n1}????˴ؤ???????褦?????Þ¤? 590: (?: @code{(modulo -1 @var{n2}) == @var{n2} - 1})?? 591: @var{n2}????ξ??ϼ??μ??ˤ?????ξ??˥ޥåפǤ??Þ¤??? 592: @example 593: modulo(@var{n1}, @var{n2}) = @minus{}modulo(@minus{}@var{n1}, @minus{}@var{n2}) 594: @end example 595: ???????äơ?modulo?η?????Ͼ?@var{n2}?????????ˤʤ????? 596: @c COMMON 597: @example 598: (remainder 10 3) @result{} 1 599: (modulo 10 3) @result{} 1 600: 601: (remainder -10 3) @result{} -1 602: (modulo -10 3) @result{} 2 603: 604: (remainder 10 -3) @result{} 1 605: (modulo 10 -3) @result{} -2 606: 607: (remainder -10 -3) @result{} -1 608: (modulo -10 -3) @result{} -1 609: @end example 610: @end defun 611: 612: @defun quotient&remainder n1 n2 613: @c EN 614: Calculates the quotient and the remainder of dividing integer @var{n1} 615: by integer @var{n2} simultaneously, and returns them as two values. 616: @c JP 617: ???r{n1}???r{n2}?dz?????(quotient)????;??emainder) 618: ????Ë·×»?????2?Ĥ?ͤȤ???Ö¤??Þ¤??? 619: @c COMMON 620: @end defun 621: 622: @defun gcd n @dots{} 623: @defunx lcm n @dots{} 624: [R5RS] 625: @c EN 626: Returns the greatest common divisor or the least common multiplier 627: of the given integers, respectively 628: @c JP 629: Í¿????????????????Ǿ????ò¤½¤ì¤¾?????Þ¤??? 630: @c COMMON 631: @end defun 632: 633: @defun numerator q 634: @defunx denominator q 635: [R5RS] 636: @c EN 637: Returns the numerator and denominator of a rational number @var{q}. 638: @c JP 639: Í???r{q}????Ò¤????ò¤½¤ì¤¾?????Þ¤??? 640: @c COMMON 641: @end defun 642: 643: 644: @defun floor x 645: @defunx ceiling x 646: @defunx truncate x 647: @defunx round x 648: [R5RS] 649: @c EN 650: The argument @var{x} must be a real number. 651: @code{Floor} and @code{ceiling} return a maximum integer that 652: isn't greater than @var{x} and a minimum integer that isn't less 653: than @var{x}, respectively. 654: @var{Truncate} returns an integer that truncates 655: @var{x} towards zero. @var{Round} returns an integer that is closest 656: to @var{x}. If fractional part of @var{x} is exactly 0.5, @var{round} 657: returns the closest even integer. 658: @c JP 659: ??ar{x}?ϼ¿?ʤ????ʤ?????code{floor}??code{ceiling}?Ϥ??줾??var{x}????ʤ????????@var{x}?ò²¼²?ʤ??Ǿ??????Ö¤??Þ¤??? 660: @var{truncate}??var{x}?ξ???ò¥¼¥?????˸????ä?ڼΤƤ????Ö¤??Þ¤??? 661: @var{round}??var{x}?˺Ǥ??????Ö¤??Þ¤???@var{x}???????0.5?Ԥä????ä?????var{round}?ϺǤ????????Þ¤??? 662: @c COMMON 663: @end defun 664: 665: @defun floor->exact x 666: @defunx ceiling->exact x 667: @defunx truncate->exacte x 668: @defunx round->exact x 669: @c EN 670: These are convenience procedures of the popular 671: phrase @code{(inexact->exact (floor x))} etc. 672: @c JP 673: ?????Ï¡???Ф???ode{(inexact->exact (floor x))} ?????ñ¤¯¤??? 674: ?????Ǥ??? 675: @c COMMON 676: @end defun 677: 678: @defun clamp x &optional min max 679: @c EN 680: Returns 681: @example 682: @var{min} @r{if} @var{x} @code{<} @var{min} 683: @var{x} @r{if} @var{min} @code{<=} @var{x} @code{<=} @var{max} 684: @var{max} @r{if} @var{max} @code{<} @var{x} 685: @end example 686: If @var{min} or @var{max} is omitted or @code{#f}, it is regarded 687: as @var{-infinity} or @var{+infinity}, respectively. 688: Returns an exact integer only if all the given numbers are exact integers. 689: @c JP 690: @example 691: @var{min} @r{if} @var{x} @code{<} @var{min} 692: @var{x} @r{if} @var{min} @code{<=} @var{x} @code{<=} @var{max} 693: @var{max} @r{if} @var{max} @code{<} @var{x} 694: @end example 695: ????Þ¤????⤷@var{min}?Þ¤???var{max}???????????@code{#f}??Í¿???????é¡¢ 696: ???줾??var{-???⤷????@var{+???Ȳ???????? 697: Í¿????????Ƥο????Τ??????˸¤??Τ????Ö¤??Þ¤??? 698: @c COMMON 699: @example 700: (clamp 3.1 0.0 1.0) @result{} 1.0 701: (clamp 0.5 0.0 1.0) @result{} 0.5 702: (clamp -0.3 0.0 1.0) @result{} 0.0 703: (clamp -5 0) @result{} 0 704: (clamp 3724 #f 256) @result{} 256 705: @end example 706: @end defun 707: 708: 709: @defun exp z 710: @defunx log z 711: @defunx sin z 712: @defunx cos z 713: @defunx tan z 714: @defunx asin z 715: @defunx acos z 716: @defunx atan z 717: [R5RS] 718: @c EN 719: Transcendental functions. Work for complex numbers as well. 720: @c JP 721: Ķ?Û´Ø¿????Ê£???????Þ¤??? 722: @c COMMON 723: @end defun 724: 725: @defun atan y x 726: [R5RS] 727: @c EN 728: For real numbers @var{x} and @var{y}, returns @code{atan(@var{y}/@var{x})}. 729: @c JP 730: ?¿?r{x}??var{y}??Ф???code{atan(@var{y}/@var{x})}????Þ¤??? 731: @c COMMON 732: @end defun 733: 734: @defun sinh z 735: @defunx cosh z 736: @defunx tanh z 737: @defunx asinh z 738: @defunx acosh z 739: @defunx atanh z 740: @c EN 741: Hyperbolic trigonometric functions. Work for complex numbers as well. 742: @c JP 743: ?????Ø¿????Ê£???????Þ¤??? 744: @c COMMON 745: @end defun 746: 747: @defun sqrt z 748: [R5RS] 749: @c EN 750: Returns a square root of a complex number @var{z}. 751: The branch cut scheme is the same as Common Lisp. 752: For real numbers, it returns a positive root. 753: @c JP 754: Ê£???r{z}???????ΤҤȤĤ???Þ¤????Þ´???????ommon Lisp??????Ǥ??? 755: ?¿?Ф??Ƥ????????????Þ¤??? 756: @c COMMON 757: @end defun 758: 759: @defun expt z1 z2 760: [R5RS] 761: @c EN 762: Returns @var{z1}^@var{z2} (@var{z1} powered by @var{z2}), 763: where @var{z1} and @var{z2} are complex numbers. 764: @c JP 765: Ê£???r{z1}, @var{z2}??Ф??Æ¡? 766: @var{z1}^@var{z2} (@var{z1}??var{z2}?????Þ¤??? 767: @c COMMON 768: @end defun 769: 770: @node Numerical conversions, Bitwise operations, Arithmetics, Numbers 771: @subsection Numerical conversions 772: @c NODE ???Ñ´? 773: 774: @defun make-rectangular x1 x2 775: @defunx make-polar x1 x2 776: [R5RS] 777: @c EN 778: Creates a complex number from two real numbers, @var{x1} and @var{x2}. 779: @code{make-rectangular} returns @var{x1} + @b{i}@var{x2}. 780: @code{make-polar} returns @var{x1}@b{e}^(@b{i}@var{x2}). 781: @c JP 782: ??Ĥμ¿?r{x1}??var{x2}?????????Þ¤??? 783: @code{make-rectangular} ??@var{