Jump to content

Simple calculator which get (),n,p,m,u,K,M,G (nano, pico, ...) - (Moved)


Go to solution Solved by jchd,

Recommended Posts

Hi

I don't find some simple calculator which support n, p, ... and doing several commands with ().
I think to take some simplest ready calculator and add option to get n,p,..K,G,... 
All calculators I found were created before many years and I get so many errors there like "redicrarate constant" or "duplicated functions".

Please advise.
 

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate AutoIt General Help and Support forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I really don't understand what you're looking for. Maybe it's something that "if you know you know", but your request doesn't make sense to me. Could you try explaining in more detail what you're looking for? Since you mentioned you did find some, can you link things that you looked at previously (but didn't work), so we can get an idea of what you're looking for?

I think the closest I can get to interpreting your request is:

Quote

For example, if you were to input the expression "3.5K(6.2 + 4.8u) / 2m", the calculator should be able to interpret it correctly as "3.5 kilo * (6.2 + 4.8 micro) / 2 milli" and return the correct result. So the inclusion of these prefixes allows for the calculator to handle very large or very small numbers that would otherwise require many digits to represent.

Is this what you mean? Either way, I think there needs to be more explanation of your request.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

2 hours ago, mistersquirrle said:

I really don't understand what you're looking for. Maybe it's something that "if you know you know", but your request doesn't make sense to me. Could you try explaining in more detail what you're looking for? Since you mentioned you did find some, can you link things that you looked at previously (but didn't work), so we can get an idea of what you're looking for?

I think the closest I can get to interpreting your request is:

Is this what you mean? Either way, I think there needs to be more explanation of your request.

It is exactly what I meant .

Link to comment
Share on other sites

  • Solution

EDIT: code is faulty. Use code from @mistersquirrle below!

Parsing a numérical expression and computing its numerical value while interpreting standard multiplier acronyms isn't hard:

Local $sExpr = "1.7G +3.5k*(6.2 + 3d - 2a + 4.8µ - 4da) / 2m"

_Normalize($sExpr)
ConsoleWrite($sExpr & @LF)
Local $nRes = Execute($sExpr)
ConsoleWrite($nRes & @LF)

Func _Normalize(ByRef $s)
    Local Static $aSuffixes = [ _
        ["y", -24], ["z", -21], ["(?<!d)a", -18], ["f", -15], ["p", -12], ["n", -9], ["µ", -6], ["m", -3], ["c", -2], ["d(?!a)", -1], _
        ["da", 1], ["h", 2], ["k", 3], ["M", 6], ["G", 9], ["T", 12], ["P", 15], ["E", 18], ["Z", 21], ["Y", 24] _
    ]
    For $i = 0 To UBound($aSuffixes) - 1
        $s = StringRegExpReplace($s, $aSuffixes[$i][0], "*1e" & $aSuffixes[$i][1])
    Next
EndFunc

Notice that kilo is k (lowercase) and micro is µ not u.

EDIT: added centi, deci, deca and hecto suffixes.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

For completion, the various multiplier suffixes are named:

y = yocto
z = zepto
a = atto
f = femto
p = pico
n = nano
µ = micro
m = milli
c = centi
d = deci

da = Deca
h = hecto
k = kilo
M = Mega
G = Giga
T = Tera
P = Peta
E = Exa
Z = Zetta
Y = yotta

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thank you very much.


I wrote a function which convert the result to number <1000 + prefix

 

