kaisies

[solved] Stupid addition question RE: Datatypes

7 posts in this topic

#1 ·  Posted (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 by kaisies

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Xandy

I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

#4 ·  Posted

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!

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by benners
1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted

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


Link to post
Share on other sites

#7 ·  Posted

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

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