Jump to content

Unpredictable result in simple calculation


Recommended Posts

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

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks Water for the explanation :)

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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