Skip to content

Instantly share code, notes, and snippets.

@recuraki
Created May 10, 2021 13:49
Show Gist options
  • Save recuraki/efc51c73e607606864415f9b9b05e98b to your computer and use it in GitHub Desktop.
Save recuraki/efc51c73e607606864415f9b9b05e98b to your computer and use it in GitHub Desktop.
小数の比較.py
# a/b == Fraction(a,b) 有理数計算クラス
from fractions import Fraction
from decimal import Decimal
def f(a, x, y):
print("a, x, y,", a, ",", x, ",", y)
a = int(a)
x = int(x) # 1e16 + 1
y = int(y) # 1e16 + 2
# int型のまま
if a/x == a/y: print("/ : a/x == a/y")
if a/x > a/y: print("/ : a/x > a/y")
if a/x < a/y: print("/ : a/x < a/y")
# Decimal型で割り算
da, dx, dy = Decimal(a), Decimal(x), Decimal(y)
if da/dx == da/dy: print("Decimal : a/x == a/y")
if da/dx > da/dy: print("Decimal : a/x > a/y")
if da/dx < da/dy: print("Decimal : a/x < a/y")
# Fractionクラスで割り算
if Fraction(a, x) == Fraction(a, y): print("Fraction: a/x == a/y")
if Fraction(a, x) > Fraction(a, y): print("Fraction: a/x > a/y")
if Fraction(a, x) < Fraction(a, y): print("Fraction: a/x < a/y")
# 通分する
if a*y == a*x: print("Common : a/x == a/y (means: a*y == a*x)")
if a*y > a*x: print("Common : a/x > a/y (means: a*y > a*x)")
if a*y < a*x: print("Common : a/x < a/y (means: a*y < a*x)")
print("Python")
f("1", "10000000000000001", "10000000000000002")
f("1", "10000000000000003", "10000000000000004")
f("5", "10000000000000001", "10000000000000002")
f("5", "10000000000000003", "10000000000000004")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment