Jump to content
Tumble

Autoit Math is Incorrect?

Recommended Posts

Tumble
#include <MsgBoxConstants.au3>

$var1 = 250000
$var2 = $var1 / 2500
$var3 = 250
MsgBox( $MB_SYSTEMMODAL, "", "$var1: " + $var1 ) ;Shows 250000
MsgBox( $MB_SYSTEMMODAL, "", "$var2: " + $var2 ) ;Shows 100
MsgBox( $MB_SYSTEMMODAL, "", "$var3: " + $var3 ) ;Shows 250
MsgBox( $MB_SYSTEMMODAL, "", "$var3/$var2: " + $var2/$var2 ) ;Shows 1

Here is my code snippet that is acting up. For some reason the variables go in correctly, but they don't come out correctly. Basically my program gets a value from a website, then tries to divide it by another value it gets from that website, and then finally divide it by a number from a database. All of my test values come from the website and my database just fine, it's just this little part of the code.

From some reason my code is showing my $var3/$var2 = 1 where it should give me 2.5. Can anyone explain to me why this is happening? It happens both in my actual code and in this little test snippet.

Share this post


Link to post
Share on other sites
Melba23

Tumble,

Welcome to the AutoIt forums.

It looks like you have fallen foul of the "floating point maths" problem - it affects all computers and is not an AutoIt problem per se. Search the forum and you will find many instances of people running into the same problem - and how to solve it.

M23

Edited by Melba23
28k

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
Tumble
MsgBox( $MB_SYSTEMMODAL, "", "$var3/$var2: " + $var3/$var2 ) ;Shows 253

I typed this line incorrectly in my test code.

However, I ended up restarting Scite and it started working... Not sure why it was giving me 253 for 250/100.

Share this post


Link to post
Share on other sites
Tumble

I can't find anything on the forums through the search bar. However it is strange to me that it works in my test code but when the same exact values go into my real code is when I experience issues. When I check the values they all show up how they should, but when I divide $var3/$var2 in my actual code I get something ridiculous.

Share this post


Link to post
Share on other sites
Melba23

Tumble,

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
Malkey
16 hours ago, Tumble said:
#include <MsgBoxConstants.au3>

$var1 = 250000
$var2 = $var1 / 2500
$var3 = 250
MsgBox( $MB_SYSTEMMODAL, "", "$var1: " + $var1 ) ;Shows 250000
MsgBox( $MB_SYSTEMMODAL, "", "$var2: " + $var2 ) ;Shows 100
MsgBox( $MB_SYSTEMMODAL, "", "$var3: " + $var3 ) ;Shows 250
MsgBox( $MB_SYSTEMMODAL, "", "$var3/$var2: " + $var2/$var2 ) ;Shows 1

....

From some reason my code is showing my $var3/$var2 = 1 where it should give me 2.5. Can anyone explain to me why this is happening? It happens both in my actual code and in this little test snippet.

I can explain why  your code is showing  $var3/$var2 = 1.

It's because your code reads  $var2/$var2,  which does equal 1.  And the arithmetic value of the string, "$var3/$var2:  " equals 0.

 

What you should have is:

#include <MsgBoxConstants.au3>

Local $var3 = 250
Local $var2 = 100

MsgBox($MB_SYSTEMMODAL, "", "$var3/$var2: " & $var3 / $var2) ;Shows $var3/$var2: 2.5

 

Share this post


Link to post
Share on other sites
mikell

Getting issues with such a creepy mix of strings/numbers/operators is not so surprising anyway  :unsure:

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

×