Func _GetDisplaySize($bytes, $outputPlaces = 4)
    $outputUnit = -5
    Local $outputMax
    Local $outputMin
    Local $unitNames[12] =['a' ,'f' ,'p', 'n', 'u' , 'm' , '', 'K' , 'M', ' G', ' T' , 'P']
    If $outputUnit < 0 Then
        $outputMax = Abs($outputUnit)
        $outputMin = -1*Abs($outputUnit)
        $outputUnit = Int(Log($bytes)/Log(1000))    
        If  $outputUnit > $outputMax Then $outputUnit = $outputMax
        If  $outputUnit < $outputMin Then $outputUnit = $outputMin  
        If $outputUnit < 0 Then  $outputUnit =$outputUnit - 1       
    EndIf

    $nRes1 =  String(Round($bytes / 1000 ^ $outputUnit, $outputPlaces)) & $unitNames[$outputUnit+6] 

    $outputUnit = $outputUnit+1
    If  $outputUnit > $outputMax Then $outputUnit = $outputMax
    If  $outputUnit <= $outputMin Then $outputUnit = $outputMin-1
    $nRes2 =  String(Round($bytes / 1000 ^ $outputUnit, $outputPlaces)) & $unitNames[$outputUnit+6] 
    return $nRes1 &  @CRLF  &'                       or '  &  @CRLF  & $nRes2   
EndFunc

 

Link to comment
Share on other sites

Why I get this error ? 
Local $fLog2 = Log10(1000)
Local $fLog2 = ^ ERROR
>Exit code: 1

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
 

Link to comment
Share on other sites

3 minutes ago, Jacov said:

Why I get this error ? 

You're likely getting it because Log10 is not an AutoIt function, so unless you wrote it, it just doesn't exist. I recommend that you post your entire code, or at least enough to fully show the problem. Also would be a good idea if you included the entire console/error output. It's pretty easy for me to see with what you provided what the problem is:

error: Log10(): undefined function.
Local $fLog2 = Log10(1000)
~~~~~~~~~~~~~~~~~~~~~~~~~~^

 

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

@jchd I think that your original script has some issues in generating the correct result. Check out this update:
 

Local $sExpr = "1.7G +3.5k*(6.2 + 3d - 2a + 4.8µ - 4da) / 2m"

ConsoleWrite($sExpr & @CRLF)
ConsoleWrite(_Normalize($sExpr) & @CRLF)
Local $nRes = Execute(_Normalize($sExpr))
ConsoleWrite($nRes & @CRLF)

Func _Normalize($sExpression)
    ; https://www.nist.gov/pml/owm/metric-si-prefixes
    Local Enum $eSI_REGEX, $eSI_UNIT, $eSI_FACTOR, $eSI_NAME, $eSI_NUMNAME, $eSI_MAX
    Local Static $aSuffixes = [ _
            ['q', 'q', -30, 'quecto', 'nonillionth'], _
            ['r', 'r', -27, 'ronto', 'octillionth'], _
            ['y', 'y', -24, 'yocto', 'septillionth'], _
            ['z', 'z', -21, 'zepto', 'sextillionth'], _
            ['(?<!d)a', 'a', -18, 'atto', 'quintillionth'], _
            ['f', 'f', -15, 'femto', 'quadrillionth'], _
            ['p', 'p', -12, 'pico', 'trillionth'], _
            ['n', 'n', -9, 'nano', 'billionth'], _
            ['µ', 'µ', -6, 'micro', 'millionth'], _
            ['m', 'm', -3, 'milli', 'thousandth'], _
            ['c', 'c', -2, 'centi', 'hundredth'], _
            ['d(?!a)', 'd', -1, 'deci', 'tenth'], _
            ['da', 'da', 1, 'deca', 'ten'], _
            ['h', 'h', 2, 'hecto', 'hundred'], _
            ['k', 'k', 3, 'kilo', 'thousand'], _
            ['M', 'M', 6, 'mega', 'million'], _
            ['G', 'G', 9, 'giga', 'billion'], _
            ['T', 'T', 12, 'tera', 'trillion'], _
            ['P', 'P', 15, 'peta', 'quadrillion'], _
            ['E', 'E', 18, 'exa', 'quintillion'], _
            ['Z', 'Z', 21, 'zetta', 'sextillion'], _
            ['Y', 'Y', 24, 'yotta', 'septillion'], _
            ['R', 'R', 27, 'ronna', 'octillion'], _
            ['Q', 'Q', 30, 'quetta', 'nonillion'] _
            ]
    Local $sPattern = ''

    For $iSuffix = 0 To UBound($aSuffixes) - 1
        $sPattern = '([\d.\-]+)' & $aSuffixes[$iSuffix][$eSI_REGEX]
