Day 22_ 數值 (Numbers)- 分數
22-1. 分數表示
式子:
import fractions
print(fractions.Fraction(0.5))
結果:
1/2
式子:
import fractions
print(fractions.Fraction(1))
結果:
1
式子:
import fractions
print(fractions.Fraction(1,3))
結果:
1/3
22-2. 四則運算
式子:
from fractions import Fraction as F
print(F(1,1) + F(1,1))
結果:
2
式子:
from fractions import Fraction as F
print(F(1,2) - F(1,1))
結果:
0.1
式子:
from fractions import Fraction as F
print(F(1, 2) * F(2,3))
結果:
1/3
式子:
from fractions import Fraction as F
print(1 / F(1,2))
結果:
2
22-3. 不等式
式子:
from fractions import Fraction as F
print(F(-1, -2) < 0)
結果:
False
式子:
from fractions import Fraction as F
print(F(-1, 2) < 0)
結果:
True