11111010111 1010 1011

Programar por Programar


¿Te gusta programar?
Inicio


Código fuente de BigNum.vb

Estructura en Visual Basic 2005 que usa BigNum_ltm.dll


Publicado: 18/Oct/2007
Actualizado: 22/oct/2007
Autor: Guillermo 'guille' Som


 

Este es el código fuente de BigNum.vb la estructura que usa la DLL BigNum_ltm.dll.


Código fuente de BigNum.vb
'------------------------------------------------------------------------------
' BigNum                                                            (18/Sep/06)
' Estructura para trabajar con números enteros de alta precisión.
'
' La DLL BigNum_ltm.dll debe estar en el mismo directorio o en el PATH.
'
' Estructura para usar la DLL BigNum_ltm.dll
'
' Revisado y sobrecarga de operadores                               (12/Oct/07)
'
' ©Guillermo 'guille' Som, 2006-2007
'
' Para usar esta estructura
' Dim a As BigNum
' a = BigNum.Parse("...")
'------------------------------------------------------------------------------
Option Explicit On
Option Strict On

Imports System
Imports Microsoft.VisualBasic

''' <summary>
''' Estructura para manejar números enteros de gran precisión
''' usando las librerías BigNum_ltm.dll y LibTomMath.lib.
''' Esta estructura usa el tipo mp_int de la librería original
''' que en el wrapper he llamado BigNum.
''' Esta estructura es el tipo usado en las otras estructuras
''' de esta librería.
''' El espacio de nombres usado para esta librería es:
''' elGuille.Developer.LibTomMath
''' </summary>
''' <remarks>
''' Iniciado: 18/Sep/2006
''' Revisado: 22/Oct/2007
''' </remarks>
Public Structure BigNum
    Public used As Integer
    Public alloc As Integer
    Public sign As Integer
    Public dp As Integer
    '
    '
    '--------------------------------------------------------------------------
    ' Métodos compartidos (incluidos los accesos a la librería de C++)
    '--------------------------------------------------------------------------
    '
    ' Las funciones del API de LibTomMath
    '
    Public Declare Function BigNumToString Lib "BigNum_ltm.dll" (ByVal n As BigNum) As String
    Public Declare Function BigNumFromString Lib "BigNum_ltm.dll" (ByVal s As String) As BigNum
    Public Declare Function BigNumFromInt Lib "BigNum_ltm.dll" (ByVal i As Integer) As BigNum
    Public Declare Function BigNumToUInt Lib "BigNum_ltm.dll" (ByVal n As BigNum) As Integer
    '
    Public Declare Sub BigNumInit Lib "BigNum_ltm.dll" (ByRef n As BigNum)
    '
    Public Declare Function BigNumMul Lib "BigNum_ltm.dll" Alias "BigNumMul" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumDiv Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum, ByRef n3 As BigNum) As BigNum
    Public Declare Function BigNumAdd Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumSub Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumModPow2 Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal pot As Integer) As BigNum
    Public Declare Function BigNumAddMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum, ByVal n3 As BigNum) As BigNum
    Public Declare Function BigNumSubMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum, ByVal n3 As BigNum) As BigNum
    Public Declare Function BigNumMulMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum, ByVal n3 As BigNum) As BigNum
    Public Declare Function BigNumSqrMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumInvMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumPowMod Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum, ByVal n3 As BigNum) As BigNum
    Public Declare Function BigNumSqrt Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As BigNum
    Public Declare Function BigNumSqr Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As BigNum
    Public Declare Function BigNumIsSquare Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As Integer
    Public Declare Function BigNumJacobi Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As Integer
    Public Declare Function BigNumPow Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumPow2 Lib "BigNum_ltm.dll" _
            (ByVal pot As Integer) As BigNum
    Public Declare Function BigNumGCD Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumLCM Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    ' Esta es en realidad la misma que BigNumGCD
    ' (pero en castellano: Máximo Común Divisor)
    Public Declare Function BigNumMCD Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    ' Esta es en realidad la misma que BigNumLCM
    ' (pero en castellano: Mínimo Común Múltiplo)
    Public Declare Function BigNumMCM Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
    Public Declare Function BigNumExtEuclid Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum, _
             ByVal U1 As BigNum, ByVal U2 As BigNum) As BigNum
    '
    Public Declare Function BigNumRand Lib "BigNum_ltm.dll" _
            (ByVal digits As Integer) As BigNum
    Public Declare Function BigNumRandBits Lib "BigNum_ltm.dll" _
            (ByVal bits As Integer) As BigNum
    ' Compara dos BigNum
    ' Devuelve un entero con:
    '	  0 si son iguales (MP_EQ)
    '	 -1 si el primero es menor (MP_LT)
    '	  1 si el primero es mayor (MP_GT)
    Public Declare Function BigNumCompare Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As Integer
    Public Declare Sub BigNumCopyTo Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByRef n2 As BigNum)
    Public Declare Function BigNumCopy Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As BigNum
    Public Declare Function BigNumIsZero Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As Integer
    Public Declare Function BigNumIsEven Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As Integer
    Public Declare Function BigNumIsOdd Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As Integer
    Public Declare Sub BigNumSetToZero Lib "BigNum_ltm.dll" _
            (ByRef n1 As BigNum)
    Public Declare Function BigNumZero Lib "BigNum_ltm.dll" () As BigNum
    Public Declare Function BigNumOne Lib "BigNum_ltm.dll" () As BigNum
    Public Declare Function BigNumTwo Lib "BigNum_ltm.dll" () As BigNum
    Public Declare Function BigNumNeg Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As BigNum
    Public Declare Function BigNumAbs Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As BigNum
    '
    Public Declare Function BigNumPrimeIsDivisible Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum) As Integer
    Public Declare Function BigNumPrimeIsFermat Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As Integer
    Public Declare Function BigNumPrimeIsMillerRabin Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal n2 As BigNum) As Integer
    Public Declare Function BigNumPrimeIsPrime Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal t As Integer) As Integer
    ''' <summary>
    ''' Siguiente número primo
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="t"></param>
    ''' <param name="bbs_style"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Declare Function BigNumPrimeNextPrime Lib "BigNum_ltm.dll" _
            (ByVal n1 As BigNum, ByVal t As Integer, ByVal bbs_style As Integer) As BigNum


    ' Parse permite crear nuevas instancias a partir de cadenas o enteros

    ''' <summary>
    ''' Convierte una cadena en un BigNum
    ''' </summary>
    ''' <param name="s"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Parse(ByVal s As String) As BigNum
        Return BigNumFromString(s)
    End Function

    ''' <summary>
    ''' Convierte un entero en un BigNum
    ''' </summary>
    ''' <param name="n"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Parse(ByVal n As Integer) As BigNum
        Return BigNumFromInt(n)
    End Function

    ''' <summary>
    ''' Convierte un ULong a BigNum
    ''' </summary>
    ''' <param name="n"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' v.0.4 20/Oct/07
    ''' </remarks>
    Public Shared Function Parse(ByVal n As ULong) As BigNum
        Return BigNumFromString(n.ToString)
    End Function

    ''' <summary>
    ''' Siguiente número primo a partir del indicado
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function NextPrime(ByVal n1 As BigNum) As BigNum
        Return BigNumPrimeNextPrime(n1, 92, 0)
    End Function

    ''' <summary>
    ''' Devuelve un BigNum con valor cero
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Zero() As BigNum
        Return BigNum.BigNumZero
    End Function

    ''' <summary>
    ''' Devuelve un BigNum con valor 1
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function One() As BigNum
        Return BigNum.BigNumOne
    End Function

    ''' <summary>
    ''' Devuelve un BigNum con valor 2
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Two() As BigNum
        Return BigNum.BigNumTwo
    End Function



    ' #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
    ''' <summary>
    ''' Devuelve True si el BigNum indicado es cero
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function IsZero(ByVal n1 As BigNum) As Boolean
        Return (n1.used = 0)
    End Function

    ''' <summary>
    ''' Devuelve True si este número es cero
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsZero() As Boolean
        Return (used = 0)
    End Function

    ''' <summary>
    ''' Devuelve la raíz cuadrada del número indicado
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Sqrt(ByVal n1 As BigNum) As BigNum
        Return BigNum.BigNumSqrt(n1)
    End Function

    ''' <summary>
    ''' Devuelve la raíz cuadrada de este número
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Sqrt() As BigNum
        Return BigNum.BigNumSqrt(Me)
    End Function

    ''' <summary>
    ''' Convierte el BigNum indicado en un entero (Int32)
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ToInt32(ByVal n1 As BigNum) As Integer
        Try
            Return BigNumToUInt(n1)
        Catch ex As Exception
            Console.WriteLine("ERROR: " & ex.Message)
            Return -1
        End Try
    End Function

    ''' <summary>
    ''' Convierte este número en un entero (Int32)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ToInt32() As Integer
        Return BigNum.ToInt32(Me)
    End Function

    ''' <summary>
    ''' Multiplica dos BigNum
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Mul(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumMul(n1, n2)
    End Function

    ''' <summary>
    ''' Suma dos BigNum
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Add(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumAdd(n1, n2)
    End Function

    ''' <summary>
    ''' Suma dos BigNum
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' v.0.1 19/Oct/07
    ''' </remarks>
    Public Shared Function Sum(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumAdd(n1, n2)
    End Function

    ''' <summary>
    ''' Resta los dos BigNum indicados
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function [Sub](ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumSub(n1, n2)
    End Function

    ''' <summary>
    ''' Devuelve la parte entera de dividir los dos números indicados
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' v.0.1 19/Oct/07
    ''' Corregido el BUG que devolvía el resto en vez del resultado
    ''' En realidad no era un bug, sino una malintepretación
    ''' de lo que se debía devolver
    ''' En realidad el enunciado era:
    ''' Devuelve el resto de dividir los dos números indicado
    ''' </remarks>
    Public Shared Function Div(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Dim n3 As BigNum = BigNum.BigNumZero
        'Return BigNum.BigNumDiv(n1, n2, n3)
        ' n4 es el resto
        Dim n4 As BigNum = BigNum.BigNumDiv(n1, n2, n3)
        Return n3
    End Function

    ''' <summary>
    ''' Devuelve el resto de dividir los dos números indicados
    ''' La parte entera la devuelve por referencia en el tercer parámetro
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <param name="n3"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Div(ByVal n1 As BigNum, ByVal n2 As BigNum, _
                               ByRef n3 As BigNum) As BigNum
        n3 = BigNum.BigNumZero
        Return BigNum.BigNumDiv(n1, n2, n3)
    End Function

    ''' <summary>
    ''' Devuelve el resto de dividir los dos números indicados
    ''' La parte entera la devuelve por referencia en el tercer parámetro
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <param name="n3"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DivMod(ByVal n1 As BigNum, ByVal n2 As BigNum, _
                                  ByRef n3 As BigNum) As BigNum
        n3 = BigNum.BigNumZero
        Return BigNum.BigNumDiv(n1, n2, n3)
    End Function

    ''' <summary>
    ''' Devuelve el resto de la división
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DivMod(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumMod(n1, n2)
    End Function

    ''' <summary>
    ''' Devuelve el resto de la división
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function [Mod](ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumMod(n1, n2)
    End Function


    ''' <summary>
    ''' Decrementa uno al BigNum indicado
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Dec(ByVal n1 As BigNum) As BigNum
        Return BigNum.Sub(n1, BigNum.BigNumOne)
    End Function

    ''' <summary>
    ''' Decrementa el primer BigNum por la cantidad del segundo
    ''' (es como restarlos, en realidad es lo mismo que Sub)
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Dec(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.Sub(n1, n2)
    End Function

    ''' <summary>
    ''' Incrementa en uno el BigNum indicado
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Inc(ByVal n1 As BigNum) As BigNum
        Return BigNum.Add(n1, BigNum.BigNumOne)
    End Function

    ''' <summary>
    ''' Incrementa el primer BigNum con el segundo
    ''' Es como sumarlos
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Inc(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.Add(n1, n2)
    End Function

    ''' <summary>
    ''' Comprueba si los dos números son iguales
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Iguales(ByVal n1 As BigNum, ByVal n2 As BigNum) As Boolean
        Return (BigNum.BigNumCompare(n1, n2) = 0)
    End Function
    '
    'Public Function Iguales(ByVal n1 As BigNum) As Boolean
    '    Return (BigNum.BigNumCompare(Me, n1) = 0)
    'End Function

    ''' <summary>
    ''' Compara dos BigNum
    ''' Devuelve un entero con:
    '''	  0 si son iguales (MP_EQ)
    '''	 -1 si el primero es menor (MP_LT)
    '''	  1 si el primero es mayor (MP_GT)
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns>
    ''' Devuelve un entero con:
    '''	  0 si son iguales (MP_EQ)
    '''	 -1 si el primero es menor (MP_LT)
    '''	  1 si el primero es mayor (MP_GT)
    ''' </returns>
    ''' <remarks></remarks>
    Public Shared Function Compare(ByVal n1 As BigNum, ByVal n2 As BigNum) As Integer
        Return BigNum.BigNumCompare(n1, n2)
    End Function



    ''' <summary>
    ''' Factorial de un número usando BigNum
    ''' </summary>
    ''' <param name="num"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Factorial(ByVal num As BigNum) As BigNum
        If BigNum.IsZero(num) Then
            Return BigNum.BigNumOne
        ElseIf BigNum.Iguales(num, BigNum.BigNumOne) Then
            Return BigNum.BigNumOne
        End If

        Return BigNum.Mul(num, Factorial(BigNum.Sub(num, BigNum.BigNumOne)))
    End Function

    Public Shared ReadOnly Property PI() As BigNum
        Get
            Dim s As String

            ' el valor que tengo en gsCalc
            's = "31415926535897932384626433832795"

            ' Valor de la página
            ' http://centros5.pntic.mec.es/ies.de.bullas/dp/matema/conocer/pi_1500.htm
            s = "314159265358979323846264338327950288419716939937510582097494459230781" & _
                "640628620899"

            Return BigNum.Parse(s)
        End Get
    End Property



    '--------------------------------------------------------------------------
    ' Métodos de instancia
    ' Si hay métodos de instancia y compartidos que se llaman igual
    ' estarán definidos con los compartidos.
    '--------------------------------------------------------------------------

    ''' <summary>
    ''' Devuelve una cadena con el BigNum actual
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' v.0.5 20/Oct/07
    ''' Si es cero, devolver cero
    ''' </remarks>
    Public Overrides Function ToString() As String
        If Me.IsZero Then
            Return "0"
        Else
            Return BigNumToString(Me)
        End If
    End Function



    '
    ' Constructores
    '

    Public Sub New(ByVal num As String)
        Dim bn As BigNum = BigNum.Parse(num)
        With Me
            .used = bn.used
            .sign = bn.sign
            .dp = bn.dp
            .alloc = bn.alloc
        End With
    End Sub

    Public Sub New(ByVal num As Double)
        Me.New(num.ToString)
    End Sub


    '--------------------------------------------------------------------------
    ' Sobrecarga de operadores y conversiones
    '--------------------------------------------------------------------------

    '
    ' Conversiones
    '

    ''' <summary>
    ''' Convierte un Double o cualquier otro tipo numérico a BigNum
    ''' </summary>
    ''' <param name="num"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Widening Operator CType(ByVal num As Double) As BigNum
        Dim i As Integer = CInt(num)
        'Dim i As ULong = CULng(num)
        Return BigNum.Parse(i)
    End Operator

    ''' <summary>
    ''' Convierte un ULong a BigNum
    ''' </summary>
    ''' <param name="num"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Widening Operator CType(ByVal num As ULong) As BigNum
        Return BigNum.Parse(num)
    End Operator

    ''' <summary>
    ''' Convierte un BigNum a una cadena
    ''' </summary>
    ''' <param name="num"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' v.0.5 20/Oct/07
    ''' Devolver siempre ToString
    ''' </remarks>
    Public Shared Widening Operator CType(ByVal num As BigNum) As String
        Return num.ToString 'BigNum.BigNumToString(num)
    End Operator

    ''' <summary>
    ''' Convierte una cadena en un BigNum
    ''' </summary>
    ''' <param name="num"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Widening Operator CType(ByVal num As String) As BigNum
        Return BigNum.Parse(num)
    End Operator

    ''' <summary>
    ''' Convierte un BigNum en un entero de 32 bits
    ''' Se debe usar con una función de conversión
    ''' (conversión explícita)
    ''' </summary>
    ''' <param name="num"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Narrowing Operator CType(ByVal num As BigNum) As Integer
        Return BigNum.ToInt32(num)
    End Operator


    '
    ' Sobrecarga de operadores aritméticos
    '

    ''' <summary>
    ''' Unario, devuelve el mismo número
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator +(ByVal bn1 As BigNum) As BigNum
        Return bn1
    End Operator

    ''' <summary>
    ''' Unario, devuelve el valor en negativo si era positivo
    ''' y viceversa
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator -(ByVal bn1 As BigNum) As BigNum
        Return BigNum.BigNumNeg(bn1)
    End Operator


    ''' <summary>
    ''' Suma dos números
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator +(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As BigNum
        Return BigNum.Add(bn1, bn2)
    End Operator

    ''' <summary>
    ''' Resta dos números
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator -(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As BigNum
        Return BigNum.Sub(bn1, bn2)
    End Operator

    ''' <summary>
    ''' Multiplica dos números
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator *(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As BigNum
        Return BigNum.Mul(bn1, bn2)
    End Operator

    ''' <summary>
    ''' Divide dos números
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator /(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As BigNum
        Return BigNum.Div(bn1, bn2)
    End Operator

    ''' <summary>
    ''' Divide dos números
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator \(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As BigNum
        Return BigNum.Div(bn1, bn2)
    End Operator

    ''' <summary>
    ''' Devuelve el resto de la división de los dos números
    ''' </summary>
    ''' <param name="n1"></param>
    ''' <param name="n2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator Mod(ByVal n1 As BigNum, ByVal n2 As BigNum) As BigNum
        Return BigNum.BigNumMod(n1, n2)
    End Operator


    ''' <summary>
    ''' Sobrecarga del operador de concatenación
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' v.0.1 19/Oct/07
    ''' </remarks>
    Public Shared Operator &(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As String
        Return bn1.ToString & bn2.ToString
    End Operator

    ' Estas dos sobrecargas es para evitar convertir una cadena a BigNum
    ' cuando los parámetros no son BigNum
    Public Shared Operator &(ByVal bn1 As BigNum, ByVal bn2 As String) As String
        Return bn1.ToString & bn2
    End Operator

    Public Shared Operator &(ByVal bn1 As String, ByVal bn2 As BigNum) As String
        Return bn1 & bn2.ToString
    End Operator


    '
    ' Sobrecarga de operadores de comparación
    '

    ''' <summary>
    ''' Devuelve True si son iguales
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator =(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As Boolean
        Return BigNum.Compare(bn1, bn2) = 0
    End Operator

    ''' <summary>
    ''' Devuelve True si son diferentes
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator <>(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As Boolean
        Return BigNum.Compare(bn1, bn2) <> 0
    End Operator

    ''' <summary>
    ''' Devuelve True si el primero es menor
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator <(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As Boolean
        Dim i As Integer = BigNum.Compare(bn1, bn2)
        Return i = -1
    End Operator

    ''' <summary>
    ''' Devuelve True si el primero es mayor
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator >(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As Boolean
        Dim i As Integer = BigNum.Compare(bn1, bn2)
        Return i = 1
    End Operator

    ''' <summary>
    ''' Devuelve True si el primero es menor o igual
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator <=(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As Boolean
        Dim i As Integer = BigNum.Compare(bn1, bn2)
        Return i <= 0
    End Operator

    ''' <summary>
    ''' Devuelve True si el primero es mayor o igual
    ''' </summary>
    ''' <param name="bn1"></param>
    ''' <param name="bn2"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator >=(ByVal bn1 As BigNum, ByVal bn2 As BigNum) As Boolean
        Dim i As Integer = BigNum.Compare(bn1, bn2)
        Return i >= 0
    End Operator


    '
    ' Sobrecarga de operadores booleanos
    '

    ''' <summary>
    ''' Devuelve True si no es cero
    ''' </summary>
    ''' <param name="bn"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator IsTrue(ByVal bn As BigNum) As Boolean
        Return Not BigNum.IsZero(bn) ' BigNum.BigNumIsZero(bn) = 0
    End Operator

    ''' <summary>
    ''' Devuelve True si es cero
    ''' </summary>
    ''' <param name="bn"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Operator IsFalse(ByVal bn As BigNum) As Boolean
        'Return BigNum.BigNumIsZero(bn) = 1
        Return BigNum.IsZero(bn)
    End Operator


End Structure


Programar por programar... ¡porque te gusta programar!
Ir al índice principal
Ir al sitio del Guille - Ir a los foros del Guille

Has entrado usando el host programarporprogramar.org