2019-10-25 13:38:10 +02:00
|
|
|
import math
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
def decfun(f):
|
|
|
|
def g(x):
|
|
|
|
return Decimal(f(float(x)))
|
|
|
|
return g
|
|
|
|
|
|
|
|
def degree2decimal(f):
|
|
|
|
def g(x):
|
|
|
|
return Decimal(f(float(x) * math.pi / 180))
|
|
|
|
return g
|
|
|
|
|
|
|
|
def decimal2degree(f):
|
|
|
|
def g(x):
|
|
|
|
return Decimal(f(float(x)) * 180 / math.pi)
|
|
|
|
return g
|
|
|
|
|
|
|
|
def decimal_repr(x):
|
|
|
|
# Remove trailing 0's
|
|
|
|
x = x.normalize()
|
|
|
|
|
|
|
|
if x == 0:
|
|
|
|
return "0"
|
|
|
|
|
|
|
|
# Keep only 10 digits for printing
|
|
|
|
i, e = int(x._int), x._exp
|
|
|
|
digits = len(str(i))
|
|
|
|
overflow = digits - 10
|
|
|
|
|
|
|
|
if overflow > 0:
|
|
|
|
i = (i + (10 ** overflow) // 2) // (10 ** overflow)
|
|
|
|
e += overflow
|
|
|
|
digits = 10
|
|
|
|
|
|
|
|
# Handle sign
|
|
|
|
sign = "-" if x._sign else ""
|
|
|
|
|
|
|
|
# Exponent in engineering notation (true exponent of value)
|
|
|
|
true_exp = e + digits - 1
|
|
|
|
|
|
|
|
# For integers up to 10 digits: print normally
|
|
|
|
if e >= 0 and true_exp <= 9:
|
|
|
|
return sign + str(i * 10 ** e)
|
|
|
|
|
|
|
|
# For larger integers, use scientific notation
|
|
|
|
elif e >= 0:
|
|
|
|
dg = str(i)
|
|
|
|
dg = dg[0] + "." + dg[1:] + "e" + str(true_exp)
|
|
|
|
return sign + dg
|
|
|
|
|
|
|
|
# Otherwise, if there are less than 10 digits overall: print normally
|
|
|
|
elif true_exp >= -9:
|
|
|
|
supplement = max(-true_exp, 0)
|
|
|
|
dg = "0" * supplement + str(i)
|
|
|
|
true_exp += supplement
|
|
|
|
|
|
|
|
dg = dg[:true_exp+1] + "." + dg[true_exp+1:]
|
|
|
|
return sign + dg
|
|
|
|
|
|
|
|
# For very small numbers, use scientific notation again
|
|
|
|
else:
|
|
|
|
dg = str(i)
|
|
|
|
dg = dg[0] + "." + dg[1:] + "e" + str(true_exp)
|
|
|
|
return sign + dg
|
2019-10-26 13:51:54 +02:00
|
|
|
|
|
|
|
def rndcoord(x):
|
|
|
|
halfint = (int(x) != x) and (int(2*x) == 2*x)
|
|
|
|
sign = -1 if x < 0 else 1
|
|
|
|
return int(round(x)) + int(halfint) * sign
|