Python の組み込み関数 float の使い方です。
float 関数で、『数値、文字列、バイト列』を『浮動小数点数 (float)』に変換します。
それらのコード例と、実行結果を載せました。
あと、『エラーになったコード例』も書きました。
エラーになったコード例
float 関数で、エラーになったコード例です。
float(None) # None
TypeError: float() argument must be a string or a number, not 'NoneType'
float(complex(3, 4)) # (3+4j)
TypeError: can't convert complex to float
float(()) # () 空のタプル
TypeError: float() argument must be a string or a number, not 'tuple'
float([]) # [] 空のリスト
TypeError: float() argument must be a string or a number, not 'list'
float(set()) # set() 空の集合
TypeError: float() argument must be a string or a number, not 'set'
float({}) # {} 空の辞書
TypeError: float() argument must be a string or a number, not 'dict'
float(lambda x: x) # <function <lambda> at 0x0000000000000000>
TypeError: float() argument must be a string or a number, not 'function'
float(range(0, 1)) # range(0, 1)
TypeError: float() argument must be a string or a number, not 'range'
import datetime
float(datetime.datetime.max) # datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
TypeError: float() argument must be a string or a number, not 'datetime.datetime'
import datetime
float(datetime.timedelta.max) # datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
TypeError: float() argument must be a string or a number, not 'datetime.timedelta'
引数無しの結果
float 関数を、引数無しで呼び出した結果です。
float() # 引数無し
0.0
組み込み定数を渡した結果
float 関数に、組み込み定数を渡した結果です。
float(False) # False
0.0
float(True) # True
1.0
int を渡した結果
float 関数に、int(整数)を渡した結果です。
int 型の数値の書き方は(整数リテラルは)、普通の 10 進数で書くこともできましたし、接頭辞を付けて、2 進数、8 進数、16 進数の表現で書くこともできました。
(Python) 整数リテラル (Integer literals)
10 進数の場合です。
float(0) # 0
0.0
float(1) # 1
1.0
float(-1) # -1
-1.0
float(15) # 15
15.0
float(-15) # -15
-15.0
2 進数の場合です。
float(0b1111) # 15
15.0
float(-0b1111) # -15
-15.0
8 進数の場合です。
float(0o17) # 15
15.0
float(-0o17) # -15
-15.0
16 進数の場合です。
float(0xf) # 15
15.0
float(-0xf) # -15
-15.0
float を渡した結果
float 関数に、float(浮動小数点数)を渡した結果です。
float 型の数値の書き方は(浮動小数点数リテラルは)、普通に『整数と小数点以下の数』で書くこともできましたし、『指数』も使用して書くこともできました。
もちろん、どの表現でも、そのままで float 型の数値として使うことができました。
なので、浮動小数点数リテラルを改めて float 関数に渡しても、意味はなかったです。
(Python) 浮動小数点数リテラル (Floating point literals)
普通に『整数と小数点以下の数』で書いた場合です。
float(0.0) # 0.0
0.0
float(+0.0) # 0.0
0.0
float(-0.0) # -0.0
-0.0
float(0.1) # 0.1
0.1
float(-0.1) # -0.1
-0.1
float(.123) # 0.123
0.123
float(-.123) # -0.123
-0.123
float(1.0) # 1.0
1.0
float(-1.0) # -1.0
-1.0
float(5.) # 5.0
5.0
float(-5.) # -5.0
-5.0
『指数』も使用して書いた場合です。
float(2.561e2) # 256.1
256.1
float(2.561E2) # 256.1
256.1
float(2.561e+2) # 256.1
256.1
float(-2.561e2) # -256.1
-256.1
float(2.561e-2) # 0.02561
0.02561
float(2.e-2) # 0.02
0.02
float(.561e-2) # 0.00561
0.00561
無限大と非数の場合です。
ところで、浮動小数点数リテラルには、無限大と非数を書く方法が、無いようでした。
では、どうやって取得するのか?
無限大と非数は、文字列 ('inf', '-inf', 'nan'
など) を float 関数に渡したときに、取得することができました。
なので、そうして取得したものを、float 関数に渡してみました。
(Python) 浮動小数点数リテラル (Floating point literals)
float(float('inf')) # inf
inf
float(float('-inf')) # -inf
-inf
float(float('nan')) # nan
nan
Decimal を渡した結果
float 関数に、Decimal 型(十進数型)を渡した結果です。
(Python) class decimal.Decimal(value="0", context=None)
from decimal import Decimal
float(Decimal(0.0)) # Decimal('0')
0.0
from decimal import Decimal
float(Decimal(-0.0)) # Decimal('-0')
-0.0
from decimal import Decimal
float(Decimal(0.1)) # Decimal('0.1000000000000000055511151231257827021181583404541015625')
0.1
from decimal import Decimal
float(Decimal(-0.1)) # Decimal('-0.1000000000000000055511151231257827021181583404541015625')
-0.1
from decimal import Decimal
float(Decimal(1)) # Decimal('1')
1.0
from decimal import Decimal
float(Decimal(-1)) # Decimal('-1')
-1.0
from decimal import Decimal
float(Decimal(2.561e2)) # Decimal('256.1000000000000227373675443232059478759765625')
256.1
from decimal import Decimal
float(Decimal(-2.561e2)) # Decimal('-256.1000000000000227373675443232059478759765625')
-256.1
from decimal import Decimal
float(Decimal(2.561e-2)) # Decimal('0.025610000000000000819344592173365526832640171051025390625')
0.02561
from decimal import Decimal
float(Decimal('inf')) # Decimal('Infinity')
inf
from decimal import Decimal
float(Decimal('-inf')) # Decimal('-Infinity')
-inf
from decimal import Decimal
float(Decimal('nan')) # Decimal('NaN')
nan
from decimal import Decimal
float(Decimal('snan')) # Decimal('sNaN')
ValueError: cannot convert signaling NaN to float
str を渡した結果
float 関数に、str(文字列)を渡した結果です。
0.0
float 関数の結果が『ゼロ 0.0』になったコード例と、エラー例です。
float('') # '' 空文字列
ValueError: could not convert string to float: ''
float('0') # '0'
0.0
float('0.') # '0.'
0.0
float('0.0') # '0.0'
0.0
float('0000.0000') # '0000.0000'
0.0
float(' 0.0 ') # ' 0.0 '
0.0
float('0 .0') # '0 .0'
ValueError: could not convert string to float: '0 .0'
float('0. 0') # '0. 0'
ValueError: could not convert string to float: '0. 0'
float('+0') # '+0'
0.0
float('+0.') # '+0.'
0.0
float('+0.0') # '+0.0'
0.0
float('+0000.0000') # '+0000.0000'
0.0
float(' +0.0 ') # ' +0.0 '
0.0
float('+ 0.0') # '+ 0.0'
ValueError: could not convert string to float: '+ 0.0'
float('+0 .0') # '+0 .0'
ValueError: could not convert string to float: '+0 .0'
float('+0. 0') # '+0. 0'
ValueError: could not convert string to float: '+0. 0'
-0.0
float 関数の結果が『マイナスのゼロ -0.0』になったコード例と、エラー例です。
float('-0') # '-0'
-0.0
float('-0.') # '-0.'
-0.0
float('-0.0') # '-0.0'
-0.0
float('-0000.0000') # '-0000.0000'
-0.0
float(' -0.0 ') # ' -0.0 '
-0.0
float('- 0.0') # '- 0.0'
ValueError: could not convert string to float: '- 0.0'
float('-0 .0') # '-0 .0'
ValueError: could not convert string to float: '-0 .0'
float('-0. 0') # '-0. 0'
ValueError: could not convert string to float: '-0. 0'
-0.5
float 関数の結果が『-0.5』になったコード例と、エラー例です。
float('-0.5') # '-0.5'
-0.5
float('-.5') # '-.5'
-0.5
float('-.50') # '-.50'
-0.5
float('-0000.5000') # '-0000.5000'
-0.5
float('-50.0e-2') # '-50.0e-2'
-0.5
float('-5.0e-1') # '-5.0e-1'
-0.5
float('-0.5e0') # '-0.5e0'
-0.5
float('-0.5e+0') # '-0.5e+0'
-0.5
float('-.5e+0') # '-.5e+0'
-0.5
float('-0.5e+0.') # '-0.5e+0.'
ValueError: could not convert string to float: '-0.5e+0.'
float('-0.5e+0.0') # '-0.5e+0.0'
ValueError: could not convert string to float: '-0.5e+0.0'
float('-0.05e1') # '-0.05e1'
-0.5
float('-0.005e2') # '-0.005e2'
-0.5
float('-0.005 e2') # '-0.005 e2'
ValueError: could not convert string to float: '-0.005 e2'
float('-0.005e 2') # '-0.005e 2'
ValueError: could not convert string to float: '-0.005e 2'
float('-0000.00500e+0002') # '-0000.00500e+0002'
-0.5
無限大 (inf, -inf)
float 関数の結果が『正の無限大 (inf)』になったコード例と、『負の無限大 (-inf)』になったコード例です。
float('inf') # 'inf'
inf
float('infinity') # 'infinity'
inf
float('-inf') # '-inf'
-inf
float('-infinity') # '-infinity'
-inf
非数 (nan)
float 関数の結果が『非数 (nan)』になったコード例です。
float('nan') # 'nan'
nan
float('+nan') # '+nan'
nan
float('-nan') # '-nan'
nan
bytes を渡した結果
float 関数に、bytes(バイト列)を渡した結果です。
(Python) class bytes([source[, encoding[, errors]]])
float(b'') # b'' 空のバイト列
ValueError: could not convert string to float: ''
float(b'-0.5') # b'-0.5'
-0.5
float(b'-0.05e1') # b'-0.05e1'
-0.5
float(b'\x2d\x30\x2e\x30\x35\x65\x31') # b'-0.05e1'
-0.5
float(bytes((45, 48, 46, 48, 53, 101, 49))) # b'-0.05e1'
-0.5
bytearray を渡した結果
float 関数に、bytearray(バイト列)を渡した結果です。
(Python) class bytearray([source[, encoding[, errors]]])
float(bytearray()) # bytearray(b'')
ValueError: could not convert string to float: ''
float(bytearray(b'-0.5')) # bytearray(b'-0.5')
-0.5
float(bytearray(b'-0.05e1')) # bytearray(b'-0.05e1')
-0.5
float(bytearray(b'\x2d\x30\x2e\x30\x35\x65\x31')) # bytearray(b'-0.05e1')
-0.5
float(bytearray((45, 48, 46, 48, 53, 101, 49))) # bytearray(b'-0.05e1')
-0.5
以上です。