숫자 타입
숫자 타입 (Number Types)
Racket에서 수(number)는 복소수(complex number)를 포함한 여러 계층으로 나뉘어요. 이 절에서는 각 숫자 계층을 판별하는 술어(predicate)와 숫자를 다른 타입으로 바꾸는 변환 함수들을 다룹니다.
본문
프로시저
(number? v) → boolean?
v : any/c
v가 숫자이면 #t, 그렇지 않으면 #f를 반환해요.
예시:
(number? 1)→#t
(number? 2+3i)→#t
(number? "hello")→#f
(number? +nan.0)→#t
프로시저
(complex? v) → boolean?
v : any/c
(number? v)를 반환해요. 모든 숫자는 복소수이기 때문이죠.
프로시저
(real? v) → boolean?
v : any/c
v가 실수이면 #t, 그렇지 않으면 #f를 반환해요.
예시:
(real? 1)→#t
(real? +inf.0)→#t
(real? 2+3i)→#f
(real? 2.0+0.0i)→#f
(real? "hello")→#f
프로시저
(rational? v) → boolean?
v : any/c
v가 유리수(rational number)이면 #t, 그렇지 않으면 #f를 반환해요.
예시:
(rational? 1)→#t
(rational? +inf.0)→#f
(rational? "hello")→#f
프로시저
(integer? v) → boolean?
v : any/c
v가 정수인 숫자이면 #t, 그렇지 않으면 #f를 반환해요.
예시:
(integer? 1)→#t
(integer? 2.3)→#f
(integer? 4.0)→#t
(integer? +inf.0)→#f
(integer? 2+3i)→#f
(integer? "hello")→#f
프로시저
(exact-integer? v) → boolean?
v : any/c
(and (integer? v) (exact? v))를 반환해요.
예시:
(exact-integer? 1)→#t
(exact-integer? 4.0)→#f
프로시저
(exact-nonnegative-integer? v) → boolean?
v : any/c
(and (exact-integer? v) (not (negative? v)))를 반환해요.
예시:
(exact-nonnegative-integer? 0)→#t
(exact-nonnegative-integer? -1)→#f
프로시저
(exact-positive-integer? v) → boolean?
v : any/c
(and (exact-integer? v) (positive? v))를 반환해요.
예시:
(exact-positive-integer? 1)→#t
(exact-positive-integer? 0)→#f
프로시저
(inexact-real? v) → boolean?
v : any/c
(and (real? v) (inexact? v))를 반환해요.
프로시저
(fixnum? v) → boolean?
v : any/c
v가 fixnum이면 #t, 그렇지 않으면 #f를 반환해요.
참고: 이 함수의 결과는 플랫폼마다 다르므로, 문법 변환기에서 이 함수를 쓰면 플랫폼마다 다른 바이트코드 파일이 만들어질 수 있어요. fixnum-for-every-system?도 함께 보세요.
프로시저
(flonum? v) → boolean?
v : any/c
v가 flonum이면 #t, 그렇지 않으면 #f를 반환해요.
프로시저
(double-flonum? v) → boolean?
v : any/c
flonum?와 동일해요.
프로시저
(single-flonum? v) → boolean?
v : any/c
v가 single-flonum(즉 단정밀도 부동소수점 숫자)이면 #t, 그렇지 않으면 #f를 반환해요.
프로시저
(single-flonum-available?) → boolean?
현재 플랫폼이 single-flonum을 지원하면 #t, 그렇지 않으면 #f를 반환해요.
현재 single-flonum-available?는 (system-type 'vm)이 'racket을 만들어 낼 때 #t를, 그 외에는 #f를 만들어 냅니다.
결과가 #f이면 single-flonum?도 모든 인자에 대해 #f를 만들어 내요.
버전 7.3.0.5 (패키지 base)에서 추가됨.
프로시저
(zero? z) → boolean?
z : number?
(= 0 z)를 반환해요.
예시:
(zero? 0)→#t
(zero? -0.0)→#t
프로시저
(positive? x) → boolean?
x : real?
(> x 0)를 반환해요.
예시:
(positive? 10)→#t
(positive? -10)→#f
(positive? 0.0)→#f
프로시저
(negative? x) → boolean?
x : real?
(< x 0)를 반환해요.
예시:
(negative? 10)→#f
(negative? -10)→#t
(negative? -0.0)→#f
프로시저
(even? n) → boolean?
n : integer?
(zero? (modulo n 2))를 반환해요.
예시:
(even? 10.0)→#t
(even? 11)→#f
(even? +inf.0)→even?: contract violation, expected: integer?, given: +inf.0
프로시저
(odd? n) → boolean?
n : integer?
(not (even? n))을 반환해요.
예시:
(odd? 10.0)→#f
(odd? 11)→#t
(odd? +inf.0)→odd?: contract violation, expected: integer?, given: +inf.0
프로시저
(exact? z) → boolean?
z : number?
z가 정확한(exact) 숫자이면 #t, 그렇지 않으면 #f를 반환해요.
예시:
(exact? 1)→#t
(exact? 1.0)→#f
프로시저
(inexact? z) → boolean?
z : number?
z가 부정확한(inexact) 숫자이면 #t, 그렇지 않으면 #f를 반환해요.
예시:
(inexact? 1)→#f
(inexact? 1.0)→#t
프로시저
(inexact->exact z) → exact?
z : number?
z를 정확한 숫자로 변환해요. z가 이미 정확하면 그대로 반환됩니다. z가 +inf.0, -inf.0, +nan.0, +inf.f, -inf.f, 또는 +nan.f이면 exn:fail:contract 예외가 발생해요.
예시:
(inexact->exact 1)→1
(inexact->exact 1.0)→1
프로시저
(exact->inexact z) → inexact?
z : number?
z를 부정확한 숫자로 변환해요. z가 이미 부정확하면 그대로 반환됩니다.
예시:
(exact->inexact 1)→1.0
(exact->inexact 1.0)→1.0
프로시저
(real->single-flonum x) → single-flonum?
x : real?
x를 단정밀도 부동소수점 숫자로 변환해요. x가 이미 단정밀도 부동소수점 숫자면 그대로 반환됩니다.
프로시저
(real->double-flonum x) → flonum?
x : real?
x를 배정밀도 부동소수점 숫자로 변환해요. x가 이미 배정밀도 부동소수점 숫자면 그대로 반환됩니다.