Jump to content

Recommended Posts

Melba23

Ascer,

Not an AutoIt problem, but one that affects all computers that do not have a specialist maths app/CPU. This has been discussed many, many times - search the forum for "floating point arithmetic".

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Ascer

@Melba23

Thans for advice but this is important problem in my opinion

Share this post


Link to post
Share on other sites
Melba23

Ascer,

I agree, but it is beyond AutoIt to do anything about it. Did you read anything about floating point arithmetic? it is a product of the way all computers store real numbers (i.e. non-integers) and, as I mentioned above, requires a special maths program or CPU do deal with it correctly.

In your case, just convert everything to integers:

$a = 3.99

$a = $a * 100
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

$a -= 300
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

$a -= 99
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

$a = $a/100
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

M23

Edited by Melba23
Added link

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Bowmore

One way of dealing with this issue is to round the final result or before doing any comparisons to a sensible number of decimal places.

 

$a = 3.99 - 3
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

$a *= 100
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

$a -= 99
ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)
ConsoleWrite(VarGetType($a) & ": " & Round($a,12) & @CRLF)

If $a = 0 Then
    ConsoleWrite(VarGetType($a) & ": $a = zero is True" & @CRLF)
Else
    ConsoleWrite(VarGetType($a) & ": $a = zero is False" & @CRLF)
EndIf

If Round($a,12) = 0 Then
    ConsoleWrite(VarGetType($a) & ": $a = zero is True" & @CRLF)
Else
    ConsoleWrite(VarGetType($a) & ": $a = zero is False" & @CRLF)
EndIf

 


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
LarsJ

It's not an AutoIt problem, but we still can do something about it by using variants of type decimal:

#include "Variant.au3"

Opt( "MustDeclareVars", 1 )

Example1()
Example2()

Func Example1()
  ConsoleWrite( "Example1" & @CRLF )

  Local $a = 3.99 - 3
  ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

  $a *= 100
  ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)

  $a -= 99
  ConsoleWrite(VarGetType($a) & ": " & $a & @CRLF)
EndFunc

Func Example2()
  ConsoleWrite( @CRLF & "Example2" & @CRLF )

  Local $a = "3.99", $b = "3", $c = "100", $d = "99"
  ; The strings ensures that the numbers are not converted to floats

  Local $ta = StrToDec( $a ), $pa = DllStructGetPtr( $ta )
  Local $tb = StrToDec( $b ), $pb = DllStructGetPtr( $tb )
  Local $tc = StrToDec( $c ), $pc = DllStructGetPtr( $tc )
  Local $td = StrToDec( $d ), $pd = DllStructGetPtr( $td )

  VarDecSub( $pa, $pb, $pa ) ; $a = $a - $b = 3.99 -   3
  $a = DecToStr( $pa )
  ConsoleWrite( $a & @CRLF )
  VarDecMul( $pa, $pc, $pa ) ; $a = $a * $c =  $a  * 100
  $a = DecToStr( $pa )
  ConsoleWrite( $a & @CRLF )
  VarDecSub( $pa, $pd, $pa ) ; $a = $a - $d =  $a  -  99
  $a = DecToStr( $pa )
  ConsoleWrite( $a & @CRLF )
EndFunc

Func StrToDec( $str )
  Local $tStr = DllStructCreate( $tagVARIANT )
  Local $pStr = DllStructGetPtr( $tStr )
  $tStr.vt   = $VT_BSTR
  $tStr.data = SysAllocString( $str )
  VariantChangeType( $pStr, $pStr, 0, $VT_DECIMAL )
  Local $tDec = DllStructCreate( $tagDEC, $pStr )
  Local $l = StringInStr( $str, "." )
  $tDec.scale = $l ? StringLen( $str ) - $l : 0
  Return $tStr
EndFunc

Func VarDecAdd( $pDecLeft, $pDecRight, $pDecResult )
  Local $aRet = DllCall( "OleAut32.dll", "long", "VarDecAdd", "ptr", $pDecLeft, "ptr", $pDecRight, "ptr", $pDecResult )
  If @error Then Return SetError(1,0,1)
  Return $aRet[0]
EndFunc

Func VarDecDiv( $pDecLeft, $pDecRight, $pDecResult )
  Local $aRet = DllCall( "OleAut32.dll", "long", "VarDecDiv", "ptr", $pDecLeft, "ptr", $pDecRight, "ptr", $pDecResult )
  If @error Then Return SetError(1,0,1)
  Return $aRet[0]
EndFunc

Func VarDecMul( $pDecLeft, $pDecRight, $pDecResult )
  Local $aRet = DllCall( "OleAut32.dll", "long", "VarDecMul", "ptr", $pDecLeft, "ptr", $pDecRight, "ptr", $pDecResult )
  If @error Then Return SetError(1,0,1)
  Return $aRet[0]
EndFunc

Func VarDecSub( $pDecLeft, $pDecRight, $pDecResult )
  Local $aRet = DllCall( "OleAut32.dll", "long", "VarDecSub", "ptr", $pDecLeft, "ptr", $pDecRight, "ptr", $pDecResult )
  If @error Then Return SetError(1,0,1)
  Return $aRet[0]
EndFunc

Func DecToStr( $pDec )
  Local $tStr = DllStructCreate( $tagVARIANT )
  VariantChangeType( DllStructGetPtr( $tStr ), $pDec, 0, $VT_BSTR )
  Return SysReadString( $tStr.data )
EndFunc

You can get Variant.au3 in this post.

Share this post


