math

package
v1.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

mathパッケージは基本的な定数と数学的な関数を提供します。

このパッケージはアーキテクチャ間でビットが完全に同じ結果になることを保証しません。

Index

Examples

Constants

View Source
const (
	E   = 2.71828182845904523536028747135266249775724709369995957496696763
	Pi  = 3.14159265358979323846264338327950288419716939937510582097494459
	Phi = 1.61803398874989484820458683436563811772030917980576286213544862

	Sqrt2   = 1.41421356237309504880168872420969807856967187537694807317667974
	SqrtE   = 1.64872127070012814684865078781416357165377610071014801157507931
	SqrtPi  = 1.77245385090551602729816748334114518279754945612238712821380779
	SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038

	Ln2    = 0.693147180559945309417232121458176568075500134360255254120680009
	Log2E  = 1 / Ln2
	Ln10   = 2.30258509299404568401799145468436420760110148862877297603332790
	Log10E = 1 / Ln10
)

数学定数。

View Source
const (
	MaxFloat32             = 0x1p127 * (1 + (1 - 0x1p-23))
	SmallestNonzeroFloat32 = 0x1p-126 * 0x1p-23

	MaxFloat64             = 0x1p1023 * (1 + (1 - 0x1p-52))
	SmallestNonzeroFloat64 = 0x1p-1022 * 0x1p-52
)

浮動小数点の制限値。 Max はその型で表現可能な最大の有限値です。 SmallestNonzero はその型で表現可能な最小の正の0以外の値です。

View Source
const (
	MaxInt    = 1<<(intSize-1) - 1
	MinInt    = -1 << (intSize - 1)
	MaxInt8   = 1<<7 - 1
	MinInt8   = -1 << 7
	MaxInt16  = 1<<15 - 1
	MinInt16  = -1 << 15
	MaxInt32  = 1<<31 - 1
	MinInt32  = -1 << 31
	MaxInt64  = 1<<63 - 1
	MinInt64  = -1 << 63
	MaxUint   = 1<<intSize - 1
	MaxUint8  = 1<<8 - 1
	MaxUint16 = 1<<16 - 1
	MaxUint32 = 1<<32 - 1
	MaxUint64 = 1<<64 - 1
)

整数の上限値。

Variables

This section is empty.

Functions

func Abs

func Abs(x float64) float64

Absはxの絶対値を返します。

特殊な場合は以下のようになります:

Abs(±Inf) = +Inf
Abs(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	x := math.Abs(-2)
	fmt.Printf("%.1f\n", x)

	y := math.Abs(2)
	fmt.Printf("%.1f\n", y)
}
Output:

2.0
2.0

func Acos

func Acos(x float64) float64

Acosは、xのarccosine(ラジアン単位)を返します。

特殊な場合は以下です:

Acos(x) = NaN if x < -1 or x > 1
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Acos(1))
}
Output:

0.00

func Acosh

func Acosh(x float64) float64

Acoshはxの逆双曲線余弦を返します。

特殊な場合は以下の通りです:

Acosh(+Inf) = +Inf
Acosh(x) = NaN if x < 1
Acosh(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Acosh(1))
}
Output:

0.00

func Asin

func Asin(x float64) float64

Asinは、xの弧度法におけるアークサインを返します。

特別なケースは次のとおりです:

Asin(±0) = ±0
Asin(x) = NaN if x < -1 or x > 1
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Asin(0))
}
Output:

0.00

func Asinh

func Asinh(x float64) float64

Asinhはxの逆双曲線正弦を返します。

特殊なケースは:

Asinh(±0) = ±0
Asinh(±Inf) = ±Inf
Asinh(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Asinh(0))
}
Output:

0.00

func Atan

func Atan(x float64) float64

Atanはxのアークタンジェント(ラジアン)を返します。

特殊なケースは以下の通りです:

Atan(±0) = ±0
Atan(±Inf) = ±Pi/2
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Atan(0))
}
Output:

0.00

func Atan2

func Atan2(y, x float64) float64

Atan2は、y/xの逆正接を返します。戻り値の象限を決定するために、二つの符号を使用します。

