Last active
January 22, 2020 08:48
-
-
Save ultimatile/33c46177ba140ec0d00964fcd118f48b to your computer and use it in GitHub Desktop.
print values and errors with significant figures in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import numpy as np | |
def ve(x, xerror): | |
if abs(x) > xerror: | |
round_xerror = np.float("{:.1g}".format(xerror)) | |
if round_xerror < 1: | |
exponent_xerror = np.int(np.floor(np.log10(round_xerror))) | |
mantissa_xerror = np.int(round_xerror / (10 ** exponent_xerror)) | |
round_x = np.round(x, -exponent_xerror) | |
specifier_x = "." + str(-exponent_xerror) + "f" | |
return str(format(round_x, specifier_x) + "({})".format(mantissa_xerror)) | |
else: | |
return str("{}({})".format(np.int(x), np.int(round_xerror))) | |
#return str("{}({})".format(np.int(x), np.int(xerror))) | |
else: | |
round_x = np.float("{:.1g}".format(abs(x))) | |
exponent_x = np.int(np.floor(np.log10(abs(round_x)))) | |
round_xerror = np.round(xerror, -exponent_x) | |
mantissa_x = np.int(round_x / (10 ** exponent_x)) | |
print("here") | |
if x < 0: | |
strh = "-" | |
else: | |
strh = "" | |
if round_xerror < 1: | |
vstr = strh + "0." + "0" * (-exponent_x - 1) + str(mantissa_x) | |
estr = str(np.int(round_xerror / (10 ** exponent_x))) | |
return vstr + "(" + estr + ")" | |
else: | |
#return str("{}({})".format(np.int(x), np.int(round_xerror))) | |
return str("{}({})".format(np.int(x), np.int(xerror))) | |
def case_ve(x, xerror): | |
if abs(x) > xerror: | |
round_xerror = np.float("{:.1g}".format(xerror)) | |
if round_xerror < 1: | |
return "case abs(x) > xerror and round_xerror < 1" | |
else: | |
return "case abs(x) > xerror and round_xerror >= 1" | |
else: | |
round_x = np.float("{:.1g}".format(abs(x))) | |
exponent_x = np.int(np.floor(np.log10(abs(round_x)))) | |
round_xerror = np.round(xerror, -exponent_x) | |
if round_xerror < 1: | |
return "case abs(x) <= xerror and round_xerror < 1" | |
else: | |
return "case abs(x) <= xerror and round_xerror >= 1" | |
def assertve(x, xerror, ans): | |
assert ve(x, xerror) == ans, "\nfailed; x={}, xerror={}\n showed: {}\n answer: {}\n{}".format(x, xerror, ve(x, xerror), ans, case_ve(x, xerror)) | |
print("pass\n") | |
if __name__ == "__main__": | |
print("test 1") | |
x = -0.0123456 | |
xerror = 0.00123456 | |
ans = "-0.012(1)" | |
assertve(x, xerror, ans) | |
print("test 2") | |
x = 9.157422490306612e-05 | |
xerror = 0.0001123 | |
ans = "0.00009(11)" | |
assertve(x, xerror, ans) | |
print("test 3") | |
x = 0.00009157422490306612 | |
xerror = 0.00000000001133813481973717 | |
ans = "0.00009157422(1)" | |
assertve(x, xerror, ans) | |
print("test 4") | |
x = 1.23456789E5 | |
xerror = 1.23456789E13 | |
ans = "123456(12345678900000)" | |
assertve(x, xerror, ans) | |
print("test 5") | |
x = 12.3456789 | |
xerror = 987.654321 | |
ans = "12(987)" | |
assertve(x, xerror, ans) | |
print("test 6") | |
x = 0.00125421 | |
xerror = 0.0235 | |
ans = "0.001(24)" | |
assertve(x, xerror, ans) | |
#not yet pass following tests | |
print("test 7") | |
x = 123456.7891423362361 | |
xerror = 1.23456789E-13 | |
ans = "123456.7891423362361(1)" | |
assertve(x, xerror, ans) | |
print("test 8") | |
x = 1.23456789E10 | |
xerror = 1.23456789E5 | |
ans = "12345678900(123457)" | |
assertve(x, xerror, ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
numpy懐疑主義者用
3倍のコード量か。。。
gistの使い方わからないのでコメントで失礼します。