Jump to content

[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
    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 by kaisies
Link to comment
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 :D

 

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

== 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
Link to comment
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.

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

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