Link to post
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

  • Similar Content

    • Simpel
      By Simpel
      Hi,
      I wondered why negative integers I wrote into registry (e.g. negative x-coordinates of a gui if using two monitors and the right one is the main one) wouldn't return right when reading. Now I know: it is saved as an unsigned integer (without algebraic sign). So here is a snippet that is changing unsigned to signed integer:
      Global Const $g_sRegKey = "HKEY_CURRENT_USER\Software\" & @ScriptName ; path to registry RegWrite($g_sRegKey, "Value", "REG_DWORD", -2147483647) ; write some negative integer into registry; -2147483647 highest possible negative integer , 2147483648 highest possible positive integer if talking of 32bit Local $sValue = RegRead($g_sRegKey, "Value") ; read out registry ConsoleWrite("Value: " & $sValue & @CRLF) ; show real value in console Local $sResult = _SignedInteger($sValue) ; change to signed value ConsoleWrite("Result: " & $sResult & @CRLF) ; and show it in console Func _SignedInteger($iUnsignedInteger) Local $iSignedInteger If $iUnsignedInteger > (2^31) Then ; then it means a negative integer $iSignedInteger = $iUnsignedInteger - (2^32) Else $iSignedInteger = $iUnsignedInteger EndIf Return $iSignedInteger EndFunc It took me some time to find out the problem and so I hope I can help somebody with this.
      Regards, Conrad
    • Rhazz
      By Rhazz
      Hello again, my second post in 24 hs!
      It's my first GUI... and my first progress bar also! Sorry if I make a newbie mistake.
      I want to configure a GUI Progress Bar by a double "for" loop.
      Here is my code with a specific annotation with what I want to do:
      Edit: new problem, I have no idea how can I continue with the next $a as if the current $a, I do not skip it because the line is an element less (I need help with both problems):
      #include <GUIConstantsEx.au3> #include <EditConstants.au3> #include <WinAPI.au3> #include <File.au3> #include <String.au3> #include <Array.au3> Local $idGUI = GUICreate("ProgressBar", 220, 130, 100, 200) Opt("GUIOnEventMode", 1) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton") Local $idInput = GUICtrlCreateInput("",80,20,60,20,$ES_NUMBER) Local $idProgressBar = GUICtrlCreateProgress(10, 60, 200, 20) Local $idButton1 = GUICtrlCreateButton("Start",85,100,50) GUICtrlSetOnEvent($idButton1, "StartProgressBar") Local $aFileOpen = _WinAPI_GetOpenFileName("Open a text file", "Text Files (*.txt)") Local $idCountLines = _FileCountLines($aFileOpen[2]) Local $idInputValue, $idSomething, $aFileResult, $idStringBetween GUISetState(@SW_SHOW, $idGUI) While 1 Sleep(20000) WEnd Func StartProgressBar() If StringInStr($aFileOpen[2], ".txt") = True Then $idInputValue = Int(GUICtrlRead($idInput)) If IsFloat($idCountLines/$idInputValue) = 1 Then $idStringBetween = _StringBetween(String($idCountLines/$idInputValue),"", ".") $idSomething = Number($idStringBetween[0]) + 1 Else $idSomething = $idCountLines/$idInputValue EndIf For $i = 1 To $idSomething $aFileResult = @ScriptDir & "\result-" & $i & ".txt" _FileCreate($aFileResult) FileOpen($aFileResult,2) For $a = ( ( ( $i - 1 ) * $idInputValue ) + 1 ) To ( $i * $idInputValue ) If FileReadLine($aFileOpen[2], $a) = "" Then ; Here I have no idea how can I continue with the next $a as if the current $a, I do not skip it because the line is an element less EndIf FileWrite($aFileResult,FileReadLine($aFileOpen[2], $a) & " - ") ;Here I want to set the progress bar value = current percentage (line that it is currently reading) of the 100% (total of lines in the file opened) Next FileClose($aFileResult) Next MsgBox(0,"Done","Done") GUICtrlSetData($idProgressBar,0) Else MsgBox(0,"Hello :)","Please open a file :D") EndIf EndFunc Func CLOSEButton() Exit EndFunc  
      Thanks in advance.
       
    • Flum
      By Flum
      Good morning community,
      I wrote an Autoit script with a double Gui.The button in the second gui wont work any ideas? Much appriciated
      #include <ButtonConstants.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) Global $hGUI, $hGUI2 vertical() func vertical() $hGUI = GUICreate("Medication", 60, 376, -1, -1, $WS_POPUP) GUISetBkColor(0x000000) $rotatebutton = GUICtrlCreateButton("", 20, 0, 16, 16) GUICtrlSetOnEvent($rotatebutton, "rotate") GUISetState(@SW_SHOW) While 1 Sleep(100) WEnd endfunc func horizontal() $hGUI2 = GUICreate("Medication", 376, 60, -1, -1, $WS_POPUP) GUISetBkColor(0x000000) $rotatebackbutton = GUICtrlCreateButton("", 20, 0, 16, 16) GUICtrlSetOnEvent($rotatebackbutton, "rotateback") GUISetState(@SW_SHOW) While 1 Sleep(100) WEnd endfunc func rotate() msgbox(0,"ROTATE","ROTATE") GUISetState(@SW_HIDE, $hGUI) horizontal() EndFunc func rotateback() msgbox(0,"ROTATE BACK","ROTATE BACK") ; NEVER GETS TO THIS POINT? GUISetState(@SW_HIDE, $hGUI2) EndFunc
    • twitchyliquid64
      By twitchyliquid64
      Hi all,

      I am presently creating a program in C++, and part of this program uses the WINAPI QueryPerformanceCounter() function to work out the time taken for certain events.

      The issue I am having is when I convert the double that my calculation gives me into a string, using


      sprintf(tempbuff, "%.15g", tok->fValue);
      When in the 'Debug' solution Config, it works perfectly well, producing numbers like 1.8736523754e+006

      However, when in the release solution Config, it will produce something completely wrong:
      1873652.3754...

      What is causing this? How can I fix the problem? What is the difference between the two configs anyway?

      Thankyou in advance.
×