Here is the result! At the moment I don't know if it is working correct. What's wrong is the BigInt1024Multiply.
The code is generally completely from Chat-GPT!
I had to correct some syntax! Like the correct use of * and Protected.
All the code comments are from Chat-CPT
Code: Select all
; First Try to create a BigInt Library with Chat-GPT
DeclareModule BigInt1024
EnableExplicit
Structure BigInt1024
q.q[16]
EndStructure
; Add two BigInt1024 numbers
Declare BigInt1024Add(*A.BigInt1024, *B.BigInt1024, *Result.BigInt1024)
; Subtract two BigInt1024 numbers
Declare BigInt1024Subtract(*A.BigInt1024, *B.BigInt1024, *Result.BigInt1024)
; Multiply two BigInt1024 numbers
Declare BigInt1024Multiply(*A.BigInt1024, *B.BigInt1024, *Result.BigInt1024)
; Divide two BigInt1024 numbers
Declare BigInt1024Divide(*A.BigInt1024, *B.BigInt1024, *Quotient.BigInt1024, *Remainder.BigInt1024)
; Other BigInt1024 operations can be added as needed
EndDeclareModule
Module BigInt1024
EnableExplicit
Macro BigInt1024Copy(Source, Dest)
CopyMemory(@Source, @Dest, SizeOf(BigInt1024))
EndMacro
Procedure BigInt1024Increment(*Value.BigInt1024)
Protected i
For i = 0 To 15
*Value\q[i] + 1
If *Value\q[i] <> 0 ; Check for overflow
ProcedureReturn
EndIf
Next
EndProcedure
; Compare two BigInt1024 numbers
Procedure.i BigInt1024Compare(*A.BigInt1024, *B.BigInt1024)
Protected i
For i = 15 To 0 Step -1
If *A\q[i] < *B\q[i]
ProcedureReturn -1
ElseIf *A\q[i] > *B\q[i]
ProcedureReturn 1
EndIf
Next
ProcedureReturn 0
EndProcedure
; Add two BigInt1024 numbers
Procedure BigInt1024Add(*A.BigInt1024, *B.BigInt1024, *Result.BigInt1024)
Protected Carry.q
Protected i
; Assuming the BigInt1024 structure has an array of Quads named q
For i = 0 To 15
*Result\q[i] = *A\q[i] + *B\q[i] + Carry
; Check for overflow
If *Result\q[i] < *A\q[i] Or *Result\q[i] < *B\q[i]
Carry = 1
Else
Carry = 0
EndIf
Next
EndProcedure
; Subtract two BigInt1024 numbers
Procedure BigInt1024Subtract(*A.BigInt1024, *B.BigInt1024, *Result.BigInt1024)
Protected Borrow.q
Protected i
; Assuming the BigInt1024 structure has an array of Quads named q
For i = 0 To 15
*Result\q[i] = *A\q[i] - *B\q[i] - Borrow
; Check for underflow
If *Result\q[i] > *A\q[i] Or *Result\q[i] > *B\q[i]
Borrow = 1
Else
Borrow = 0
EndIf
Next
EndProcedure
; Multiply two BigInt1024 numbers
Procedure BigInt1024Multiply(*A.BigInt1024, *B.BigInt1024, *Result.BigInt1024)
Protected i, j
; Assuming the BigInt1024 structure has an array of Quads named q
; TODO! The BigInt1024 Multiply exeedes the Array Size
For i = 0 To 15
For j = 0 To 15
; Perform the multiplication and accumulate the result
; [i+j] exeedes the ArraySize
; *Result\q[i + j] + *Result\q[i + j] + (*A\q[i] * *B\q[j])
Next
Next
EndProcedure
; Divide two BigInt1024 numbers
Procedure BigInt1024Divide(*A.BigInt1024, *B.BigInt1024, *Quotient.BigInt1024, *Remainder.BigInt1024)
Protected TempA.BigInt1024
Protected TempB.BigInt1024
Protected TempQuotient.BigInt1024
Protected TempRemainder.BigInt1024
; Initialize the temporary variables
BigInt1024Copy(*A, TempA)
BigInt1024Copy(*B, TempB)
; Loop until TempA >= TempB
While BigInt1024Compare(TempA, TempB) >= 0
; Subtract TempB from TempA
BigInt1024Subtract(TempA, TempB, TempRemainder)
; Increment the quotient
BigInt1024Increment(TempQuotient)
; Copy the remainder back to TempA for the next iteration
BigInt1024Copy(TempRemainder, TempA)
Wend
; Copy the final quotient and remainder
BigInt1024Copy(TempQuotient, *Quotient)
BigInt1024Copy(TempRemainder, *Remainder)
EndProcedure
; Other BigInt1024 operations can be added as needed
EndModule
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
UseModule BigInt1024
; Test function for BigInt1024 operations
Procedure TestBigInt1024Operations()
; Declare BigInt1024 variables
Protected BigInt1.BigInt1024
Protected BigInt2.BigInt1024
Protected Result.BigInt1024
Protected Quotient.BigInt1024
Protected Remainder.BigInt1024
; Initialize BigInt1 and BigInt2 with values (modify as needed)
BigInt1\q[0] = 123
BigInt2\q[0] = 456
; The addition result would be 579.
; The subtraction result would be -333 (If working With signed integers).
; The multiplication result would be 56188.
; The division quotient would be 0, And the remainder would be 123.
; Test addition
BigInt1024Add(BigInt1, BigInt2, Result)
Debug("Addition Result: " + Result\q[0])
; Test subtraction
BigInt1024Subtract(BigInt1, BigInt2, Result)
Debug("Subtraction Result: " + Result\q[0])
; Test multiplication
BigInt1024Multiply(BigInt1, BigInt2, Result)
Debug("Multiplication Result: " + Result\q[0])
; Test division
BigInt1024Divide(BigInt1, BigInt2, Quotient, Remainder)
Debug("Division Quotient: " + Quotient\q[0])
Debug("Division Remainder: " + Remainder\q[0])
EndProcedure
; Main program
TestBigInt1024Operations()
CompilerEndIf