fpclassify(n)

πŸ“¦
version 9.0.3 Β· 2 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05

πŸ’– fpclassify(n) β€” the Tcl Float Classifier πŸ’–

okay bestie, so this little cutie lives in the n section of the Tcl manual, and it's literally called the Tcl Float Classifier. We're talking Tcl 9.0 energy. She's small, she's precise, she's the friend who tells you exactly what kind of weird your number is. πŸ¦‹


✨ NAME ✨

fpclassify β€” Floating point number classification of Tcl values


🌸 SYNOPSIS 🌸

package require tcl 9.0

fpclassify value

πŸ’… DESCRIPTION πŸ’…

So here's the tea: fpclassify takes a floating point number, value, and hands you back one of these strings describing what kind of float it is. Not a vibe check β€” a number check. πŸŽ€

  • zero β€” value is a floating point zero.
  • subnormal β€” value is the result of a gradual underflow. (😭 the precision loss era)
  • normal β€” value is an ordinary floating-point number. Not zero, not subnormal, not infinite, not NaN. Just a chill, well-adjusted float. ✨
  • infinite β€” value is a floating-point infinity.
  • nan β€” value is Not-a-Number. πŸ’€
heads up bestie: fpclassify throws an error if value is not a floating-point value and can't be converted into one. She does not play about that. πŸ™ˆ

πŸ”₯ EXAMPLE πŸ”₯

Here's how you check whether the result of a computation is numerically safe or not. (note: this does not guard against numerical errors β€” it only guards against representational problems. iykyk.)

set value [command-that-computes-a-value]
switch [fpclassify $value] {
    normal - zero {
        puts "Result is $value"
    }
    infinite {
        puts "Result is infinite"
    }
    subnormal {
        puts "Result is $value - WARNING! precision lost"
    }
    nan {
        puts "Computation completely failed"
    }
}

the subnormal branch screaming "WARNING! precision lost" is so real, honestly. 🫢


πŸ‘€ SEE ALSO πŸ‘€


⭐ KEYWORDS ⭐

  • floating point

🌈 STANDARDS 🌈

This command depends on the fpclassify() C macro conforming to β€œISO C99” (i.e., to ISO/IEC 9899:1999). yes she's standards-compliant, she's got her life together. πŸ‘‘


Copyright Β© 2018 Kevin B. Kenny <kennykb@acm.org>. All rights reserved


and that's the whole page bestie, no cap. πŸ“ this diva shipped with Tcl 9.0 (that's the version stamped on the page, front and back) β€” mwah, periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from