特殊なケースは以下の通りです:

Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +Pi
Atan2(-0, x<=-0) = -Pi
Atan2(y>0, 0) = +Pi/2
Atan2(y<0, 0) = -Pi/2
Atan2(+Inf, +Inf) = +Pi/4
Atan2(-Inf, +Inf) = -Pi/4
Atan2(+Inf, -Inf) = 3Pi/4
Atan2(-Inf, -Inf) = -3Pi/4
Atan2(y, +Inf) = 0
Atan2(y>0, -Inf) = +Pi
Atan2(y<0, -Inf) = -Pi
Atan2(+Inf, x) = +Pi/2
Atan2(-Inf, x) = -Pi/2
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Atan2(0, 0))
}
Output:

0.00

func Atanh

func Atanh(x float64) float64

Atanhはxの逆双曲線正接を返します。

特別な場合は次のとおりです:

Atanh(1) = +Inf
Atanh(±0) = ±0
Atanh(-1) = -Inf
Atanh(x) = NaN if x < -1 or x > 1
Atanh(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Atanh(0))
}
Output:

0.00

func Cbrt

func Cbrt(x float64) float64

Cbrtはxの立方根を返します。

特殊なケースは次の通りです:

Cbrt(±0) = ±0
Cbrt(±Inf) = ±Inf
Cbrt(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f\n", math.Cbrt(8))
	fmt.Printf("%.2f\n", math.Cbrt(27))
}
Output:

2.00
3.00

func Ceil

func Ceil(x float64) float64

Ceil(天井)は、x以上の最小の整数値を返します。

特別なケースは:

Ceil(±0) = ±0
Ceil(±Inf) = ±Inf
Ceil(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	c := math.Ceil(1.49)
	fmt.Printf("%.1f", c)
}
Output:

2.0

func Copysign

func Copysign(f, sign float64) float64

Copysign は f の絶対値と sign の符号を持つ値を返す。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Copysign(3.2, -1))
}
Output:

-3.20

func Cos

func Cos(x float64) float64

Cosは、ラジアンの引数xの余弦を返します。

特殊な場合は:

Cos(±Inf) = NaN
Cos(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Cos(math.Pi/2))
}
Output:

0.00

func Cosh

func Cosh(x float64) float64

Coshはxの双曲線余弦を返します。

特殊なケースは以下の通りです:

Cosh(±0) = 1
Cosh(±Inf) = +Inf
Cosh(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Cosh(0))
}
Output:

1.00

func Dim

func Dim(x, y float64) float64

Dim関数は、x-yまたは0のうちの大きい方を返します。

特別なケースは以下の通りです:

Dim(+Inf, +Inf) = NaN
Dim(-Inf, -Inf) = NaN
Dim(x, NaN) = Dim(NaN, x) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f\n", math.Dim(4, -2))
	fmt.Printf("%.2f\n", math.Dim(-4, 2))
}
Output:

6.00
0.00

func Erf

func Erf(x float64) float64

Erfはxの誤差関数を返します。

特殊なケースは以下の通りです:

Erf(+Inf) = 1
Erf(-Inf) = -1
Erf(NaN) = NaN

func Erfc

func Erfc(x float64) float64

Erfcはxの補完誤差関数を返します。

特別なケースは以下の通りです:

Erfc(+Inf) = 0
Erfc(-Inf) = 2
Erfc(NaN) = NaN

func Erfcinv added in v1.10.0

func Erfcinv(x float64) float64

Erfcinvは Erfc(x)の逆関数を返します。

特殊な場合は以下の通りです:

Erfcinv(0) = +Inf
Erfcinv(2) = -Inf
Erfcinv(x) = NaN if x < 0 or x > 2
Erfcinv(NaN) = NaN

func Erfinv added in v1.10.0

func Erfinv(x float64) float64

Erfinvはxの逆誤差関数を返します。

特殊なケースは以下の通りです:

Erfinv(1) = +Inf
Erfinv(-1) = -Inf
Erfinv(x) = NaN if x < -1 or x > 1
Erfinv(NaN) = NaN

func Exp

func Exp(x float64) float64

Expはe**x、つまりxの自然対数の底eによる指数関数です。

特殊な場合は次の通りです:

Exp(+Inf) = +Inf
Exp(NaN) = NaN

非常に大きな値は0または+Infにオーバーフローします。 非常に小さい値は1にアンダーフローします。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f\n", math.Exp(1))
	fmt.Printf("%.2f\n", math.Exp(2))
	fmt.Printf("%.2f\n", math.Exp(-1))
}
Output:

2.72
7.39
0.37

func Exp2

func Exp2(x float64) float64

