{"id":12289,"date":"2023-12-28T03:23:38","date_gmt":"2023-12-28T03:23:38","guid":{"rendered":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=12289"},"modified":"2023-12-28T18:08:14","modified_gmt":"2023-12-28T18:08:14","slug":"%e6%9c%89%e5%8a%b9%e6%95%b0%e5%ad%97%e3%81%ae%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=12289","title":{"rendered":"\u6709\u52b9\u6570\u5b57\u306e\u53d6\u5f97"},"content":{"rendered":"<p>\u4ee5\u524d\u306b\u3082\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u6709\u52b9\u6570\u5b57\u3092 Python \u3067\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3092\u518d\u63b2\u3057\u307e\u3059\u3002\u5f0f\u3092\u6587\u5b57\u5217\u3067 getSignificantFigures \u306b\u6e21\u3057\u307e\u3059\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\">%reset -f\r\n\r\nimport sys\r\n#from sympy import *\r\nimport sympy\r\n\r\ndef getSignificantFigures(tmpStr):\r\n\r\n    def mainProcess(tmpStr):\r\n        workingString = tmpStr\r\n        index_e = tmpStr.find(&#39;e&#39;)\r\n        index_E = tmpStr.find(&#39;E&#39;)\r\n\r\n        if index_e != -1:\r\n            workingString = tmpStr[:index_e]\r\n        if index_E != -1:\r\n            workingString = tmpStr[:index_E]\r\n\r\n        workingString = workingString.replace(&#39;+&#39;,&quot;&quot;)\r\n        workingString = workingString.replace(&#39;-&#39;,&quot;&quot;)\r\n        workingString = workingString.replace(&#39;.&#39;,&quot;&quot;)\r\n\r\n        while workingString[0] == &#39;0&#39; and len(workingString) &gt; 1:\r\n            workingString = workingString[1:]\r\n\r\n        significantFigures = len(workingString)    \r\n\r\n        return significantFigures\r\n\r\n    message = &quot;none&quot;\r\n    significantFigures = -1\r\n\r\n    typeArray = [&quot;&lt;class &#39;sympy.core.numbers.Integer&#39;&gt;&quot;, &quot;&lt;class &#39;sympy.core.numbers.Float&#39;&gt;&quot;, &quot;&lt;class &#39;sympy.core.numbers.Zero&#39;&gt;&quot;, &quot;&lt;class &#39;sympy.core.numbers.One&#39;&gt;&quot;, &quot;&lt;class &#39;sympy.core.numbers.NegativeOne&#39;&gt;&quot;]\r\n\r\n    tmpStr = tmpStr.replace(&#39; &#39;,&quot;&quot;)\r\n\r\n    try:\r\n        expr = sympy.sympify(tmpStr, evaluate=False)\r\n    except Exception as ex:\r\n        expr = ex\r\n\r\n    exprClass = str(type(expr))\r\n\r\n    if exprClass == &quot;&lt;class &#39;sympy.core.sympify.SympifyError&#39;&gt;&quot;:    \r\n        return significantFigures, &quot;is not numerical value or expression.&quot;\r\n\r\n    # \u4ee5\u4e0b\u3001\u3068\u308a\u3042\u3048\u305a\u6570\u5f0f\u306b\u306f\u306a\u3063\u3066\u3044\u308b    \r\n    # \u6307\u6570\u90e8\u306e\u8868\u8a18\u304c\u7570\u306a\u308b\u5834\u5408\u3078\u306e\u5bfe\u5fdc\r\n\r\n    tmpStr2 = tmpStr.replace(&#39;*10^&#39;,&quot;e&quot;)\r\n    tmpStr2 = tmpStr2.replace(&#39;*10**&#39;,&quot;e&quot;)    \r\n\r\n    try:\r\n        expr2 = sympy.sympify(tmpStr2, evaluate=False)\r\n    except Exception as ex:\r\n        expr2 = ex\r\n\r\n    expr2Class = str(type(expr2))    \r\n\r\n    if exprClass in typeArray:        \r\n        significantFigures = mainProcess(tmpStr)        \r\n        return significantFigures, &quot;float or integer&quot;\r\n\r\n    elif expr2Class in typeArray:\r\n        significantFigures = mainProcess(tmpStr2)        \r\n        return significantFigures, &quot;you use 10^ or 10**.&quot;\r\n\r\n    else:\r\n        return significantFigures, &quot;it does not have the form of a simple number.&quot;\r\n\r\n\r\n\r\n\r\n\r\ntestArray = []\r\n\r\ntestArray.append(&#39;0&#39;)\r\ntestArray.append(&#39;1&#39;)\r\ntestArray.append(&#39;-1&#39;)\r\ntestArray.append(&#39;2&#39;) \r\ntestArray.append(&#39;100&#39;)\r\ntestArray.append(&#39;123&#39;)\r\ntestArray.append(&#39;0.010&#39;)\r\ntestArray.append(&#39;0.012&#39;)\r\ntestArray.append(&#39;0.00123&#39;)\r\n\r\ntestArray.append(&#39;-123&#39;)\r\ntestArray.append(&#39;- 123&#39;) # \u7b26\u53f7\u3068\u306e\u9593\u306b\u7a7a\u767d\r\ntestArray.append(&#39;-123 &#39;) # \u53f3\u5074\u306b\u7a7a\u767d\r\ntestArray.append(&#39; - 123  &#39;) # \u591a\u6570\u306e\u7a7a\u767d\r\ntestArray.append(&#39; -0123  &#39;) \r\ntestArray.append(&#39;-0.123&#39;)\r\n\r\ntestArray.append(&#39;0.12e-3&#39;)\r\ntestArray.append(&#39;-0.12e-3&#39;)\r\ntestArray.append(&#39;-0.12E-3&#39;)\r\ntestArray.append(&#39;-0.0120e-3&#39;)\r\ntestArray.append(&#39; + 0.01200e-3&#39;)\r\ntestArray.append(&#39;-2.14*10^3&#39;)\r\ntestArray.append(&#39;-2.14*10**3&#39;)\r\n\r\ntestArray.append(&#39;123*10^2*3*10^2&#39;)\r\ntestArray.append(&#39;x*y&#39;) \r\ntestArray.append(&#39;sin(x)&#39;) \r\ntestArray.append(&#39;x[0]&#39;) \r\ntestArray.append(&#39;x1&#39;) \r\ntestArray.append(&#39;2*pi&#39;) \r\n\r\n\r\n\r\n\r\nfor i in range(len(testArray)):\r\n\r\n    tmpStr = testArray[i]\r\n    significantFigures = -1\r\n    \r\n    print(&#39;\\nNo. &#39; + str(i))\r\n    print(&#39;&quot;&#39; + tmpStr + &#39;&quot;&#39;)\r\n    \r\n    significantFigures, message = getSignificantFigures(tmpStr)\r\n    \r\n    print(message)\r\n    print(&#39;significantFigures : &#39; + str(significantFigures))\r\n<\/code><\/pre>\n<p>\u8fd4\u4e8b\u306f\u3001\u6709\u52b9\u6570\u5b57\u306e\u6841\u6570\u3068\u6587\u5b57\u5217\u306e\u5185\u5bb9\u306b\u95a2\u3059\u308b\u30e1\u30c3\u30bb\u30fc\u30b8\u3067\u3059\u3002\u4e0b\u8a18\u304c\u7d50\u679c\u3067\u3059\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\">\u200b\r\nNo. 0\r\n&quot;0&quot;\r\nfloat or integer\r\nsignificantFigures : 1\r\n\r\nNo. 1\r\n&quot;1&quot;\r\nfloat or integer\r\nsignificantFigures : 1\r\n\r\nNo. 2\r\n&quot;-1&quot;\r\nfloat or integer\r\nsignificantFigures : 1\r\n\r\nNo. 3\r\n&quot;2&quot;\r\nfloat or integer\r\nsignificantFigures : 1\r\n\r\nNo. 4\r\n&quot;100&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 5\r\n&quot;123&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 6\r\n&quot;0.010&quot;\r\nfloat or integer\r\nsignificantFigures : 2\r\n\r\nNo. 7\r\n&quot;0.012&quot;\r\nfloat or integer\r\nsignificantFigures : 2\r\n\r\nNo. 8\r\n&quot;0.00123&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 9\r\n&quot;-123&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 10\r\n&quot;- 123&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 11\r\n&quot;-123 &quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 12\r\n&quot; - 123  &quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 13\r\n&quot; -0123  &quot;\r\nis not numerical value or expression.\r\nsignificantFigures : -1\r\n\r\nNo. 14\r\n&quot;-0.123&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 15\r\n&quot;0.12e-3&quot;\r\nfloat or integer\r\nsignificantFigures : 2\r\n\r\nNo. 16\r\n&quot;-0.12e-3&quot;\r\nfloat or integer\r\nsignificantFigures : 2\r\n\r\nNo. 17\r\n&quot;-0.12E-3&quot;\r\nfloat or integer\r\nsignificantFigures : 2\r\n\r\nNo. 18\r\n&quot;-0.0120e-3&quot;\r\nfloat or integer\r\nsignificantFigures : 3\r\n\r\nNo. 19\r\n&quot; + 0.01200e-3&quot;\r\nfloat or integer\r\nsignificantFigures : 4\r\n\r\nNo. 20\r\n&quot;-2.14*10^3&quot;\r\nyou use 10^ or 10**.\r\nsignificantFigures : 3\r\n\r\nNo. 21\r\n&quot;-2.14*10**3&quot;\r\nyou use 10^ or 10**.\r\nsignificantFigures : 3\r\n\r\nNo. 22\r\n&quot;123*10^2*3*10^2&quot;\r\nit does not have the form of a simple number.\r\nsignificantFigures : -1\r\n\r\nNo. 23\r\n&quot;x*y&quot;\r\nit does not have the form of a simple number.\r\nsignificantFigures : -1\r\n\r\nNo. 24\r\n&quot;sin(x)&quot;\r\nit does not have the form of a simple number.\r\nsignificantFigures : -1\r\n\r\nNo. 25\r\n&quot;x[0]&quot;\r\nit does not have the form of a simple number.\r\nsignificantFigures : -1\r\n\r\nNo. 26\r\n&quot;x1&quot;\r\nit does not have the form of a simple number.\r\nsignificantFigures : -1\r\n\r\nNo. 27\r\n&quot;2*pi&quot;\r\nit does not have the form of a simple number.\r\nsignificantFigures : -1\r\n\r\n\u200b<\/code><\/pre>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u306b\u3082\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u6709\u52b9\u6570\u5b57\u3092 Python \u3067\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3092\u518d\u63b2\u3057\u307e &#8230; <a class=\"more-link\" href=\"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=12289\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-12289","post","type-post","status-publish","format-standard","hentry","category-column"],"_links":{"self":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/12289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12289"}],"version-history":[{"count":7,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/12289\/revisions"}],"predecessor-version":[{"id":12297,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/12289\/revisions\/12297"}],"wp:attachment":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12289"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}