pluto41

Unpredictable result in simple calculation

6 posts in this topic

For fun i wrote a little program to find solutions to this puzzle: https://www.youtube.com/watch?v=WiB2_dXSSMg

I get 128 solutions in 18 minutes. (Windows XP, virtualized on a iMac). However there should be 136 solutions.

I simplified the problem. See code below. Does anyone know whats going wrong here.

#cs does compute
$a = 1

$b = 2
$c = 6

$d = 4

$e = 7

$f = 8

$g = 3
$h = 5

$i = 9

#ce / does compute



;#cs does not Compute

$a = 1

$b = 8

$c = 3

$d = 7

$e = 4

$f = 5

$g = 2

$h = 6

$i = 9

;#ce / does not Compute



Local $iSolution
$iSolution = ( $a + (13 * $b / $c) + $d + (12 * $e) - $f - 11 + ($g * $h / $i) - 10 )

Msgbox ( 0, "calculates to", $iSolution )


If $iSolution = 66 Then
    
MsgBox ( 0, "found = ", $iSolution )

Else
    
Msgbox ( 0, "not found", $iSolution )

EndIf

 

Share this post


Link to post
Share on other sites



After the calculation $iSolution is of type Double. It seems the calculation doesn't exactly return 66 (as an integer) but a floating point like 66.00000001 or whatever.
If you know that the result always is an integer then the following script works as expected:

#cs does compute
    $a = 1
    $b = 2
    $c = 6
    $d = 4
    $e = 7
    $f = 8
    $g = 3
    $h = 5
    $i = 9
#ce / does compute

;#cs does not Compute
$a = 1
$b = 8
$c = 3
$d = 7
$e = 4
$f = 5
$g = 2
$h = 6
$i = 9
;#ce / does not Compute

Local $iSolution, $iTargetValue = 66
$iSolution = ($a + (13 * $b / $c) + $d + (12 * $e) - $f - 11 + ($g * $h / $i) - 10)
$iSolution = Int($iSolution)
MsgBox(0, "calculates to", $iSolution)
If $iSolution = $iTargetValue Then
    MsgBox(0, "found = ", $iSolution)
Else
    MsgBox(0, "not found", $iSolution)
EndIf

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here you see that the exact value is 65.000000000000008579...

$a = 1
$b = 8
$c = 3
$d = 7
$e = 4
$f = 5
$g = 2
$h = 6
$i = 9
Local $iSolution = ($a + (13 * $b / $c) + $d + (12 * $e) - $f - 11 + ($g * $h / $i) - 10)
MsgBox(0, "Difference of $iSolution - 66",$iSolution - 66)
If $iSolution = 66 Then
    MsgBox(0, "found = ", $iSolution)
Else
    MsgBox(0, "not found", $iSolution)
EndIf

That's a result of the algorithm you use plus the internal representation of floating point numbers.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

That indeed did the trick. I checked if it was a float and you are correct.

Thanks Water for the explanation :)

Share this post


Link to post
Share on other sites

You might like to take a look at operator64 in my signature. Whether replacing the mathematical operators with these functions will give the right result depends on a number of factors.

Share this post


Link to post
Share on other sites

Thanks Water for the explanation :)

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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