Exp2はxの2の累乗(2**x)を返します。

特殊なケースは Exp と同じです。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f\n", math.Exp2(1))
	fmt.Printf("%.2f\n", math.Exp2(-3))
}
Output:

2.00
0.12

func Expm1

func Expm1(x float64) float64

Expm1はxの自然対数e**x - 1を返します。 xがほぼゼロの場合、 Exp(x) - 1よりも精度が高くなります。

特殊なケースは以下の通りです:

Expm1(+Inf) = +Inf
Expm1(-Inf) = -1
Expm1(NaN) = NaN

非常に大きな値は-1または+Infにオーバーフローします。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.6f\n", math.Expm1(0.01))
	fmt.Printf("%.6f\n", math.Expm1(-1))
}
Output:

0.010050
-0.632121

func FMA added in v1.14.0

func FMA(x, y, z float64) float64

FMAは、一度の丸めのみを使用して計算されたx * y + zを返します。 (つまり、FMAはx、y、およびzの融合乗算-加算結果を返します。)

func Float32bits

func Float32bits(f float32) uint32

Float32bitsは、fのIEEE 754バイナリ表現を返します。 fの符号ビットと結果は同じビット位置にあります。 Float32bits(Float32frombits(x)) == x.

func Float32frombits

func Float32frombits(b uint32) float32

Float32frombitsは、符号ビットの位置が同じであるように IEEE 754バイナリ表現bに対応する浮動小数点数を返します。 Float32frombits(Float32bits(x))== x。

func Float64bits

func Float64bits(f float64) uint64

Float64bitsは、fのIEEE 754バイナリ表現を返します。 fの符号ビットと結果が同じビット位置になります。 また、Float64bits(Float64frombits(x)) == x となります。

func Float64frombits

func Float64frombits(b uint64) float64

Float64frombitsは、IEEE 754のバイナリ表現bに対応する浮動小数点数を返します。bの符号ビットと結果は同じビット位置にあります。 Float64frombits(Float64bits(x)) == x.

func Floor

func Floor(x float64) float64

Floorはx以下の最大の整数値を返します。

特殊なケースは次の通りです:

Floor(±0) = ±0
Floor(±Inf) = ±Inf
Floor(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	c := math.Floor(1.51)
	fmt.Printf("%.1f", c)
}
Output:

1.0

func Frexp

func Frexp(f float64) (frac float64, exp int)

Frexpは、fを正規化された分数と2の整数冪に分解します。 それは、f == frac × 2**expを満たすfracとexpを返します。 fracの絶対値は[½, 1)の範囲にあります。

特殊な場合は以下の通りです:

Frexp(±0) = ±0, 0
Frexp(±Inf) = ±Inf, 0
Frexp(NaN) = NaN, 0

func Gamma

func Gamma(x float64) float64

Gammaはxのガンマ関数を返します。

特殊な場合は以下の通りです:

Gamma(+Inf) = +Inf
Gamma(+0) = +Inf
Gamma(-0) = -Inf
Gamma(x) = NaN for integer x < 0
Gamma(-Inf) = NaN
Gamma(NaN) = NaN

func Hypot

func Hypot(p, q float64) float64

Hypotは Sqrt(p*p + q*q) を返します。不必要なオーバーフローやアンダーフローを避けるように注意します。

特殊な場合は以下の通りです:

Hypot(±Inf, q) = +Inf
Hypot(p, ±Inf) = +Inf
Hypot(NaN, q) = NaN
Hypot(p, NaN) = NaN

func Ilogb

func Ilogb(x float64) int

Ilogbはxのバイナリ指数を整数として返します。

特殊なケースは以下の通りです:

Ilogb(±Inf) = MaxInt32
Ilogb(0) = MinInt32
Ilogb(NaN) = MaxInt32

func Inf

func Inf(sign int) float64

signが0以上の場合、正の無限大を返し、signが0より小さい場合は負の無限大を返します。

func IsInf

func IsInf(f float64, sign int) bool

IsInfは、fが無限大であるかどうかをsignに基づいて報告します。 sign > 0の場合、fが正の無限大であるかどうかを報告します。 sign < 0の場合、fが負の無限大であるかどうかを報告します。 sign == 0の場合、fがどちらかの無限大であるかどうかを報告します。

func IsNaN

func IsNaN(f float64) (is bool)

