Hướng dẫn how do you add fractions in python? - làm thế nào để bạn thêm phân số trong python?

Chạy

def findGCD[n1, n2]:
    gcd = 0
    for i in range[1, int[min[n1, n2]] + 1]:
        if n1 % i == 0 and n2 % i == 0:
            gcd = i
    return gcd


# input first fraction
num1, den1 = map[int, list[input["Enter numerator and denominator of first number : "].split[" "]]]

# input first fraction
num2, den2 = map[int, list[input["Enter numerator and denominator of second number: "].split[" "]]]

lcm = [den1 * den2] // findGCD[den1, den2]

sum = [num1 * lcm // den1] + [num2 * lcm // den2]

num3 = sum // findGCD[sum, lcm]

lcm = lcm // findGCD[sum, lcm]

print[num1, "/", den1, " + ", num2, "/", den2, " = ", num3, "/", lcm]

Đầu ra

Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5

Mã nguồn: lib/phân số.py Lib/fractions.py

Mô -đun fractions cung cấp hỗ trợ cho số học số hợp lý.

Một thể hiện phân số có thể được xây dựng từ một cặp số nguyên, từ một số hợp lý khác hoặc từ một chuỗi.

Lớp ____ ____ 8 ________ 9 [Số = 0, mẫu số = 1] ¶ Lớp ________ 8 ________ 9 [Other_Fraction] Lớp ________ 8 ________ 9 [Float] Lớp[numerator=0, denominator=1]¶ class fractions.Fraction[other_fraction] class fractions.Fraction[float] class fractions.Fraction[decimal] class fractions.Fraction[string]

Phiên bản đầu tiên yêu cầu tử số và mẫu số là các phiên bản là

Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
8 và trả về một phiên bản
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 mới với giá trị
[sign] numerator ['/' denominator]
0. Nếu mẫu số là
[sign] numerator ['/' denominator]
1, nó sẽ tăng
[sign] numerator ['/' denominator]
2. Phiên bản thứ hai yêu cầu các trường hợp khác là một thể hiện của
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
8 và trả về một thể hiện
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 với cùng một giá trị. Hai phiên bản tiếp theo chấp nhận một ví dụ
[sign] numerator ['/' denominator]
5 hoặc
[sign] numerator ['/' denominator]
6 và trả về một thể hiện
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 với chính xác cùng một giá trị. Lưu ý rằng do các vấn đề thông thường với điểm nổi nhị phân [xem Số học điểm nổi: Các vấn đề và giới hạn], đối số của
[sign] numerator ['/' denominator]
8 không chính xác bằng 11/10, và do đó
[sign] numerator ['/' denominator]
8 không trả lại
>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
0 như người ta có thể mong đợi. [Nhưng xem tài liệu cho phương thức
>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
1 bên dưới.] Phiên bản cuối cùng của hàm tạo mong đợi một trường hợp chuỗi hoặc unicode. Mẫu thông thường cho trường hợp này là:Floating Point Arithmetic: Issues and Limitations], the argument to
[sign] numerator ['/' denominator]
8 is not exactly equal to 11/10, and so
[sign] numerator ['/' denominator]
8 does not return
>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
0 as one might expect. [But see the documentation for the
>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
1 method below.] The last version of the constructor expects a string or unicode instance. The usual form for this instance is:

[sign] numerator ['/' denominator]

trong đó

>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
2 tùy chọn có thể là ‘+hoặc hoặc‘-và và
>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
3 và
>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]
4 [nếu có] là các chuỗi của các chữ số thập phân. Ngoài ra, bất kỳ chuỗi nào đại diện cho một giá trị hữu hạn và được chấp nhận bởi hàm tạo
[sign] numerator ['/' denominator]
5 cũng được chấp nhận bởi hàm tạo
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9. Trong cả hai hình thức, chuỗi đầu vào cũng có thể có khoảng trắng dẫn đầu và/hoặc dấu vết. Dưới đây là một số ví dụ:

>>> from fractions import Fraction
>>> Fraction[16, -10]
Fraction[-8, 5]
>>> Fraction[123]
Fraction[123, 1]
>>> Fraction[]
Fraction[0, 1]
>>> Fraction['3/7']
Fraction[3, 7]
>>> Fraction[' -3/7 ']
Fraction[-3, 7]
>>> Fraction['1.414213 \t\n']
Fraction[1414213, 1000000]
>>> Fraction['-.125']
Fraction[-1, 8]
>>> Fraction['7e-6']
Fraction[7, 1000000]
>>> Fraction[2.25]
Fraction[9, 4]
>>> Fraction[1.1]
Fraction[2476979795053773, 2251799813685248]
>>> from decimal import Decimal
>>> Fraction[Decimal['1.1']]
Fraction[11, 10]

Lớp

Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 kế thừa từ lớp cơ sở trừu tượng
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
8 và thực hiện tất cả các phương pháp và hoạt động từ lớp đó. Các trường hợp
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 có thể băm, và nên được coi là bất biến. Ngoài ra,
Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 có các thuộc tính và phương pháp sau:

Đã thay đổi trong phiên bản 3.9: Hàm

>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
1 hiện được sử dụng để bình thường hóa tử số và mẫu số.
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
1 Luôn trả về loại
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
3. Trước đây, loại GCD phụ thuộc vào tử số và mẫu số.The
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
1 function is now used to normalize the numerator and denominator.
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
1 always return a
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
3 type. Previously, the GCD type depended on numerator and denominator.

________ 44¶

