Jump to content

Autoit Math is Incorrect?


Tumble
 Share

Recommended Posts

#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.

Link to comment
Share on other sites

  • Moderators

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

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 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.

Link to comment
Share on other sites

  • Moderators

Tumble,

M23

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

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

 

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...