0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

NaNって何なん?

Posted at

はじめに

いままでの人生であまり、NaNに出会わなかったので、いろいろ知らなかった。

NaN

https://en.wikipedia.org/wiki/NaN
IEEE 754 floating-point standard にNaNあるよって書いてる。

こういうときに、NaNになるっぽい。

Indeterminate forms:
The divisions (±0) / (±0) and (±∞) / (±∞).
The multiplications (±0) × (±∞) and (±∞) × (±0).
Remainder x % y when x is an infinity or y is zero.
The additions (+∞) + (−∞), (−∞) + (+∞) and equivalent subtractions (+∞) − (+∞) and (−∞) − (−∞).
The standard has alternative functions for powers:
The standard pow function and the integer exponent pown function define 00, 1∞, and ∞0 as 1.
The powr function defines all three indeterminate forms as invalid operations and so returns NaN.
Real operations with complex results, for example:
The square root of a negative number.
The logarithm of a negative number.
The inverse sine or inverse cosine of a number that is less than −1 or greater than 1.

Go

https://golang.org/ref/spec#Numeric_types
ここに、float32/float64はIEEE-754だよって書いてある

float32 the set of all IEEE-754 32-bit floating-point numbers

試してみた。

package main

import (
	"fmt"
	"math"
)

func main() {
	var hoge float64 = math.NaN()
	fmt.Println(hoge)
	fmt.Println(math.IsNaN(hoge))
	var fuga float64 = math.Log(-1)
	fmt.Println(fuga)
}
NaN
true
NaN

なるほど。

おわりに

NaNって値があるのにそこそこびっくり。エラーにしちゃわないところが、寛容な感じして微妙な気がするけど、結構便利そう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?