Library contents

Go math.IsInf and IsNaN

Check whether a temperature is a finite number before including it in a daily average.

A simple example

math.IsInf and IsNaN

  1. Start with 2.0
  2. Do this Test for a finite value
  3. Result Yes
An ordinary number such as 2.0 is finite. Infinity and NaN fail this test.
main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    if !math.IsInf(2.0, 0) && !math.IsNaN(2.0) {
        fmt.Println("yes")
    } else {
        fmt.Println("no")
    }
}
Output
yes

Syntax

Go syntax
!math.IsInf(21.5, 0) && !math.IsNaN(21.5)
Run these examples in Go

Save one example at a time as main.go. Use Go 1.21 or later.

go run main.go

Reject unusable sensor readings

A sensor feed can contain values that cannot be used in an ordinary calculation. Check the incoming readings before allowing them to affect a stored average or a chart.

Resultyes
main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    if !math.IsInf(21.5, 0) && !math.IsNaN(21.5) {
        fmt.Println("yes")
    } else {
        fmt.Println("no")
    }
}
Output
yes

A measurement of 21.5 is finite. A missing calculation represented by NaN or an infinite value fails this test.

Advertisements

Plot a freezer sensor reading

A freezer probe reports a numeric temperature below zero. Test whether the reading is a finite number before putting it on the chart.

Given−8 °C
Worked resultPlot

Skip an unavailable light reading

A disconnected light meter uses NaN to mark an unavailable measurement. Test whether the reading is a finite number before putting it on the chart.

GivenNaN
Worked resultSkip

What to watch for

A finite reading can still be implausible. Apply physical range checks separately.

Documentation for math.IsInf and IsNaN