Tử số của phân số trong thời hạn thấp nhất.

________ 45¶

Mẫu số của phân số trong thời hạn thấp nhất.

________ 46 [][]

Trả về một tuple của hai số nguyên, có tỷ lệ bằng phân số và với mẫu số dương.

Mới trong phiên bản 3.8.

ClassMethod ________ 47 [FLT] ¶[flt]

Hàm tạo thay thế chỉ chấp nhận các trường hợp

[sign] numerator ['/' denominator]
5 hoặc
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
9. Hãy coi chừng
>>> from math import pi, cos
>>> Fraction[cos[pi/3]]
Fraction[4503599627370497, 9007199254740992]
>>> Fraction[cos[pi/3]].limit_denominator[]
Fraction[1, 2]
>>> Fraction[1.1].limit_denominator[]
Fraction[11, 10]
0 không giống với giá trị
>>> from math import pi, cos
>>> Fraction[cos[pi/3]]
Fraction[4503599627370497, 9007199254740992]
>>> Fraction[cos[pi/3]].limit_denominator[]
Fraction[1, 2]
>>> Fraction[1.1].limit_denominator[]
Fraction[11, 10]
1.

Ghi chú

Từ Python 3.2 trở đi, bạn cũng có thể xây dựng một ví dụ

Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 trực tiếp từ
[sign] numerator ['/' denominator]
5.

ClassMethod ________ 54 [tháng 12] ¶[dec]

Hàm tạo thay thế chỉ chấp nhận các trường hợp

[sign] numerator ['/' denominator]
6 hoặc
>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
9.

________ 57 [max_denominator = 1000000] ¶[max_denominator=1000000]

Tìm và trả về

Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5
9 gần nhất đến
>>> from math import pi, cos
>>> Fraction[cos[pi/3]]
Fraction[4503599627370497, 9007199254740992]
>>> Fraction[cos[pi/3]].limit_denominator[]
Fraction[1, 2]
>>> Fraction[1.1].limit_denominator[]
Fraction[11, 10]
9 có mẫu số ở hầu hết MAX_DENOMINATOR. Phương pháp này rất hữu ích để tìm các xấp xỉ hợp lý cho một số điểm nổi nhất định:

>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]

hoặc để phục hồi một số hợp lý mà LỚN đại diện cho một chiếc phao:

>>> from math import pi, cos
>>> Fraction[cos[pi/3]]
Fraction[4503599627370497, 9007199254740992]
>>> Fraction[cos[pi/3]].limit_denominator[]
Fraction[1, 2]
>>> Fraction[1.1].limit_denominator[]
Fraction[11, 10]

________ 60 [][]

Trả về

>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
3 lớn nhất
>>> from math import floor
>>> floor[Fraction[355, 113]]
3
2. Phương pháp này cũng có thể được truy cập thông qua hàm
>>> from math import floor
>>> floor[Fraction[355, 113]]
3
3:

>>> from math import floor
>>> floor[Fraction[355, 113]]
3

________ 64 [][]

Trả về ít nhất

>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
3
>>> from math import floor
>>> floor[Fraction[355, 113]]
3
6. Phương pháp này cũng có thể được truy cập thông qua hàm
>>> from math import floor
>>> floor[Fraction[355, 113]]
3
7.

________ 68 [] ____ ____ 68 [ndigits][]
>>> from math import floor
>>> floor[Fraction[355, 113]]
3
8[ndigits]

Phiên bản đầu tiên trả lại

>>> from fractions import Fraction
>>> Fraction['3.1415926535897932'].limit_denominator[1000]
Fraction[355, 113]
3 gần nhất đến
>>> from math import pi, cos
>>> Fraction[cos[pi/3]]
Fraction[4503599627370497, 9007199254740992]
>>> Fraction[cos[pi/3]].limit_denominator[]
Fraction[1, 2]
>>> Fraction[1.1].limit_denominator[]
Fraction[11, 10]
9, làm tròn một nửa vào chẵn. Phiên bản thứ hai làm tròn
>>> from math import pi, cos
>>> Fraction[cos[pi/3]]
Fraction[4503599627370497, 9007199254740992]
>>> Fraction[cos[pi/3]].limit_denominator[]
Fraction[1, 2]
>>> Fraction[1.1].limit_denominator[]
Fraction[11, 10]
9 đến bội số gần nhất của fractions3 [về mặt logic, nếu fractions4 là âm], một lần nữa làm tròn một nửa về phía chẵn. Phương pháp này cũng có thể được truy cập thông qua hàm fractions5.

Xem thêm

Mô -đun fractions6

Các lớp cơ sở trừu tượng tạo nên tháp số.

Làm thế nào để bạn thêm một tử số và mẫu số trong Python?

Algorithm..
Khởi tạo các biến của tử số và mẫu số ..
Lấy đầu vào người dùng của hai phân số ..
Tìm tử số bằng điều kiện này [N1*D2] +[D1*N2] trong đó N1, N2 là tử số và D1 và D2 là mẫu số ..
Tìm mẫu số bằng điều kiện này [D1*D2] cho LCM ..
Tính GCD của một tử số và mẫu số mới này ..

Làm thế nào để bạn tạo ra một phần hỗn hợp trong Python?

Phân số hỗn hợp trong Python..
num = int [input ['Tiểu số']].
dem = int [input ['loại mẫu số']].
a = num // dem ..
b = num % dem ..
In 'Số hỗn hợp là {} và {}/{}'.Định dạng [a, b, dem].

Bài Viết Liên Quan

Chủ Đề