Sign in to follow this  
Followers 0
Tlem

Strange result on a simple arithmetical calculation

11 posts in this topic

I don't know if I make a mistake somewhere, but to find a solution of a problem, I try to make a simple calculation and he gave me a wrong result !

So this is the code :

$Cpt = 10
For $i = 1 to 20
    $Cpt *= 10
    ConsoleWrite($Cpt & @CRLF)
Next
MsgBox(64, "Strange...", "10*10*10*10*10 ... (20 time) gave this => " & $Cpt)
MsgBox(64, "Strange..." , "10^20 gave this => " & 10^20)

Did someone have a response of this strange fact ?


Best Regards.Thierry

Share this post


Link to post
Share on other sites



My guess is byte limit of the variable


Giggity

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I can't believe it because look here : http://www.autoitscript.com/autoit3/docs/faq.htm#15

Number range (floating point): 1.7E–308 to 1.7E+308 with 15-digit precision

Number range (integers): 64-bit signed integer

Edited by Tlem

Best Regards.Thierry

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

It's really odd...

$Cpt = 10^1
For $i = 1 to 19
    $Cpt *= 10
;Sleep(50)
    ConsoleWrite($Cpt & @CRLF)
Next
MsgBox(64, "Strange...", "10*10*10*10*10 ... (20 time) gave this => " & $Cpt)
MsgBox(64, "Strange..." , "10^20 gave this => " & 10^20)

fixes it.

Curse of IRQ 13? (for those that don't know, when cp's had math co-processors they were IRQ 13... and they didn't work to well)

Edit: don't need the sleep

Another Edit: also needs to be to 19

Edited by youknowwho4eva

Giggity

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Both work.

$Cpt = 10.0
 For $i = 1 to 20
     $Cpt *= 10
     ConsoleWrite($Cpt & @CRLF)
 Next
 ConsoleWrite( 10^20 & @CRLF)

$Cpt = 10^0
 For $i = 1 to 20
     $Cpt *= 10
     ConsoleWrite($Cpt & @CRLF)
 Next
 ConsoleWrite( 10^20 & @CRLF)
Edited by weaponx

Share this post


Link to post
Share on other sites

Odd. I thought any number to the 0th power = 1

This also confuses me more

dim $Cpt[21]
$cpt[0] = 10
$cpt[1] = 10
For $i = 1 to 20
    $Cpt[$i] = $cpt[$i - 1] * 10
;~  Sleep(50)
    ConsoleWrite($Cpt & @CRLF)
Next
MsgBox(64, "Strange...", "10*10*10*10*10 ... (20 time) gave this => " & $Cpt[20])
MsgBox(64, "Strange..." , "10^19 * 10 gave this => " & 10^19 * 10)

Giggity

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

The value of 10^20 overflows a signed INT64. By default, integers will be INT32 to start, then will be INT64, then should go to DOUBLE (float) for larger numbers, but the "*=" operator will not change to float, so it overflows the signed INT64 variable:

$Cpt = 10
For $i = 1 to 20
    $Cpt *= 10
    ConsoleWrite("$i = " & $i & "  $Cpt = " & $Cpt & "  Type = " & VarGetType($Cpt) & @CRLF)
Next
$Cpt = 2^63 - 1; Max positive value in an INT64
ConsoleWrite("2^64 - 1 = " & $Cpt & "  Type = " & VarGetType($Cpt) & @LF)
$Cpt = 10^20
ConsoleWrite("10^20 = " & $Cpt & "  Type = " & VarGetType($Cpt) & @LF)

:)

P.S. If you change the value multiplier to 10.0, as weaponx posted, you get float types from the start because of the decimal point. Run the script above with "$Cpt *= 10.0" to see the difference.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

@weaponx

Your right, but 10.0 and 10^1 is not a way natural to write the number 10. :)

If I use Calc, I can do 10*10*10... 30 time without going to scientific writing and I have no calculation error.


Best Regards.Thierry

Share this post


Link to post
Share on other sites

@weaponx

Your right, but 10.0 and 10^1 is not a way natural to write the number 10. :)

If I use Calc, I can do 10*10*10... 30 time without going to scientific writing and I have no calculation error.

I wasn't using 10^0 to represent 10, I used it so your loop could still go from 1-20 instead 1-19...

Share this post


Link to post
Share on other sites

So the answer is that it is the curse of IRQ 13? or is it a *= problem that can be fixed?


Giggity

Share this post


Link to post
Share on other sites

So the answer is that it is the curse of IRQ 13? or is it a *= problem that can be fixed?

I don't think it is considered a 'problem' to be fixed. In most languages aside from AutoIt variable typing is strict and the idea that a function would just automatically change a variable from and INT type to a Float on it's own would be considered a huge bug. The programmer is expected to determine if INT or Float type is appropriate and just code to that. In this instance you do that by using 10.0 vice 10 to force the Float type you need.

AutoIt is not strongly typed because all its variables are acutally just c-type variant under the hood anyway, and it will automatically make the change from INT32 to an INT64 variant as shown above, but the switch from an INT to a Float could be considered a bug in itself, I think.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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
Sign in to follow this  
Followers 0