有効数字の取得

28 12月

以前にも記事を書きましたが、有効数字を Python で取得するコードを再掲します。式を文字列で getSignificantFigures に渡します。

%reset -f

import sys
#from sympy import *
import sympy

def getSignificantFigures(tmpStr):

    def mainProcess(tmpStr):
        workingString = tmpStr
        index_e = tmpStr.find('e')
        index_E = tmpStr.find('E')

        if index_e != -1:
            workingString = tmpStr[:index_e]
        if index_E != -1:
            workingString = tmpStr[:index_E]

        workingString = workingString.replace('+',"")
        workingString = workingString.replace('-',"")
        workingString = workingString.replace('.',"")

        while workingString[0] == '0' and len(workingString) > 1:
            workingString = workingString[1:]

        significantFigures = len(workingString)    

        return significantFigures

    message = "none"
    significantFigures = -1

    typeArray = ["<class 'sympy.core.numbers.Integer'>", "<class 'sympy.core.numbers.Float'>", "<class 'sympy.core.numbers.Zero'>", "<class 'sympy.core.numbers.One'>", "<class 'sympy.core.numbers.NegativeOne'>"]

    tmpStr = tmpStr.replace(' ',"")

    try:
        expr = sympy.sympify(tmpStr, evaluate=False)
    except Exception as ex:
        expr = ex

    exprClass = str(type(expr))

    if exprClass == "<class 'sympy.core.sympify.SympifyError'>":    
        return significantFigures, "is not numerical value or expression."

    # 以下、とりあえず数式にはなっている    
    # 指数部の表記が異なる場合への対応

    tmpStr2 = tmpStr.replace('*10^',"e")
    tmpStr2 = tmpStr2.replace('*10**',"e")    

    try:
        expr2 = sympy.sympify(tmpStr2, evaluate=False)
    except Exception as ex:
        expr2 = ex

    expr2Class = str(type(expr2))    

    if exprClass in typeArray:        
        significantFigures = mainProcess(tmpStr)        
        return significantFigures, "float or integer"

    elif expr2Class in typeArray:
        significantFigures = mainProcess(tmpStr2)        
        return significantFigures, "you use 10^ or 10**."

    else:
        return significantFigures, "it does not have the form of a simple number."





testArray = []

testArray.append('0')
testArray.append('1')
testArray.append('-1')
testArray.append('2') 
testArray.append('100')
testArray.append('123')
testArray.append('0.010')
testArray.append('0.012')
testArray.append('0.00123')

testArray.append('-123')
testArray.append('- 123') # 符号との間に空白
testArray.append('-123 ') # 右側に空白
testArray.append(' - 123  ') # 多数の空白
testArray.append(' -0123  ') 
testArray.append('-0.123')

testArray.append('0.12e-3')
testArray.append('-0.12e-3')
testArray.append('-0.12E-3')
testArray.append('-0.0120e-3')
testArray.append(' + 0.01200e-3')
testArray.append('-2.14*10^3')
testArray.append('-2.14*10**3')

testArray.append('123*10^2*3*10^2')
testArray.append('x*y') 
testArray.append('sin(x)') 
testArray.append('x[0]') 
testArray.append('x1') 
testArray.append('2*pi') 




for i in range(len(testArray)):

    tmpStr = testArray[i]
    significantFigures = -1
    
    print('\nNo. ' + str(i))
    print('"' + tmpStr + '"')
    
    significantFigures, message = getSignificantFigures(tmpStr)
    
    print(message)
    print('significantFigures : ' + str(significantFigures))

返事は、有効数字の桁数と文字列の内容に関するメッセージです。下記が結果です。

​
No. 0
"0"
float or integer
significantFigures : 1

No. 1
"1"
float or integer
significantFigures : 1

No. 2
"-1"
float or integer
significantFigures : 1

No. 3
"2"
float or integer
significantFigures : 1

No. 4
"100"
float or integer
significantFigures : 3

No. 5
"123"
float or integer
significantFigures : 3

No. 6
"0.010"
float or integer
significantFigures : 2

No. 7
"0.012"
float or integer
significantFigures : 2

No. 8
"0.00123"
float or integer
significantFigures : 3

No. 9
"-123"
float or integer
significantFigures : 3

No. 10
"- 123"
float or integer
significantFigures : 3

No. 11
"-123 "
float or integer
significantFigures : 3

No. 12
" - 123  "
float or integer
significantFigures : 3

No. 13
" -0123  "
is not numerical value or expression.
significantFigures : -1

No. 14
"-0.123"
float or integer
significantFigures : 3

No. 15
"0.12e-3"
float or integer
significantFigures : 2

No. 16
"-0.12e-3"
float or integer
significantFigures : 2

No. 17
"-0.12E-3"
float or integer
significantFigures : 2

No. 18
"-0.0120e-3"
float or integer
significantFigures : 3

No. 19
" + 0.01200e-3"
float or integer
significantFigures : 4

No. 20
"-2.14*10^3"
you use 10^ or 10**.
significantFigures : 3

No. 21
"-2.14*10**3"
you use 10^ or 10**.
significantFigures : 3

No. 22
"123*10^2*3*10^2"
it does not have the form of a simple number.
significantFigures : -1

No. 23
"x*y"
it does not have the form of a simple number.
significantFigures : -1

No. 24
"sin(x)"
it does not have the form of a simple number.
significantFigures : -1

No. 25
"x[0]"
it does not have the form of a simple number.
significantFigures : -1

No. 26
"x1"
it does not have the form of a simple number.
significantFigures : -1

No. 27
"2*pi"
it does not have the form of a simple number.
significantFigures : -1

​