{"id":20496319,"url":"https://github.com/giocip/num7","last_synced_at":"2025-04-13T18:21:11.898Z","repository":{"id":64462363,"uuid":"575932068","full_name":"giocip/num7","owner":"giocip","description":"num7 - SUPREME PRECISION GENERAL PURPOSE ARITHMETIC-LOGIC DECIMAL LIBRARY PACKAGE","archived":false,"fork":false,"pushed_at":"2025-03-07T15:40:58.000Z","size":322,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T09:04:16.407Z","etag":null,"topics":["arbitrary-precision","arithmetic","decimal","financial","floating-point","ieee754","math","micropython","precision","python","raspberry","school-education"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/giocip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["giocip"]}},"created_at":"2022-12-08T16:14:13.000Z","updated_at":"2025-03-07T15:41:01.000Z","dependencies_parsed_at":"2025-02-19T10:23:06.849Z","dependency_job_id":"5fb22ed3-cb28-42c4-acae-266ff08bd55c","html_url":"https://github.com/giocip/num7","commit_stats":{"total_commits":59,"total_committers":1,"mean_commits":59.0,"dds":0.0,"last_synced_commit":"b9826aeaefd472571456c08f271f3889c04e7443"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giocip%2Fnum7","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giocip%2Fnum7/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giocip%2Fnum7/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giocip%2Fnum7/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giocip","download_url":"https://codeload.github.com/giocip/num7/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248758987,"owners_count":21157067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arbitrary-precision","arithmetic","decimal","financial","floating-point","ieee754","math","micropython","precision","python","raspberry","school-education"],"created_at":"2024-11-15T18:06:32.688Z","updated_at":"2025-04-13T18:21:11.881Z","avatar_url":"https://github.com/giocip.png","language":"Python","funding_links":["https://github.com/sponsors/giocip"],"categories":[],"sub_categories":[],"readme":"# num7 - SUPREME PRECISION GENERAL PURPOSE ARITHMETIC-LOGIC DECIMAL LIBRARY PACKAGE\n## _DESCRIPTION AND DOC_\n\n- _**`Num`**_ is a lightweight floating point numeric class for arbitrary precision results with always supreme precision.\n\nEasy to use like school math and WITHOUT IEEE754 ISSUES or -0 FAILURES, it can be deployed  \nfor web e-commerce developing, accounting apps and general math programs included financial ones.  \nCompatible with MicroPython also a Rasperry pi pico (RP2040) can work with almost num7 capability.  \n\n---\n\n## Installation num7 package\n\n### Using PIP\n\n- To install _**`num7 package`**_ using `pip`, enter the following:\n\n  ```python\n  pip  install num7 #win\n  pip3 install num7 #linux\n  ```\n\n- Ok!\n\n---\n\n## HOW TO USE (integer numeric strings (ex. '2.0') MUST BE SUFFIXED WITH .0):\n--- CALCULATOR MODE ---  \n\n\t                   \u003e\u003e\u003e from num7 import Num, Num as calc  \n\tADDITION:          \u003e\u003e\u003e calc.add('-5.3', '2.1')    #Num('-3.2')  \n\tSUBTRACTION:       \u003e\u003e\u003e calc.sub('-5.3', '2.1')    #Num('-7.4')  \n\tMULTIPLICATION:    \u003e\u003e\u003e calc.mul('-5.3', '2.1')    #Num('-11.13')  \n\tDIVISION:          \u003e\u003e\u003e calc.div('-5.3', '2.1')    #Num('-2.52380952380952380952380952380952380952380952380952380952380952380952380952380952')  \n\tM+:                \u003e\u003e\u003e M = calc('0.0'); M.inc('3.0'); M.inc('3.3'); M.inc('3.7'); print(M) #10.0  \n\tM-:                \u003e\u003e\u003e                  M.dec('5.0'); M.dec('3.3'); M.dec('1.5'); print(M) #0.2  \n\tMC:                \u003e\u003e\u003e M.clear(); print(M) #0.0  \n\tINT   DIV AND REM: \u003e\u003e\u003e calc.divmod('5.0', '3.0')  #(Num('1.0'), Num('2.0')) =\u003e tuple  \n\tFLOAT DIV AND REM: \u003e\u003e\u003e calc.divmod('5.2', '3.1')  #(Num('1.0'), Num('2.1')) =\u003e tuple  \n\tPOWER:             \u003e\u003e\u003e calc.pow('-5.3', '2.0')    #Num('28.09')  \n\tSQRT:              \u003e\u003e\u003e calc.sqrt('2.0')           #Num('1.41421356237309504880168872420969807856967187537694807317667973799073247846210703')  \n\tROOT_ith           \u003e\u003e\u003e calc.root_i('1.860867', 3) #Num('1.23')  \n\tROUND:             \u003e\u003e\u003e calc.sqrt('2.0').round(2)  #Num('1.41')  \n\tABSOLUTE VALUE     \u003e\u003e\u003e calc.abs('-3.0')           #Num('3.0')  \n\tSUM:               \u003e\u003e\u003e cart = ['19.32','18.37','15.13']; calc.sum(*cart)          #Num('52.82')  \n\tMEAN:              \u003e\u003e\u003e cart = ['19.32','18.37','15.13']; calc.mean(*cart).round() #Num('17.61')  \n\tMIN:               \u003e\u003e\u003e cart = ['19.32','18.37','15.13']; calc.min(cart)           #Num('15.13')  \n\tMAX:               \u003e\u003e\u003e cart = ['19.32','18.37','15.13']; calc.max(cart)           #Num('19.32')  \n\tEXP:               \u003e\u003e\u003e calc.mul('-5.3e1024', '2.1e1024').num2exp()                #'-1.113e2049'  \n\tREPL:              \u003e\u003e\u003e a = calc('0.1'); b = calc('0.2'); print(calc.add(a, b))    #0.3  \n\n## CODING:  \n\t\u003e\u003e\u003e from num7 import Num, Num as calc\n\n(=) assignment:  \n\n\t\u003e\u003e\u003e a = Num('3.0'); b = Num('5.0'); c = Num('0.0'); #  \n\t\u003e\u003e\u003e print('a =', a, 'b =', b, 'c =', c) #a = 3.0 b = 5.0 c = 0.0  \n\n(+) adding:  \n\n\t\u003e\u003e\u003e R = a+b+c; print(R) #8.0  \n\t\u003e\u003e\u003e a = Num('0.1'); b = Num('0.2'); c = Num('0.0'); print(a+b+c) #0.3  \n\n(-) subtracting:  \n\n\t\u003e\u003e\u003e a = Num('0.1'); b = Num('0.2'); c = Num('0.3');  \n\t\u003e\u003e\u003e print(a+b-c) #0.0  \n\t\u003e\u003e\u003e R = Num('-3.99') - Num('-5.20') - Num('+3.01'); print(R) #-1.8  \n\n(*) multiplying:  \n\n\t\u003e\u003e\u003e Num('-3.99') * Num('-5.20') * Num('+3.01') #-3.99 * (-5.20) * (+3.01 ) = Num('62.45148')  \n\n(/) dividing (80 decimal digits default gets only for division operation):  \n\n\t\u003e\u003e\u003e Num('3.0') / Num('5.7') #3 : 5.7 = Num('0.52631578947368421052631578947368421052631578947368421052631578947368421052631578')  \n\nDivision precision (ex. 128 decs) may be specified as parameter after numeric string as: \n \t    \n\t\u003e\u003e\u003e Num('3.0', 128) / Num('5.7', 128) #3 : 5.7 = Num('0.52631578947368421052631578947368421052631578947368421052631578947368421052631578947368421052631578947368421052631578947368421052')  \n\n(// % operators, divmod python3 built-in function) int division and remainder:  \n\n\t\u003e\u003e\u003e a = Num('5.0'); b = Num('2.0') #  \n\t\u003e\u003e\u003e Q = a // b; R = a % b; print('Quotient =', Q, 'Remainder =', R) #Quotient = 2.0 Remainder = 1.0  \n\t\u003e\u003e\u003e a = Num('15.0'); b = Num('4.0') #  \n\t\u003e\u003e\u003e Q, R = divmod(a, b); print('Quotient =', Q, 'Remainder =', R)   #Quotient = 3.0 Remainder = 3.0  \n\n(divmod python3 built-in function) floating division and remainder:  \n\n\t\u003e\u003e\u003e a = Num('10.123456789'); b = Num('2.0') #  \n\t\u003e\u003e\u003e Q, R = divmod(a, b); print('Quotient =', Q, 'Remainder =', R)   #Quotient = 5.0 Remainder = 0.123456789  \n\n(sqrt) square root function:  \n\n\t\u003e\u003e\u003e a = Num('123_456_789.1234567890123456789'); root = a.sqrt() # Num('11111.11106611111096998611053449930232404576951925017079015206589094347963821409843324')  \n\t\u003e\u003e\u003e print('result digits number tuple =\u003e', root.len()) #result digits number tuple =\u003e (5, 80)  \n\n(**) power operator and pow python3 built-in function:  \n\n\t\u003e\u003e\u003e a = Num('2.22123') ** 64; print(a) # 15204983311631674774944.65147209888660757554174463321311015807893679105748958794491681177995203669698667160837739445605536688871012507194541849848681968140805876570485027380472936734094801420552285940765338219588362327695177798251793912104057999943308320501195784173135380826413054938730768027747418766018606636039075568645106645889100039914241  \n\t\u003e\u003e\u003e print(a.len())      #(23, 320) digits len tuple  \n\t\u003e\u003e\u003e print(Num(Num.pi))  #3.141592654  \n\t\u003e\u003e\u003e pow(Num(Num.pi), 8) #Num('9488.531025982131642534428505085353941520356351078169077371202330414440366336')  \n\nlogic (in, not in, is, is not, \u003c, \u003c=, \u003e, \u003e=, !=, ==) and relational operators (and, or, not).  \n\n(in):  \n\n\t\u003e\u003e\u003e L = [Num('0.1'), Num('1.0'), Num('5.5'), Num('-3.0'), Num('-2.9'), Num('-3.0001'), Num('2.2')]  \n\t\u003e\u003e\u003e Num('-3.0001') in L; Num('-3.00001') in L         #True False  \n\n(not in):  \n\n\t\u003e\u003e\u003e Num('-3.0001') not in L; Num('-3.00001') not in L #False True  \n\n(is, is not):  \n\n\t\u003e\u003e\u003e M = calc('0.0'); Num('0.0') is M #False  \n\t\u003e\u003e\u003e M = calc('0.0'); M.inc('0.1') is not M; #False \n\t\u003e\u003e\u003e M; N = M; N.dec('0.1'); N is M; #True\n\n(\u003c \u003c= \u003e \u003e= != ==)  \n\n\t\u003e\u003e\u003e a = Num('0.0'); b = Num('0.1'); c = Num('-0.2')  \n\t\u003e\u003e\u003e a \u003c  b; a \u003c  c; b \u003c  c #True  False False  \n\t\u003e\u003e\u003e a \u003c= a; a \u003c= c; b \u003c= c #True  False False  \n\t\u003e\u003e\u003e a \u003e  b; a \u003e  c; b \u003e  c #False True  True  \n\t\u003e\u003e\u003e a \u003e= a; a \u003e= c; b \u003e= c #True  True  True  \n\t\u003e\u003e\u003e c == -2*b; a == c + 2*b ; a != a+b+c #True  True  True  \n\t\u003e\u003e\u003e a and b; a or b; not a #Num('0.0') Num('0.1') True  \n\t\u003e\u003e\u003e True if a and b else False #False  \n\t\u003e\u003e\u003e True if a or  b else False #True  \n\n(+ - unary operators)\n  \n\t\u003e\u003e\u003e Num('+2.5521') # Num('2.5521')  \n\t\u003e\u003e\u003e Num('-3.3321') # Num('-3.3321')  \n\t\u003e\u003e\u003e Num('+2.5521') + Num('-3.3321') #Num('-0.78')  \n\nbitwise operators code:\n\n    from num7 import Num  \n    print('--- (\u0026) AND ---')  \n    op1 = Num('3.0')  \n    op2 = 5  \n    print(f'{int(op1):08b}', op1) #00000011 3.0  \n    op1 \u0026= op2                    #AND  \n    print(f'{op2:08b}', op2)      #00000101 5  \n    print(f'{int(op1):08b}', op1) #00000001 1  \n\n    print('--- (|) OR  ---')  \n    op1 = Num('3.0')  \n    op2 = 5  \n    print(f'{int(op1):08b}', op1) #00000011 3.0  \n    op1 |= op2                    #OR  \n    print(f'{op2:08b}', op2)      #00000101 5  \n    print(f'{int(op1):08b}', op1) #00000111 7  \n\n    print('--- (^) XOR ---')  \n    op1 = Num('3.0')  \n    op2 = 5  \n    print(f'{int(op1):08b}', op1) #00000011 3.0  \n    op1 ^= op2                    #XOR  \n    print(f'{op2:08b}', op2)      #00000101 5  \n    print(f'{int(op1):08b}', op1) #00000110 6  \n\n    print('--- (\u003c\u003c) LEFT SHIFT -X10 MULTIPLIER ---')  \n    op1 = Num('1.0')  \n    op2 = 2  \n    print(f'{int(op1):08b}', op1) #00000001 1.0  \n    op1 \u003c\u003c= op2                   #LEFT SHIFT -X10 MULTIPLIER  \n    print(f'{op2:08b}', op2)      #00000010 2  \n    print(f'{int(op1):08b}', op1) #01100100 100.0  \n\n    print('--- (\u003e\u003e) RIGHT SHIFT -X10 DIVIDER ---')  \n    op1 = Num('250.0')  \n    op2 = 1  \n    print(f'{int(op1):08b}', op1) #11111010 250.0  \n    op1 \u003e\u003e= op2                   #RIGHT SHIFT -X10 DIVIDER  \n    print(f'{op2:08b}', op2)      #00000001 1  \n    print(f'{int(op1):08b}', op1) #00011001 25.0  \n\n    print('--- (~) NOT ---')  \n    op1 = Num('10.0')  \n    print(f'{int(op1):08b}', op1) #00001010 10.0  \n    op2 = ~op1                    #(~) NOT  \n    print(f'{int(op2):08b}', op2) #00000101 5.0  \n\nOn a given variable the following arithmetic methods are available:\n\n    #variable arithmetics  \n    from num7 import Num  \n    a = Num('10.25')  \n    print(a)       #10.25  \n    a.inc()        #increment (default) by one  \n    print(a)       #11.25   \n    a.dec(2)       #decrement (optional) 2 units  \n    print(a)       #9.25  \n    a.incmul()     #multiply (default) 10 times  \n    print(a)       #92.5  \n    a.decdiv(100)  #x100 (optional) division  \n    print(a)       #0.925  \n    a.clear()      #a variable set to zero   \n    print(a)       #0.0    \n\nEVEN ODD numbering methods:\n\n    from num7 import Num  \n    a = Num(6); b = Num(3); c = Num('3.14')  \n    print(a, 'INTEGER =\u003e', a.is_numint(), 'EVEN =\u003e', a.is_numeven()) #6.0 INTEGER =\u003e True EVEN =\u003e True  \n    print(b, 'INTEGER =\u003e', b.is_numint(), 'ODD  =\u003e', b.is_numodd())  #3.0 INTEGER =\u003e True ODD  =\u003e True \n    print(c, 'FLOAT  =\u003e', c.is_numfloat())                           #3.14 FLOAT  =\u003e True  \n\n# Advanced logic programming snippet\n\nLOOP EXAMPLE \u003e\u003e\u003e  \n\n\tfrom num7 import Num  \n\ti = Num(0)  \n\twhile i \u003c Num('1.0'):  \n\t\ti.inc('0.1') #i += Num('0.1')  \n\t\tif i \u003c= Num('0.5'):  \n\t\t\tcontinue  \n\t\tprint(i) #0.6, 0.7, 0.8, 0.9, 1.0  \n\twhile i:  \n\t\ti.dec('0.1') #i -= Num('0.1')  \n\t\tif i \u003e= Num('0.5'):  \n\t\t\tcontinue  \n\t\tprint(i) #0.4 0.3 0.2 0.1 0.0  \n\nROUNDING AND ACCOUNTING \u003e\u003e\u003e  \n\n\tfrom num7 import Num   \n\tp = Num('11.19')                       #PRICE -Toslink cable for soundbar  \n\tpd = round(p.f_price_over(-7))         #PRICE DISCOUNTED 7%  \n\td = round(p - pd)                      #DISCOUNT  \n\tp_noTAX = round(p.f_price_spinoff(22)) #ITEM COST WITHOUT TAX 22%  \n\tTAX = round(p - p_noTAX)               #TAX 22%  \n\tprint(F'price={p} PAYED={pd} discount={d} COST={p_noTAX} TAX={TAX}') #price=11.19 PAYED=10.41 discount=0.78 COST=9.17 TAX=2.02  \n\nOUTPUT FORMATTING AND LOCALIZATION \u003e\u003e\u003e\n\n    import locale  \n    from num7 import Num  \n    s = locale.setlocale(locale.LC_ALL, \"\")  \n    print('settings:', s) #settings: Italian_Italy.1252  \n    #calculating banking loan  \n    asset = Num('100_000.0'); rate = Num('6.5'); years = Num('20.0')  \n    monthly_payment = Num.f_fund_fr(asset, rate, years)  \n    print(locale.format_string(\"%.2f\", float(monthly_payment)))   #756,30  \n    print(locale.currency(float(monthly_payment), grouping=True)) #756,30 (currency symbol)  \n\nROUNDING TYPES \u003e\u003e\u003e  \n\n    from num7 import Num  \n    ''' Num floor rounding '''  \n    print('--' * 10 + ' Num floor rounding')  \n    n = Num(Num.pi)            # 3.141592654  \n    print(n, n.round_floor(2)) # 3.14  \n    n = -Num(Num.pi)           #-3.141592654  \n    print(n, n.round_floor(2)) #-3.15  \n    n = Num(Num.pi) - 3        # 0.141592654  \n    print(n, n.round_floor(2)) # 0.14  \n    n = -Num(Num.pi) + 3       #-0.141592654  \n    print(n, n.round_floor(2)) #-0.15  \n\n    print('--' * 10 + ' Num ceil rounding')  \n    ''' Num ceil rounding '''  \n    n = Num(Num.pi)           # 3.141592654  \n    print(n, n.round_ceil(2)) # 3.15  \n    n = -Num(Num.pi)          #-3.141592654  \n    print(n, n.round_ceil(2)) #-3.14  \n    n = Num(Num.pi) - 3       # 0.141592654  \n    print(n, n.round_ceil(2)) # 0.15  \n    n = -Num(Num.pi) + 3      #-0.141592654  \n    print(n, n.round_ceil(2)) #-0.14  \n\n    print('--' * 10 + ' Num standard rounding')  \n    ''' Num standard rounding '''  \n    n = Num(Num.pi)      # 3.141592654  \n    print(n, n.round())  # 3.14  \n    n = -Num(Num.pi)     #-3.141592654  \n    print(n, n.round())  #-3.14  \n    n = Num(Num.pi) - 3  # 0.141592654  \n    print(n, n.round(4)) # 0.1416  \n    n = -Num(Num.pi) + 3 #-0.141592654  \n    print(n, n.round(4)) #-0.1416  \n\n    print('--' * 10 + ' Num half to even rounding (statistic, zero symmetric)')  \n    ''' Num half even rounding '''  \n    n = Num(Num.pi).round_floor(4)      # 3.1415  \n    print(n, n.round_bank(3))           # 3.142  \n    n = -Num(Num.pi).round_floor(4)     #-3.1415  \n    print(n, n.round_bank(3))           #-3.142  \n    n = Num(Num.pi).round_floor(8) - 3  # 0.14159265  \n    print(n, n.round_bank(7))           # 0.1415926  \n    n = -Num(Num.pi).round_floor(8) + 3 #-0.14159265  \n    print(n, n.round_bank(7))           #-0.1415926  \n\nPERFORMANCE EVALUATION AND SQUARENESS \u003e\u003e\u003e  \n\t\n\tfrom num7 import Num  \n\tfrom time import perf_counter  \t\n \n\ttic = perf_counter() #Start Time  \n\ta = Num('-1.123456789'+'e-100')      #calculating division 10**100...  \n\ttoc = perf_counter() #End Time  \n\tT1 = toc - tic  \n\tprint(f\"a finished sec. {T1:1.6f}\") #a finished sec. 0.000062\n \n\ttic = perf_counter() #Start Time  \n\tb = ('-1.123456789') \u003e\u003e Num('100.0') #calculating division 10**100...  \n\ttoc = perf_counter() #End Time  \n\tT2 = toc - tic  \n\tprint(f\"b finished sec. {T2:1.6f}\") #b finished sec. 0.000341\n \n\tR = Num.f_perf_time(str(T1), str(T2))  \n\tprint('PCT=\u003e', R[0].round(), 'SCALE=\u003e', R[1].round(), 'SQUARENESS=\u003e', a == b) #PCT=\u003e -81.87 SCALE=\u003e -4.52 SQUARENESS=\u003e True \n \n\t#stock exchange assets performance  \n \tprevious = Num('26.96'); now = Num('27.27')  \n\tvar_pct = Num.f_perf(previous, now).round()  \n   \tprint(f'{float(var_pct):+.2f}') #+1.15\n\nSCIENTIFIC NOTATION AND HIGH PRECISION RESULTS \u003e\u003e\u003e\n\n\tfrom num7 import Num    \n\ta = Num('1_000_000_000_000_000_000_000.0') #standard notation  \n\tb = Num('1e21') #scientific notation  \n\tSUM = a + b     #SUM  \n\tieee754 = float(a)+float(b)  \n\tprint('SUM == ieee754', SUM == Num(str(ieee754)), ' SUM =\u003e', SUM.num2exp()) #SUM == ieee754 True  SUM =\u003e 2.0e21  \n\t\n\ta = Num('1_000_000_000_000_000_000_000.0') #standard notation  \n\tb = Num('1e21') #scientific notation  \n\tMUL = a * b     #MUL  \n\tieee754 = float(a)*float(b)  \n\tprint('MUL == ieee754', MUL == Num(str(ieee754)), ' MUL =\u003e', MUL.num2exp()) #MUL == ieee754 True  MUL =\u003e 1.0e42  \n\t\n\ta = '1.23456789'  \n\tb = '9.87654321'  \n\tMUL = Num(a) * Num(b) #MUL  \n\tieee754 = float(a)*float(b)  \n\tprint('MUL == ieee754', MUL == Num(str(ieee754)), 'MUL =\u003e', MUL, float(a)*float(b), '=\u003e IEEE754 PRECISION FAILURE!') #MUL == ieee754 False MUL =\u003e 12.1932631112635269 12.193263111263525 =\u003e IEEE754 PRECISION FAILURE!  \n\t\n\ta = '1.23456789e320'  #scientific notation  \n\tb = '9.87654321e320'  \n\tMUL = Num(a) * Num(b) #MUL  \n\tieee754 = float(a)*float(b)  \n\tprint('MUL == ieee754', MUL.str() == str(ieee754), 'MUL =\u003e', MUL.num2exp(), float(a)*float(b), '=\u003e IEEE754 inf FAILURE!') #MUL == ieee754 False MUL =\u003e 1.21932631112635269e641 inf =\u003e IEEE754 inf FAILURE!  \n\t\n\ta = '2e320' #scientific notation  \n\tb = '3e-320'  \n\tMUL = Num(a) * Num(b) #MUL  \n\tieee754 = float(a)*float(b)  \n\tprint('MUL == ieee754', MUL.str() == str(ieee754), 'MUL =\u003e', MUL.num2exp(), ieee754, '=\u003e IEEE754 inf FAILURE!') #MUL == ieee754 False MUL =\u003e 6.0 inf =\u003e IEEE754 inf FAILURE!  \n\t\n\ta = '1e200' #scientific notation  \n\tb = '5e1200'  \n\tT1 = Num(a, 1200) #ultra precision (over 80 digits default) floating point division must be specified!  \n\tT2 = Num(b)  \n\tDIV = T1 / T2 #DIV  \n\tieee754 = float(a)/float(b)  \n\tprint('DIV == ieee754', DIV.str() == str(ieee754), 'DIV =\u003e', DIV.num2exp(), ieee754, '=\u003e IEEE754 precision FAILURE!') #DIV == ieee754 False DIV =\u003e 2.0e-1001 0.0 =\u003e IEEE754 precision FAILURE!  \n\nFLOAT TO NUM CONVERSION LIST \u003e\u003e\u003e\n\n\tfrom num7 import Num  \n\tL = [1011, 0.0, 9.998412, 7.0, 0.123, -2.0123, 10, 6]\n\tLN= Num.float2num_list(L)\n\tprint(list(i.n for i in LN)) #['1011.0', '0.0', '9.998412', '7.0', '0.123', '-2.0123', '10.0', '6.0']\n\tprint(list(i for i in LN))   #[Num('1011.0'), Num('0.0'), Num('9.998412'), Num('7.0'), Num('0.123'), Num('-2.0123'), Num('10.0'), Num('6.0')]\n\nSAVE NUMERIC LIST TO DISK FILE \u003e\u003e\u003e\n\n\tNum.f_filewrite(L) #\n\nREAD NUMERIC LIST FROM DISK FILE (nums.txt default filename) \u003e\u003e\u003e\n\n\tL = Num.f_fileread(); print(L) #[Num('1011.0'), Num('0.0'), Num('9.998412'), Num('7.0'), Num('0.123'), Num('-2.0123'), Num('10.0'), Num('6.0')]\n\n### FAQ \n\nQ. I usually try to add 0.1 to 0.2 in python3 with this code:  \n\n\t\u003e\u003e\u003e print(0.1 + 0.2)  \nand the result is:  \n\n\t\u003e\u003e\u003e 0.30000000000000004  \n\t\nHow instead can it gets exactly 0.3?  \nA. Using Num class \u003e\u003e\u003e  \n\n\tfrom num7 import Num, Num as calc  \n\tprint(Num('0.1') + Num('0.2'))  #calc.add('0.1', '0.2') #0.3  \n\nQ. I'll get an error when i usually type:  \n\t\n\t\u003e\u003e\u003e Num(0.1)    \n \n\tTraceback (most recent call last):  \n\tFile \"\u003cpyshell\u003e\", line 1, in \u003cmodule\u003e  \n\tFile \"C:\\Users\\pincopallino\\mydata\\Python\\Python310\\lib\\site-packages\\num7.py\", line 470, in __init__  \n\t\traise ValueError(F\"Num.__init__ =\u003e float, type not valid: {n}\")  \n\tValueError: Num.__init__ =\u003e float, type not valid: 1.0\t\n\t\nWhat is wrong?  \nA. You must use quotes or string conversion with built-in str function:\n\n\t\u003e\u003e\u003e from num7 import Num   \n\t\u003e\u003e\u003e Num('0.1')    #Num('0.1')  \n\t\u003e\u003e\u003e Num(str(0.1)) #Num('0.1')  \n\nQ. How can i convert a regular float to a Decimal?  \nA. With Num.ieee754() method \u003e\u003e\u003e  \n\n\tfrom num7 import Num, Num as calc  \n\ta=0.1; b=0.2;  \n\tc=a+b                              #0.30000000000000004 =\u003e PRECISION FAILURE!  \n\tan = Num.ieee754(a); print(an)     #0.1000000000000000055511151231257827021181583404541015625  \n\tbn = Num.ieee754(b); print(bn)     #0.200000000000000011102230246251565404236316680908203125  \n\tcn = Num.ieee754(a+b);  \n\tprint(cn, '=\u003e PRECISION FAILURE!') #0.3000000000000000444089209850062616169452667236328125 =\u003e PRECISION FAILURE!  \n\tT = calc.add(an, bn)  \n\tprint(T, '=\u003e OK.')                 #0.3000000000000000166533453693773481063544750213623046875 =\u003e OK.  \n\nQ. I have two float variables in my code:  \n\n\t\u003e\u003e\u003e a = 0.1; b = 0.2  \n\t\nHow can i convert them in Num type?  \nA. With Num.float2num method (or directly with str() built-in function) \u003e\u003e\u003e  \n\n\tfrom num7 import Num  \n\ta = 0.1; b = 0.2 #  \n\tan= Num.float2num(a); bn= Num.float2num(b)        #an= Num(str(a)); bn= Num(str(b))  \n\tprint(an+bn, 'OK. VS', a+b, 'PRECISION FAILURE!') #0.3 OK. VS 0.30000000000000004 PRECISION FAILURE!  \n\nQ. Can i do add or other math operations also with 10,000 digits after floating point?  \nA. Yes, you can. \u003e\u003e\u003e\n\n\tfrom num7 import Num  \n\tprint((Num('1.123456789e-10_000') + Num('3.987654321e-10_000')).num2exp()) #5.11111111e-10000  \n\tprint((Num('1.123456789e-10_000') - Num('3.987654321e-10_000')).num2exp()) #-2.864197532e-10000  \n\tprint((Num('1.123456789e-10_000') * Num('3.987654321e-10_000')).num2exp()) #4.479957319112635269e-20000  \n\tprint((Num('1.123456789e-10_000') / Num('3.987654321e-10_000'))) #0.281... (10011 digits)\n\t\nQ. With Python 3.11 it gets an error when running code with digits thousands \u003e\u003e\u003e  \n\n\tfrom num7 import Num  \n\tprint((Num('1.123456789e-10_000') + Num('3.987654321e-10_000')).num2exp()) #5.11111111e-10000  \n\t\n\tValueError: Exceeds the limit (4300) for integer string conversion: value has 10010 digits; use sys.set_int_max_str_digits() to increase the limit  \n\t\nHow can i fix it?  \nA. Set the max string digits allowed in this way \u003e\u003e\u003e  \n\n\tfrom num7 import Num  \n\timport sys  \n\tsys.set_int_max_str_digits(1_000_000) #1_000_000 str digits set \n\tprint((Num('1.123456789e-10_000') + Num('3.987654321e-10_000')).num2exp()) #5.11111111e-10000 \n\nQ. I must enter many integer variables in my code:  \n\n\t\u003e\u003e\u003e a = Num('123.0'); b = Num('456.0'); c = Num('789.0')\n\t\nCan i input them without quotes and suffix .0?  \nA. Yes, this the way:\n\n\t\u003e\u003e\u003e a = Num(123); b = Num(456); c = Num(789)  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiocip%2Fnum7","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiocip%2Fnum7","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiocip%2Fnum7/lists"}