python floatタイプの使用方法

5967 ワード

今日はpythonにおけるfloatの方法を見てみましょう
1:取得値の最も簡略化された結果
def as_integer_ratio(self): # real signatureunknown; restored from __doc__     """    float.as_integer_ratio() -> (int,int)         Return a pair of integers, whoseratio is exactly equal to the original     float and with a positivedenominator.     Raise OverflowError on infinities anda ValueError on NaNs.         >>>(10.0).as_integer_ratio()###ここでは使用方法を示し、メタグループタイプ(10,1)>>>>(0.0)を返します.as_integer_ratio()     (0, 1)     >>>(-.25).as_integer_ratio()     (-1, 4)     """
 
2:複数取り共役
a = 1.1
b = 10.5
c = 2 + 1.3J                      ###           

print("a",a.conjugate())
print("b",b.conjugate())
print("c",c.conjugate())

a 1.1
b 10.5
c (2-1.3j)

 
3:16進数から浮動小数点数へ
def fromhex(self, string): # real signature unknown; restored from __doc__
    """
    float.fromhex(string) -> float
    
    Create a floating-point number from a hexadecimal string.
    >>> float.fromhex('0x1.ffffp10')    ###     
    2047.984375
    >>> float.fromhex('-0x1p-1074')
    -5e-324
    """
    return 0.0

 
4:浮動小数点数を16進数に変換
def hex(self): # real signature unknown; restored from __doc__
    """
    float.hex() -> string
    
    Return a hexadecimal representation of a floating-point number.
    >>> (-0.1).hex()                  ####     
    '-0x1.999999999999ap-4'
    >>> 3.14159.hex()
    '0x1.921f9f01b866ep+1'
    """
    return ""

 
5:整数か否かを判断しbool値を返す
def is_integer(self, *args, **kwargs): # real signature unknown
    """ Return True if the float is an integer. """
    pass

テスト:
a = 1.1
b = 10.5
c = 2 + 1.3J
d = 5.0

print("a",a.is_integer())
print("b",b.is_integer())
print("d",d.is_integer())

########               bool  
a False
b False
d True