Jump to content

Passing large numbers to functions


Recommended Posts

I'm trying to pass this number to a function: 9223372036854775807. When I use consolewrite in the function to display the number I get the scientific value instead: 9.22337203685478e+018. This only happens when I use AutoItObject.

I have abstracted the script to focus on the problem area:

I should mention that my Windows 7 machine is 64 bit.

#include <AutoItObject.au3>

Global Const $intMAX = 9223372036854775807
;~ Global Const $intMAX = 2147483647 ; works fine when using this instead

_AutoItObject_Startup()

Global $calc = Calculator()

ConsoleWrite("Before:" & @TAB & $intMAX & @LF)

$calc.Sum($intMAX, 0)

Func Calculator()

    Local $this = _AutoItObject_Create()

    _AutoItObject_AddMethod($this, "Sum", "Calculator_Sum")

    Return $this
EndFunc ;==>Calculator

Func Calculator_Sum($this, Const $p, Const $q)

    ConsoleWrite("After:" & @TAB & $p & @LF)

    If ($p >= 0 And $q >= 0) Or ($p < 0 And $q < 0) Then ; possibility of overflow condition

        If $q >= 0 Then

            Local Const $result = $p + $q

            If $result < $p Then ; overfloweth

                Return SetError(1, 0, "overflow")
            Else

                Return $result
            EndIf
        EndIf
    Else ; no possibility of overflow

        Return $p + $q
    EndIf
EndFunc ;==>Calculator_Sum

this works fine:

Global Const $intMAX = 9223372036854775807

display($intMAX)

Func display($num)
    ConsoleWrite($num & @LF)
EndFunc ;==>display
Edited by jaberwocky6669
Link to comment
Share on other sites

Global Const $intMAX = "9223372036854775807" ;it's string now, but $intMAX + $Something still returns right numbric value

Thanks E1M1. I tried it just now but unfortunately when I add $p and $q the result still returns scientific notation.

#include <AutoItObject.au3>

Global Const $intMAX = "9223372036854775807"
;~ ;Global Const $intMAX = 2147483647 ; works fine when using this instead

_AutoItObject_Startup()

Global $calc = Calculator()

ConsoleWrite("Before:" & @TAB & $intMAX & @LF)

$calc.Sum($intMAX, 1)

Func Calculator()

    Local $this = _AutoItObject_Create()

    _AutoItObject_AddMethod($this, "Sum", "Calculator_Sum")

    Return $this
EndFunc ;==>Calculator

Func Calculator_Sum($this, Const $p, Const $q)

    ConsoleWrite("After:" & @TAB & $p & @LF)

    If ($p >= 0 And $q >= 0) Or ($p < 0 And $q < 0) Then ; possibility of overflow condition

        If $q >= 0 Then

            Local Const $result = $p + $q

            ConsoleWrite("But still returns scientific notation:" & @TAB & $result & @LF)

            If $result < $p Then ; overfloweth

                Return SetError(1, 0, "overflow")
            Else

                Return $result
            EndIf
        EndIf
    Else ; no possibility of overflow

        Return $p + $q
    EndIf
EndFunc ;==>Calculator_Sum
Link to comment
Share on other sites

I just played with some string commands, and got it work. It's reinventing ther wheel, but if wheel is badly invented you have to reinvent it.

#include <AutoItObject.au3>

Global Const $intMAX = "9223372036854775807"
;~ ;Global Const $intMAX = 2147483647 ; works fine when using this instead

_AutoItObject_Startup()

Global $calc = Calculator()

ConsoleWrite("Before:" & @TAB & $intMAX & @LF)

$calc.Sum($intMAX, 1)

Func Calculator()

    Local $this = _AutoItObject_Create()

    _AutoItObject_AddMethod($this, "Sum", "Calculator_Sum")

    Return $this
EndFunc   ;==>Calculator

Func Calculator_Sum($this, Const $p, Const $q)

    ConsoleWrite("After:" & @TAB & $p & @LF)

    If ($p >= 0 And $q >= 0) Or ($p < 0 And $q < 0) Then ; possibility of overflow condition

        If $q >= 0 Then

            $plen = StringLen($p)
            $qlen = StringLen($q)
            If $plen > $qlen Then
                $str = StringRight($p, $qlen + 1)
                If StringLeft($str, 1) = 0 And StringLen($str) > 1 Then
                    $res = "0" & $str + $q
                    $result = StringLeft($p,$plen - ($qlen +1)) & $res
                Else
                    $res = $str + $q
                    $result = StringLeft($p,$plen - ($qlen +1)) & $res
                EndIf
            Else
                ;Your code here
            EndIf

            ConsoleWrite("But still returns scientific notation:" & @TAB & $result & @LF)

            If $result < $p Then ; overfloweth

                Return SetError(1, 0, "overflow")
            Else

                Return $result
            EndIf
        EndIf
    Else ; no possibility of overflow

        Return $p + $q
    EndIf
EndFunc   ;==>Calculator_Sum
Edited by E1M1

edited

Link to comment
Share on other sites

Hey, that works for sure. Now to wonder why it doesn't work when I pass large numbers when using autoitobject. The workaround is nice but I'd like to be able to do it using integers though.

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