strtodのヘルプ・マニュアル
日本語 英語
strtod --help
man strtod
STRTOD(3) Linux Programmer’s Manual STRTOD(3)
名前
strtod, strtof, strtold - ASCII 文字列を浮動小数点実数に変換する
書式
#include
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
strtof(), strtold(): _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or
cc -std=c99
説明
strtod(), strtof(), strtold() 関数は、 nptr が指し示す文字列のはじめ の
部分を double, float, long double 型の値にそれぞれ変換する。
入 力する文字列 (の先頭部分) は以下の形式が期待されている。先頭にホワイ
トスペース、次にプラス ('+') またはマイナス ('-') の記号、その後に (i)
10 進 数、(ii) 16 進数、(iii) 無限、 (iv) NAN (計算できない数、not-a-
number) のいずれかがある (ホワイトスペース、符号は省略可能。ホワイト ス
ペースは isspace(3) で識別される)。
10 進 数 は 1 文字以上の 10 進の数字の列からなり、基を表す文字 (radix
charater) (小数点。ロケールに依存するが、通常は '.') が含まれることもあ
る 。 この後に 10 進の指数部が続いても良い。 10 進の指数部は 'E' または
'e' と、その後に置かれる正負記号 (省略可)、およびその後に続く 1 文字 以
上の 10 進の数字の列からなり、 10 の何乗であるかを表す。
16 進数は、"0x" または "0X" とその後に続く 1 文字以上の 16 進の数字の列
からなり、基を表す文字が含まれることもある。この後に 2 進の指数部が続い
ても良い。 2 進の指数部は 'P' または 'p' と、その後に置かれる正負記号 (
省略可)、およびその後に続く 1 文字以上の 10 進の数字の列から構成され 、
2 の何乗であるかを表す。基を表す文字と 2 進の指数部は、どちらか一方しか
存在してはならない。
無限は "INF" または "INFINITY" で表され、大文字小文字は区別されない。
NAN は "NAN" (大文字小文字は区別されない) で表され、その後に '(' 文字列
')' が続く場合もある。この文字列は実装に依存する NAN を指定する。
返り値
これらの関数は、変換された値があれば、それを返す。
endptr が NULL でないときは、変換に使われた最終文字の次の文字へのポイン
ターが endptr で参照される場所へ保存される。
変換が行われなかったときには 0 が返る。そして endptr が参照している場所
に nptr の値 (変換対象である文字列の開始アドレス) が保存される。
正 しい形式の数値文字列であるが、変換結果がオーバーフローを起こした場合
には、プラスまたはマイナスの HUGE_VAL (HUGE_VALF, HUGE_VALL) が返り (値
の 符号による)、 ERANGE が errno に代入される。変換結果がアンダーフロー
を起こした場合には 0 が返り、 ERANGE が errno に代入される。
エラー
ERANGE オーバーフローまたはアンダーフローが起こった。
準拠
C89 では strtod() が、C99 では残りの 2 つの関数が記述されている。
注意
成功、失敗どちらの場合でも 0 を返す可能性があるので、プログラムは呼び出
す 前に errno を 0 に設定し、呼び出し後に errno が 0 以外の値かどうかを
確認しエラーが発生したかどうかを判断する必要がある。
例
strtol(3) のマニュアルページの例を参照。このページで説明した関数の使 用
方法も同様である。
関連項目
atof(3), atoi(3), atol(3), strtol(3), strtoul(3)
Linux 2007-07-26 STRTOD(3)
STRTOD(3) Linux Programmer’s Manual STRTOD(3)
NAME
strtod, strtof, strtold - convert ASCII string to floating-point number
SYNOPSIS
#include
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strtof(), strtold(): _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or
cc -std=c99
DESCRIPTION
The strtod(), strtof(), and strtold() functions convert the initial
portion of the string pointed to by nptr to double, float, and long
double representation, respectively.
The expected form of the (initial portion of the) string is optional
leading white space as recognized by isspace(3), an optional plus ('+')
or minus sign ('-') and then either (i) a decimal number, or (ii) a
hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-number).
A decimal number consists of a non-empty sequence of decimal digits
possibly containing a radix character (decimal point, locale-dependent,
usually '.'), optionally followed by a decimal exponent. A decimal
exponent consists of an 'E' or 'e', followed by an optional plus or
minus sign, followed by a non-empty sequence of decimal digits, and
indicates multiplication by a power of 10.
A hexadecimal number consists of a "0x" or "0X" followed by a non-empty
sequence of hexadecimal digits possibly containing a radix character,
optionally followed by a binary exponent. A binary exponent consists
of a 'P' or 'p', followed by an optional plus or minus sign, followed
by a non-empty sequence of decimal digits, and indicates multiplication
by a power of 2. At least one of radix character and binary exponent
must be present.
An infinity is either "INF" or "INFINITY", disregarding case.
A NAN is "NAN" (disregarding case) optionally followed by '(', a
sequence of characters, followed by ')'. The character string speci-
fies in an implementation-dependent way the type of NAN.
RETURN VALUE
These functions return the converted value, if any.
If endptr is not NULL, a pointer to the character after the last char-
acter used in the conversion is stored in the location referenced by
endptr.
If no conversion is performed, zero is returned and the value of nptr
is stored in the location referenced by endptr.
If the correct value would cause overflow, plus or minus HUGE_VAL
(HUGE_VALF, HUGE_VALL) is returned (according to the sign of the
value), and ERANGE is stored in errno. If the correct value would
cause underflow, zero is returned and ERANGE is stored in errno.
ERRORS
ERANGE Overflow or underflow occurred.
CONFORMING TO
C89 describes strtod(), C99 describes the other two functions.
NOTES
Since 0 can legitimately be returned on both success and failure, the
calling program should set errno to 0 before the call, and then deter-
mine if an error occurred by checking whether errno has a non-zero
value after the call.
EXAMPLE
See the example on the strtol(3) manual page; the use of the functions
described in this manual page is similar.
SEE ALSO
atof(3), atoi(3), atol(3), strtol(3), strtoul(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2007-07-26 STRTOD(3)