finite?(3scm)
π finite?, infinite?, nan? β floating-point classification procedures
Okay bestie, welcome to finite?(3scm), aka the Scheme Programmer's Manual page where we vibe-check your numbers π. Yes, that's a 3scm section man page, and yes we're doing this together, hand in hand π«Ά.
β¨ LIBRARY β¨
Import these depending on your whole situation:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme inexact)) ;R7RS
(import (rnrs arithmetic flonums)) ;R6RS
Like, pick your era. R6RS girlie or R7RS girlie, both valid π .
πΈ SYNOPSIS πΈ
The R6RS lineup, no cap:
;; R6RS
(finite? x)
(infinite? x)
(nan? x)
(flfinite? fl)
(flinfinite? fl)
(flnan? fl)
And the R7RS lineup, which is giving complex number main character energy:
;; R7RS
(finite? z)
(infinite? z)
(nan? z)
π¦ DESCRIPTION π¦
So these are numerical predicates, right? They test a number object for one specific property and hand you back #t or #f. That's it, that's the whole job. Simple, iconic, obsessed β¨.
finite? β Tests whether x is not an infinity and not a NaN. Basically "is this a normal well-adjusted number" vibes.
infinite? β Tests whether x is an infinity. Going off forever, lowkey π.
nan? β Tests whether x is a NaN. That's "Not a Number", bestie, and yes it's exactly as messy as it sounds π.
The R7RS variants are extended to support complex numbers β ya know, if the implementation supports complex numbers. Conditional slay.
finite? β Tests whether the real and imaginary parts of z are finite.
infinite? β Tests whether the real or imaginary parts of z are infinite. Note the "or", she's inclusive π
nan? β Tests whether the real or imaginary parts of z are NaN.
The flonum variants in R6RS are restricted to flonum arguments. No mixing, no mingling, flonums only fr fr.
β οΈ Heads up bestie: the results may be unreliable because a small inaccuracy may affect the result. Yeah. Floating point. We don't talk about it π.
π RETURN VALUES π
These procedures return a single value; a boolean object. One value. A boolean. That's the whole tea β.
β EXAMPLES β
Watch and learn, this is the good part:
(finite? +inf.0) => #f
(finite? 5) => #t
(finite? 5.0) => #t
(infinite? 5.0) => #f
(infinite? +inf.0) => #t
(nan? 32) => #f
And now the R7RS examples, where the complex numbers show up and absolutely ate π :
;; R7RS examples
(finite? +inf.0) => #f
(finite? 3.0+inf.0i) => #f
(infinite? +inf.0) => #t
(infinite? +nan.0) => #f
(infinite? 3.0+inf.0i) => #t
(nan? +nan.0) => #t
(nan? +nan.0+5.0i) => #t
(nan? 1+2i) => #f
Look at (infinite? +nan.0) => #f β NaN is not an infinity, okay, she's her own thing π.
π§ APPLICATION USAGE π§
So a super common use for the nan? procedure is to replace NaNs with some another value, such as zero. Like, find the NaN, swap it out, move on with your life π«Ά.
And listen: it's not possible to check for NaNs with code like (= x +nan.0). Don't do it. It will not work. It is not that girl. Use nan? π.
π₯ COMPATIBILITY π₯
The variants in R6RS only support real numbers. That's it, that's the note. Real numbers only, no imaginary besties invited π€.
π ERRORS π
This procedure can raise exceptions with the following condition types:
&assertion(R6RS) β The wrong number of arguments was passed or an argument was outside its domain. In particular, the flonum variants require a flonum argument. Like, they really require it, no exceptions (well, an exception, technically π).
R7RS β The assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically.
π "fail catastrophically" β yeah, that's the actual wording, and honestly? Iconic. Be careful.
π SEE ALSO π
zero?(3scm)β go say hi, she's the neighbor π
π STANDARDS π
R6RS, R7RS. Both. We contain multitudes β¨.
πΈ HISTORY πΈ
These procedures are new in R6RS and were later also added in R7RS with names that coincide with those in R6RS. Yeah, same names, different era, the crossover event nobody asked for but everybody needed π
Also, the C99 language and POSIX offer a more detailed classification which includes subnormals, see fpclassify(3). If you want the extended director's cut, that's where to go π.
π«Ά AUTHORS π«Ά
This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.
okay that's the whole page, bestie β literally every flag, value, example and caveat is in there, no cap π. This one's dated 2022-05-03 and it's the finite?(3scm) page of the Scheme Programmer's Manual, so go forth and vibe-check your floats responsibly β¨ππ¦
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms