Hướng dẫn python get integer part of float - python lấy phần nguyên của float

Tôi đã đưa ra hai câu có thể chia số dương và số tiêu cực thành số nguyên và phân số mà không ảnh hưởng đến độ chính xác (tràn bit) và tốc độ.

Ví dụ, giá trị dương hoặc âm của giá trị 100.1323 sẽ được chia thành: 100.1323 -> (100,

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
0)
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
1 -> (
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
2,
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
3)
100.1323 -> (100,
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
0)
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
1 -> (
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
2,
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
3)

Mã số

# Divide a number (x) into integer and fraction
i = int(x) # Get integer
f = (x*1e17 - i*1e17) / 1e17 # Get fraction

SpeedTest

Bài kiểm tra hiệu suất cho thấy hai câu nói nhanh hơn

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
4, miễn là chúng không được đưa vào chức năng hoặc phương pháp của riêng chúng.

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
5:

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')

Result:

         4 function calls in 1.357 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.357    1.357 :1()
        1    1.357    1.357    1.357    1.357 test.py:11(scenario_a)
        1    0.000    0.000    1.357    1.357 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         4 function calls in 1.858 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.858    1.858 :1()
        1    1.858    1.858    1.858    1.858 test.py:18(scenario_b)
        1    0.000    0.000    1.858    1.858 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 2.744 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.744    2.744 :1()
        1    1.245    1.245    2.744    2.744 test.py:26(scenario_c)
  5000000    1.499    0.000    1.499    0.000 test.py:29(modf)
        1    0.000    0.000    2.744    2.744 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 1.904 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.904    1.904 :1()
        1    1.073    1.073    1.904    1.904 test.py:37(scenario_d)
        1    0.000    0.000    1.904    1.904 {built-in method builtins.exec}
  5000000    0.831    0.000    0.831    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 2.547 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.547    2.547 :1()
        1    1.696    1.696    2.547    2.547 test.py:43(scenario_e)
        1    0.000    0.000    2.547    2.547 {built-in method builtins.exec}
  5000000    0.851    0.000    0.851    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Sử dụng tiện ích mở rộng C/C ++

Tôi đã cố gắng biên dịch hai câu lệnh với hỗ trợ C/C ++ và kết quả thậm chí còn tốt hơn. Với mô -đun mở rộng Python, có thể có được một phương pháp nhanh hơn và chính xác hơn

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
4.

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
7:

def modf(number):
    cdef float num =  number
    cdef int i =  num
    return i, (num*1e17 - i*1e17) / 1e17

Xem những điều cơ bản của Cython

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
5:

#!/usr/bin/env python
import math
import cProfile
import math2

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integers and fractions
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Tests the speed of the statements in a function using C/C++ support """
    for _ in LOOPS:
        i, f = math2.modf(X)  # (-100, -0.1323)


def scenario_b():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')

Result:

         5000004 function calls in 1.629 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.629    1.629 :1()
        1    1.100    1.100    1.629    1.629 test.py:10(scenario_a)
        1    0.000    0.000    1.629    1.629 {built-in method builtins.exec}
  5000000    0.529    0.000    0.529    0.000 {math2.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 1.802 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.802    1.802 :1()
        1    1.010    1.010    1.802    1.802 test.py:16(scenario_b)
        1    0.000    0.000    1.802    1.802 {built-in method builtins.exec}
  5000000    0.791    0.000    0.791    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

GHI CHÚ

Các câu lệnh có thể nhanh hơn với modulo, nhưng modulo không thể được sử dụng để chia số âm thành các phần số nguyên và phân số.

i, f = int(x), x*1e17%1e17/1e17 # Divide a number (x) into integer and fraction

Ví dụ, giá trị dương hoặc âm của giá trị 100.1323 sẽ được chia thành: 100.1323 -> (100,

#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
0)
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
1 -> (
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
2,
         4 function calls in 1.357 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.357    1.357 :1()
        1    1.357    1.357    1.357    1.357 test.py:11(scenario_a)
        1    0.000    0.000    1.357    1.357 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         4 function calls in 1.858 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.858    1.858 :1()
        1    1.858    1.858    1.858    1.858 test.py:18(scenario_b)
        1    0.000    0.000    1.858    1.858 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 2.744 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.744    2.744 :1()
        1    1.245    1.245    2.744    2.744 test.py:26(scenario_c)
  5000000    1.499    0.000    1.499    0.000 test.py:29(modf)
        1    0.000    0.000    2.744    2.744 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 1.904 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.904    1.904 :1()
        1    1.073    1.073    1.904    1.904 test.py:37(scenario_d)
        1    0.000    0.000    1.904    1.904 {built-in method builtins.exec}
  5000000    0.831    0.000    0.831    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 2.547 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.547    2.547 :1()
        1    1.696    1.696    2.547    2.547 test.py:43(scenario_e)
        1    0.000    0.000    2.547    2.547 {built-in method builtins.exec}
  5000000    0.851    0.000    0.851    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
5)
100.1323 -> (100,
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
0)
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
1 -> (
#!/usr/bin/env python
import math
import cProfile

""" Get the performance of both statements and math.modf """

X = -100.1323  # The number to be divided into integer and fraction
LOOPS = range(5 * 10 ** 6)  # Number of loops


def scenario_a():
    """ Get the performance of the statements """
    for _ in LOOPS:
        i = int(X)  # -100
        f = (X*1e17-i*1e17)/1e17  # -0.1323


def scenario_b():
    """ Tests the speed of the statements when integer need to be float.
        NOTE: The only difference between this and math.modf is the accuracy """
    for _ in LOOPS:
        i = int(X)  # -100
        i, f = float(i), (X*1e17-i*1e17)/1e17  # (-100.0, -0.1323)


def scenario_c():
    """ Tests the speed of the statements in a function """
    def modf(x):
        i = int(x)
        return i, (x*1e17-i*1e17)/1e17

    for _ in LOOPS:
        i, f = modf(X)  # (-100, -0.1323)


def scenario_d():
    """ Tests the speed of math.modf """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)


def scenario_e():
    """ Tests the speed of math.modf when the integer part should be integer """
    for _ in LOOPS:
        f, i = math.modf(X)  # (-0.13230000000000075, -100.0)
        i = int(i)  # -100


if __name__ == '__main__':
    cProfile.run('scenario_a()')
    cProfile.run('scenario_b()')
    cProfile.run('scenario_c()')
    cProfile.run('scenario_d()')
    cProfile.run('scenario_e()')
2,
         4 function calls in 1.357 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.357    1.357 :1()
        1    1.357    1.357    1.357    1.357 test.py:11(scenario_a)
        1    0.000    0.000    1.357    1.357 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         4 function calls in 1.858 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.858    1.858 :1()
        1    1.858    1.858    1.858    1.858 test.py:18(scenario_b)
        1    0.000    0.000    1.858    1.858 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 2.744 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.744    2.744 :1()
        1    1.245    1.245    2.744    2.744 test.py:26(scenario_c)
  5000000    1.499    0.000    1.499    0.000 test.py:29(modf)
        1    0.000    0.000    2.744    2.744 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 1.904 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.904    1.904 :1()
        1    1.073    1.073    1.904    1.904 test.py:37(scenario_d)
        1    0.000    0.000    1.904    1.904 {built-in method builtins.exec}
  5000000    0.831    0.000    0.831    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         5000004 function calls in 2.547 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.547    2.547 :1()
        1    1.696    1.696    2.547    2.547 test.py:43(scenario_e)
        1    0.000    0.000    2.547    2.547 {built-in method builtins.exec}
  5000000    0.851    0.000    0.851    0.000 {built-in method math.modf}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
5
) <-- Wrong

Làm thế nào để bạn có được phần số nguyên của số float trong Python?

modf () trong mô -đun toán học có thể được sử dụng để có được phần phân số và các phần số nguyên của một số cùng một lúc. môn Toán. modf () trả về một tuple (phần phân số, phần nguyên). can be used to get the fractional and the integer parts of a number at the same time. math. modf() returns a tuple (fractional part, integer part) .

Làm thế nào để bạn có được phần int của một chiếc phao?

Ví dụ, chúng tôi có giá trị phao ban đầu là 27,8 ...
Bằng cách chỉ sử dụng giá trị float ban đầu vào INT, bạn loại bỏ phần phân số và chỉ giữ phần số nguyên ..
Bằng cách nhân giá trị nổi ban đầu với 10, chúng tôi nhận được kết quả là 278.0, sau đó bằng cách chuyển kết quả này cho int, mang lại cho bạn giá trị 278 ..

Làm thế nào để bạn có được phần phân đoạn của một chiếc phao trong Python?

Sử dụng toán tử modulo ( %), toán tử % là toán tử số học tính toán và trả về phần còn lại sau khi phân chia hai số.Nếu một số được chia cho 1, phần còn lại sẽ là phần phân số.Vì vậy, sử dụng toán tử modulo sẽ cung cấp phần phân đoạn của một chiếc phao. The % operator is an arithmetic operator that calculates and returns the remainder after the division of two numbers. If a number is divided by 1, the remainder will be the fractional part. So, using the modulo operator will give the fractional part of a float.

Làm thế nào để bạn sử dụng .2f trong Python?

2F là một trình giữ chỗ cho số điểm nổi.Vì vậy, %D được thay thế bằng giá trị đầu tiên của tuple tức là 12 và %.2F được thay thế bằng giá trị thứ hai I.E 150.87612.... Định dạng chuỗi Python ..