;~      $sExpression = StringRegExpReplace($sExpression, $sPattern, "($1*10^" & $aSuffixes[$iSuffix][$eSI_FACTOR] & ')')
        $sExpression = StringRegExpReplace($sExpression, $sPattern, "($1*1e" & $aSuffixes[$iSuffix][$eSI_FACTOR] & ')')
        If @error Then
            ConsoleWrite('Error with RegEx: ' & $sPattern & _
                    ', @error: ' & @error & _
                    ', @extended: ' & @extended & @CRLF)
        EndIf
;~      If @extended > 0 Then
;~          ConsoleWrite('Processed SI suffix: ' & $aSuffixes[$iSuffix][$eSI_UNIT] & ', ' & $aSuffixes[$iSuffix][$eSI_NAME] & @CRLF)
;~      EndIf
    Next

    Return $sExpression
EndFunc   ;==>_Normalize

Your original version output this final result: 

1.7*1e9 +3.5*1e3*(6.2 + 3*1e-1 - 2*1e-18 + 4.8*1e-6 - 4*1e1) / 2*1e-3
1699999941.37501

 

This update outputs:

(1.7*1e9) +(3.5*1e3)*(6.2 + (3*1e-1) - (2*1e-18) + (4.8*1e-6) - (4*1e1)) / (2*1e-3)
1641375008.4

Or you can switch the $s = lines and get:

(1.7*10^9) +(3.5*10^3)*(6.2 + (3*10^-1) - (2*10^-18) + (4.8*10^-6) - (4*10^1)) / (2*10^-3)
1641375008.4

 

I'm not 100% sure what the correct answer is supposed to be, but filling in the values manually and running it through a calculator shows 1641375008.4. So if I'm wrong, let me know why, otherwise you may want to update your script. I also added some more information about the SI suffixes

Edited by mistersquirrle
Updated code

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

2 hours ago, mistersquirrle said:

You're likely getting it because Log10 is not an AutoIt function

There it has example with Log10 and I tried it and it ran.
https://www.autoitscript.com/autoit3/docs/functions/Log.htm

 

I just did convert to other basis of log  : Log_a(b) = Log_c(b)/Log_c(a)

It also was my mistake in first version of converter output , I used LOG , which with has basis "e".

Link to comment
Share on other sites

3 hours ago, mistersquirrle said:

You're likely getting it because Log10 is not an AutoIt function

There it has example with Log10 and I tried it and it ran.
https://www.autoitscript.com/autoit3/docs/functions/Log.htm

 

I just did convert to other basis

It also was my mistake in first version of converter output , I used LOG , which with has basis "e".

 

 

 

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Global $hGUI = GUICreate("Pico Shmiko calculator", 789, 235, 760, 365)
Global $Input1 = GuiCtrlCreateInput("1.7G +3.5k*(6.2 + 4.8µ) / 2m", 10, 10, 761, 41)
Global $BtnOk = GUICtrlCreateButton("Press the button", 190, 70, 131, 71)
Global $Input2 = GuiCtrlCreateEdit("phhhh", 10, 70, 140, 70,$ES_WANTRETURN)
Local $accelerators[1][2]
    $accelerators[0][0] = "{ENTER}"
    $accelerators[0][1] = $Input1

_main()

Func _main()
GUISetAccelerators($accelerators)
GUISetState(@SW_SHOWNORMAL)
While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
        ExitLoop
    Case $BtnOk OR $Input1
        $sExpr = GUICtrlRead($Input1)
        _Normalize($sExpr)
        ConsoleWrite($sExpr & @LF)
        Local $nRes = Execute($sExpr)
        ConsoleWrite($nRes & @LF)
        $nRes = _GetDisplaySize($nRes,5) 
        GUICtrlSetData($Input2, $nRes)
    Case Else           
    EndSwitch
