[solved] Stupid addition question RE: Datatypes

Recommended Posts

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
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 by kaisies

Share on other sites

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 by Xandy

Share on other sites

Maybe floats are strings.  I have no idea.

Share on other sites

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!

Share on other sites

== 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 by benners
• 1

Share on other sites
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.

Share on other sites

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

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

Create an account

Register a new account