kaisies Posted May 3, 2017 Share Posted May 3, 2017 (edited) I feel like I did not get enough sleep last night. Can someone remove my haze.. Why is $total greater than $num1 + $num2? Been staring at this for ~20 and my usual coding partner is gone for the day I feel like this is because autoit's fuzzy math with datatypes.. but.. ugh... $total = 11.74 $num1 = 9.54 $num2 = 2.2 ;2.20 also does not work here If $total = $num1 + $num2 Then msgbox(0,'',"Numbers equal after addition") EndIf If $total > ($num1 + $num2) Then Msgbox(0,'',"this has to be something related to variable type but I don't know why...") EndIf Edited May 3, 2017 by kaisies Link to comment Share on other sites More sharing options...
Xandy Posted May 3, 2017 Share Posted May 3, 2017 (edited) Simple fix and can't tell you 100% why but just add an additional '=' sign. If $total == $num1 + $num2 Then Above works for me Edited May 3, 2017 by Xandy Human Male Programmer (-_-) Xandy About (^o^) Discord - Xandy Programmer MapIt (Tile world editor, Image Tile Extractor, and Game Maker) Link to comment Share on other sites More sharing options...
Xandy Posted May 3, 2017 Share Posted May 3, 2017 Maybe floats are strings. I have no idea. Human Male Programmer (-_-) Xandy About (^o^) Discord - Xandy Programmer MapIt (Tile world editor, Image Tile Extractor, and Game Maker) Link to comment Share on other sites More sharing options...
kaisies Posted May 3, 2017 Author Share Posted May 3, 2017 Not sure myself. I've seen posts regarding some of the weird math results people get in autoit, but of course I can't find them now This is bizarre: $total = 11.74 $num1 = 10 $num2 = 1.74 ;~ $total = "11.74" ;~ $num1 = "9.54" ;~ $num2 = "2.2" ;2.20 also does not work here For $x = 1 to 200 If $total = $num1 + $num2 Then $sTmp = "Equal To" ElseIf $total > $num1 + $num2 Then $sTmp = "Greater Than" ElseIf $total < $num1 + $num2 Then $sTmp = "Less Than" Else $sTmp = "MATH ERROR!" EndIf ConsoleWrite("Total (" & $total & ") is " & $sTmp & " " & $num1 & "+" & $num2 & @CRLF) $num1 -= .01 $num2 += .01 Next But as soon as you change the line to If $total == $num1 + $num2 Then then at least the "math" works. This is part of a much larger project where its comparing 3 numbers in a loop, and if $total is greater than, an action is performed. I ended up throwing a If $total == $num1 + $num2 Then ContinueLoop Before the Greater than check, and at least this solves the problem I'm facing. Would still like to know more about this if anyone knows more? But thank you for the suggestion! Link to comment Share on other sites More sharing options...
benners Posted May 3, 2017 Share Posted May 3, 2017 (edited) == is used to test if two strings are equal (case sensitive). The numbers will be converted to strings so will match. I think it's a floating point thing with the OPs code. If I Round to 2 (or any) decimal places It works. $total = 11.74 $num1 = 9.54 $num2 = 2.2 ;2.20 also does not work here If Round($total, 2) = Round($num1 + $num2, 2) Then MsgBox(0, '', "Numbers equal after addition") EndIf If Round($total, 2) > Round($num1 + $num2, 2) Then Msgbox(0,'',"this has to be something related to variable type but I don't know why...") EndIf Edited May 3, 2017 by benners Xandy 1 Link to comment Share on other sites More sharing options...
kaisies Posted May 3, 2017 Author Share Posted May 3, 2017 23 minutes ago, benners said: == is used to test if two strings are equal (case sensitive). The numbers will be converted to strings so will match. I think it's a floating point thing with the OPs code. If I Round to 2 (or any) decimal places It works. $total = 11.74 $num1 = 9.54 $num2 = 2.2 ;2.20 also does not work here If Round($total, 2) = Round($num1 + $num2, 2) Then MsgBox(0, '', "Numbers equal after addition") EndIf If Round($total, 2) > Round($num1 + $num2, 2) Then Msgbox(0,'',"this has to be something related to variable type but I don't know why...") EndIf This is perfect. I'm dealing with currency, so this solves my issue exactly. Link to comment Share on other sites More sharing options...
kylomas Posted May 3, 2017 Share Posted May 3, 2017 It is a floating point issue. One common technique is to convert to whole number, do the math then divide by 100... $total = 11.74 $num1 = 9.54 $num2 = 2.2 ;2.20 also does not work here If $total = _addfloat($num1, $num2) Then msgbox(0,'',"Numbers equal after addition") EndIf If $total > _addfloat($num1, $num2) Then Msgbox(0,'',"this has to be something related to variable type but I don't know why...") EndIf func _addfloat($f1, $f2) return ( ($f1*100) + ($f2*100) ) / 100 endfunc kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now