WEnd
EndFunc 

Func _GetDisplaySize($bytes, $outputPlaces = 4)
    $outputUnit = -6
    Local $outputMax
    Local $outputMin
    Local $unitNames[12] =['a' ,'f' ,'p', 'n', 'u' , 'm' , '', 'K' , 'M', ' G', ' T' , 'P']
    If $outputUnit < 0 Then
        $outputMax = Abs($outputUnit)
        $outputMin = -1*Abs($outputUnit)
        $outputUnit = Floor(Log($bytes)/Log(10)/3)  
        ;ConsoleWrite($outputUnit & @LF)
        If  $outputUnit > $outputMax-1 Then $outputUnit = $outputMax
        If  $outputUnit < $outputMin Then $outputUnit = $outputMin  
        If $outputUnit < 0 Then  $outputUnit =$outputUnit 
    EndIf
    ;ConsoleWrite($outputUnit & @LF)
    $nRes1 =  String(Round($bytes / 1000 ^ $outputUnit, $outputPlaces)) & $unitNames[$outputUnit+6] 

    $outputUnit = $outputUnit+1
    ConsoleWrite($outputUnit & @LF)
    If  $outputUnit >= $outputMax Then $outputUnit = $outputMax-1
    If  $outputUnit <= $outputMin Then $outputUnit = $outputMin-1
         ConsoleWrite($outputUnit & @LF)
    $nRes2 =  String(Round($bytes / 1000 ^ $outputUnit, $outputPlaces)) & $unitNames[$outputUnit+6] 
    
    return $nRes1 &  @CRLF  &'                       or '  &  @CRLF  & $nRes2   
EndFunc


Func _Normalize(ByRef $s)
    Local Static $aPrefixes = [ _
        ["y", -24], ["z", -21], ["(?<!d)a", -18], ["f", -15], ["p", -12], ["n", -9], ["u", -6], ["m", -3], ["c", -2], ["d(?!a)", -1], _
        ["da", 1], ["h", 2], ["k", 3], ["K", 3], ["M", 6], ["G", 9], ["T", 12], ["P", 15], ["E", 18], ["Z", 21], ["Y", 24] _
    ]
    For $i = 0 To UBound($aPrefixes) - 1
        $s = StringRegExpReplace($s, $aPrefixes[$i][0], "*1e" & $aPrefixes[$i][1])
    Next
EndFunc

 

Link to comment
Share on other sites

22 minutes ago, Jacov said:

There it has example with Log10 and I tried it and it ran.
https://www.autoitscript.com/autoit3/docs/functions/Log.htm

Yes, that example they are specifically creating the Log10 function. It is not a built-in AutoIt function, so if you want to use it, you need to copy/paste it into your script:

; User-defined function for common log
Func Log10($fNb)
        Return Log($fNb) / Log(10) ; 10 is the base
EndFunc   ;==>Log10

 

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

On 4/3/2023 at 11:17 PM, mistersquirrle said:

@jchd I think that your original script has some issues in generating the correct result.

My bad, you're correct.  It was very late when I posted and suspected 1.5e3*(7 - 2) could result in 1.3e(3*5) but after trying it yielded the correct result (1.5e3) * 5.

What I completely overlooked was that (1 + 2) / 2m would give (1+2) / 2 * 1e-3 that is (being evaluated left to right) ((1 + 2) / 2) * (1e-3).

So indeed parenthetising the normalized values is required, like your post demonstrates.

Also thanks for pointing out the names for 10⁻³⁰,10⁻²⁷,10²⁷,10³⁰ which I ignored. They have only been introduced in 2022, years after I built my SQLite Units table.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...