IsNaNは、fがIEEE 754の"非数"値であるかどうかを報告します。

func J0

func J0(x float64) float64

J0は第1種のゼロ次ベッセル関数を返します。

特殊な場合は次の通りです:

J0(±Inf) = 0
J0(0) = 1
J0(NaN) = NaN

func J1

func J1(x float64) float64

J1は第1種の1次ベッセル関数を返します。

特殊なケースは以下の通りです:

J1(±Inf) = 0
J1(NaN) = NaN

func Jn

func Jn(n int, x float64) float64

Jnは、第一種ベッセル関数のn次の順序を返します。

特殊なケースは次の通りです:

Jn(n, ±Inf) = 0
Jn(n, NaN) = NaN

func Ldexp

func Ldexp(frac float64, exp int) float64

Ldexpは Frexp の逆です。 それはfrac × 2 ** expを返します。

特別なケースは以下の通りです:

Ldexp(±0, exp) = ±0
Ldexp(±Inf, exp) = ±Inf
Ldexp(NaN, exp) = NaN

func Lgamma

func Lgamma(x float64) (lgamma float64, sign int)

Lgammaは Gamma(x)の自然対数と符号(-1または+1)を返します。

特殊なケースは以下の通りです:

Lgamma(+Inf) = +Inf
Lgamma(0) = +Inf
Lgamma(-integer) = +Inf
Lgamma(-Inf) = -Inf
Lgamma(NaN) = NaN

func Log

func Log(x float64) float64

Logはxの自然対数を返します。

特別なケースは以下の通りです:

Log(+Inf) = +Inf
Log(0) = -Inf
Log(x < 0) = NaN
Log(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	x := math.Log(1)
	fmt.Printf("%.1f\n", x)

	y := math.Log(2.7183)
	fmt.Printf("%.1f\n", y)
}
Output:

0.0
1.0

func Log10

func Log10(x float64) float64

Log10 は x の 10 を底とする対数(常用対数)を返します。 特殊な場合は Log と同じです。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.1f", math.Log10(100))
}
Output:

2.0

func Log1p

func Log1p(x float64) float64

Log1pは引数xの1を加えたものの自然対数を返します。 xがゼロに近い場合、 Log(1 + x)よりも正確です。

特殊な場合は次のとおりです:

Log1p(+Inf) = +Inf
Log1p(±0) = ±0
Log1p(-1) = -Inf
Log1p(x < -1) = NaN
Log1p(NaN) = NaN

func Log2

func Log2(x float64) float64

Log2 は x の二進対数を返します。 特殊な場合は Log と同じです。

Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.1f", math.Log2(256))
}
Output:

8.0

func Logb

func Logb(x float64) float64

Logbはxの二進指数を返します。

特殊なケースは次の通りです:

Logb(±Inf) = +Inf
Logb(0) = -Inf
Logb(NaN) = NaN

func Max

func Max(x, y float64) float64

Maxはxとyのうち大きい方を返します。

特殊なケースは以下の通りです:

Max(x, +Inf) = Max(+Inf, x) = +Inf
Max(x, NaN) = Max(NaN, x) = NaN
Max(+0, ±0) = Max(±0, +0) = +0
Max(-0, -0) = -0

なお、これはNaNと+Infの場合に組み込み関数maxとは異なります。

func Min

func Min(x, y float64) float64

Min関数は、xとyのうち小さい方を返します。

特殊な場合は以下の通りです:

Min(x, -Inf) = Min(-Inf, x) = -Inf
Min(x, NaN) = Min(NaN, x) = NaN
Min(-0, ±0) = Min(±0, -0) = -0

注意:これは、NaNと-Infを引数にして呼び出すと、組み込みのmin関数とは異なる結果になります。

func Mod

func Mod(x, y float64) float64

Modはx/yの浮動小数点の余りを返します。 結果の絶対値はyより小さく、符号はxと一致します。

特殊なケースは次のとおりです:

Mod(±Inf, y) = NaN
Mod(NaN, y) = NaN
Mod(x, 0) = NaN
Mod(x, ±Inf) = x
Mod(x, NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	c := math.Mod(7, 4)
	fmt.Printf("%.1f", c)
}
Output:

3.0

func Modf

func Modf(f float64) (int float64, frac float64)

Modfは整数部分と小数部分の浮動小数点数を返します。両方の値はfと同じ符号を持ちます。

特殊なケースは以下の通りです:

