finite?(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… 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