Modf(±Inf) = ±Inf, NaN
Modf(NaN) = NaN, NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	int, frac := math.Modf(3.14)
	fmt.Printf("%.2f, %.2f\n", int, frac)

	int, frac = math.Modf(-2.71)
	fmt.Printf("%.2f, %.2f\n", int, frac)
}
Output:

3.00, 0.14
-2.00, -0.71

func NaN

func NaN() float64

NaNはIEEE 754の「非数値」を返します。

func Nextafter

func Nextafter(x, y float64) (r float64)

Nextafterはxからyに向かって次の表現可能なfloat64値を返します。

特別な場合は以下の通りです:

Nextafter(x, x)   = x
Nextafter(NaN, y) = NaN
Nextafter(x, NaN) = NaN

func Nextafter32 added in v1.4.0

func Nextafter32(x, y float32) (r float32)

Nextafter32はxからyの方向に次に表現可能なfloat32値を返します。

特殊なケースは以下の通りです:

Nextafter32(x, x)   = x
Nextafter32(NaN, y) = NaN
Nextafter32(x, NaN) = NaN

func Pow

func Pow(x, y float64) float64

Powはx**y、yのxを底とした指数を返します。

特殊な場合は以下の通りです:

Pow(x, ±0) = 1 for any x
Pow(1, y) = 1 for any y
Pow(x, 1) = x for any x
Pow(NaN, y) = NaN
Pow(x, NaN) = NaN
Pow(±0, y) = ±Inf for y an odd integer < 0
Pow(±0, -Inf) = +Inf
Pow(±0, +Inf) = +0
Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
Pow(±0, y) = ±0 for y an odd integer > 0
Pow(±0, y) = +0 for finite y > 0 and not an odd integer
Pow(-1, ±Inf) = 1
Pow(x, +Inf) = +Inf for |x| > 1
Pow(x, -Inf) = +0 for |x| > 1
Pow(x, +Inf) = +0 for |x| < 1
Pow(x, -Inf) = +Inf for |x| < 1
Pow(+Inf, y) = +Inf for y > 0
Pow(+Inf, y) = +0 for y < 0
Pow(-Inf, y) = Pow(-0, -y)
Pow(x, y) = NaN for finite x < 0 and finite non-integer y
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	c := math.Pow(2, 3)
	fmt.Printf("%.1f", c)
}
Output:

8.0

func Pow10

func Pow10(n int) float64

Pow10はnの10のn乗、つまり10の指数nを返します。

特別な場合は以下の通りです:

Pow10(n) =    0 for n < -323
Pow10(n) = +Inf for n > 308
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	c := math.Pow10(2)
	fmt.Printf("%.1f", c)
}
Output:

100.0

func Remainder

func Remainder(x, y float64) float64

Remainderはx/yのIEEE 754浮動小数点余りを返します。

特殊なケースは以下の通りです:

Remainder(±Inf, y) = NaN
Remainder(NaN, y) = NaN
Remainder(x, 0) = NaN
Remainder(x, ±Inf) = x
Remainder(x, NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.1f", math.Remainder(100, 30))
}
Output:

10.0

func Round added in v1.10.0

func Round(x float64) float64

Roundは、最も近い整数を返します。半の場合はゼロから離れます。

特殊なケースは次の通りです:

Round(±0) = ±0
Round(±Inf) = ±Inf
Round(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	p := math.Round(10.5)
	fmt.Printf("%.1f\n", p)

	n := math.Round(-10.5)
	fmt.Printf("%.1f\n", n)
}
Output:

11.0
-11.0

func RoundToEven added in v1.10.0

func RoundToEven(x float64) float64

RoundToEvenは、最も近い整数を返し、丸めの際に引き分けを偶数に丸めます。

特別なケースは以下の通りです:

RoundToEven(±0) = ±0
RoundToEven(±Inf) = ±Inf
RoundToEven(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	u := math.RoundToEven(11.5)
	fmt.Printf("%.1f\n", u)

	d := math.RoundToEven(12.5)
	fmt.Printf("%.1f\n", d)
}
Output:

12.0
12.0

func Signbit

func Signbit(x float64) bool

Signbitはxが負または負のゼロかどうかを報告します。

func Sin

func Sin(x float64) float64

Sinは、ラジアンの引数xの正弦を返します。

特殊なケースは次の通りです:

Sin(±0) = ±0
Sin(±Inf) = NaN
Sin(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Sin(math.Pi))
}
Output:

0.00

func Sincos

func Sincos(x float64) (sin, cos float64)

SincosはSin(x), Cos(x)を返します。

特殊なケースは以下の通りです:

Sincos(±0) = ±0, 1
Sincos(±Inf) = NaN, NaN
Sincos(NaN) = NaN, NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	sin, cos := math.Sincos(0)
	fmt.Printf("%.2f, %.2f", sin, cos)
}
Output:

0.00, 1.00

func Sinh

func Sinh(x float64) float64

Sinhはxの双曲線正弦を返します。

特殊なケースは以下の通りです:

Sinh(±0) = ±0
Sinh(±Inf) = ±Inf
Sinh(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Sinh(0))
}
Output:

0.00

func Sqrt

func Sqrt(x float64) float64

Sqrtはxの平方根を返します。

特殊な場合は次の通りです:

Sqrt(+Inf) = +Inf
Sqrt(±0) = ±0
Sqrt(x < 0) = NaN
Sqrt(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	const (
		a = 3
		b = 4
	)
	c := math.Sqrt(a*a + b*b)
	fmt.Printf("%.1f", c)
}
Output:

5.0

func Tan

func Tan(x float64) float64

Tanはラジアンの引数xの正接(タンジェント)を返します。

特殊なケースは以下の通りです:

Tan(±0) = ±0
Tan(±Inf) = NaN
Tan(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Tan(0))
}
Output:

0.00

func Tanh

func Tanh(x float64) float64

Tanhはxの双曲線正接を返します。

特殊な場合は次のとおりです:

Tanh(±0) = ±0
Tanh(±Inf) = ±1
Tanh(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f", math.Tanh(0))
}
Output:

0.00

func Trunc

func Trunc(x float64) float64

Truncはxの整数値を返します。

特殊なケースは以下の通りです:

Trunc(±0) = ±0
Trunc(±Inf) = ±Inf
Trunc(NaN) = NaN
Example
package main

import (
	"github.com/shogo82148/std/fmt"
	"github.com/shogo82148/std/math"
)

func main() {
	fmt.Printf("%.2f\n", math.Trunc(math.Pi))
	fmt.Printf("%.2f\n", math.Trunc(-1.2345))
}
Output:

3.00
-1.00

func Y0

func Y0(x float64) float64

Y0は第二種ベッセル関数のゼロ次の値を返します。

特殊なケースは以下の通りです:

Y0(+Inf) = 0
Y0(0) = -Inf
Y0(x < 0) = NaN
Y0(NaN) = NaN

func Y1

func Y1(x float64) float64

Y1は第2種ベッセル関数の1次の値を返します。

特殊な場合は以下の通りです:

Y1(+Inf) = 0
Y1(0) = -Inf
Y1(x < 0) = NaN
Y1(NaN) = NaN

func Yn

func Yn(n int, x float64) float64

Ynは第二種ベッセル関数のn番目の順序を返します。

特殊な場合は次の通りです:

Yn(n, +Inf) = 0
Yn(n ≥ 0, 0) = -Inf
Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
Yn(n, x < 0) = NaN
Yn(n, NaN) = NaN

Types

This section is empty.

Directories

Path Synopsis
bigパッケージは任意精度算術(大きな数)を実装します。
bigパッケージは任意精度算術(大きな数)を実装します。
bitsパッケージは、事前に宣言された符号なし整数型のためのビットカウントと操作 関数を実装します。
bitsパッケージは、事前に宣言された符号なし整数型のためのビットカウントと操作 関数を実装します。
cmplxパッケージは、複素数の基本的な定数と数学関数を提供します。
cmplxパッケージは、複素数の基本的な定数と数学関数を提供します。
randパッケージは、シミュレーションなどのタスクに適した擬似乱数生成器を実装しますが、 セキュリティに敏感な作業には使用しないでください。
randパッケージは、シミュレーションなどのタスクに適した擬似乱数生成器を実装しますが、 セキュリティに敏感な作業には使用しないでください。
v2
randパッケージは、シミュレーションなどのタスクに適した擬似乱数生成器を実装しますが、セキュリティに敏感な作業には使用しないでください。
randパッケージは、シミュレーションなどのタスクに適した擬似乱数生成器を実装しますが、セキュリティに敏感な作業には使用しないでください。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL