Test: Chat GPT created BigInt Library for PB

Everything else that doesn't fall into one of the other PB categories.
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Test: Chat GPT created BigInt Library for PB

Post by SMaag »

I just created a BigInt1024 Library with ChatGPT! Just for a test and see what is possible
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
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Test: Chat GPT created BigInt Library for PB

Post by STARGÅTE »

SMaag wrote: Fri Dec 15, 2023 11:58 pm At the moment I don't know if it is working correct.
No, it's trash!
First of all, the code can't work correctly because Pure Basic quads are signed.
Therefore the quad array has positive and negative values inside the number (not only in front).
This means BigInt1024Compare() doesn't work.
The division code is very funny, it just subtracts the divisor step by step. If *A is huge and *B small, this ends up in an infinite loop.
As Pure Basic quads are signed, also the BigInt1024Add and BigInt1024Subtract can't work like this.
if *A\q[ i ] and *B\q[ i ] are large positive numbers *A\q[ i ] + *B\q[ i ] becomes negative, generating a Carry, however, the result would fit in an unsigned quad.

As you said, you had corrected the syntax of the code, it is continues to be a waste of time using Chat-GPT for Pure Basic.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Test: Chat GPT created BigInt Library for PB

Post by SMaag »

Yes you are right, it's completely nonsens!

But it shows how Chat GPT is working. It is basically a statistical weighting! If your case is the most common, maybe you'll get an aceptable result.
But you can't trust it!

I asked GPT for Testcode and I got it! With values what should be the correct result! The problem: I got this values
when calcualating with GPT's code. These were the correct values for an incorrect code! Very dangerous!

Then I said to GPT I found an error in the Deviding function. Dividing BigInt in this way is not possible and
we are out of the Array limits. Than GPT tried to fix the 2 For Next to keep the limit of 15 for the Array Index.
Completely nonsens!

My conclusion is: All advertising that GPT can write programms or support you writing programms isn't true.
Maybe yes, GPT can write code, but you can't trust not a single line. Therefore it has no value for you!

What was working good was an other test: It was just searching for forumlas and converting this into code.
I tried this for sine and cosine functions with complex numbers and the 3 Point Function of a circle!
If GPT can find the right forumla it can be converted into a working code. But that is only a small part programming.

Code: Select all

Structure P2D
  x.f
  y.f
EndStructure

; by Chat CPT
Procedure CalculateCircleCenter(*Out.P2D, *Pt1.P2D, *Pt2.P2D, *Pt3.P2D)
  Protected.d d, xc, yc
  
  ; d = 2.0[(x1-x2)(y2-y3) - (x2-x3)(y1-y2)]
  
  d= 2.0 * ((*Pt1\x - *Pt2\x) * (*Pt2\y - *Pt3\y) - (*Pt2\x - *Pt3\x) * (*Pt1\y - *Pt2\y))
  
  ; xc = [ (x1²​+y1²​)(y2​−y3​) + (x2²​+y2²​)(y3​−y1​) + (x3²​+y3²​)(y1​−y2​) ] /d
  xc = ((*Pt1\x * *Pt1\x + *Pt1\y * *Pt1\y) * (*Pt2\y - *Pt3\y) + (*Pt2\x * *Pt2\x + *Pt2\y * *Pt2\y) * (*Pt3\y - *Pt1\y) + (*Pt3\x * *Pt3\x + *Pt3\y * *Pt3\y) * (*Pt1\y - *Pt2\y)) / d
  
  ; yc = [ (x1²+y1²​)(x3​−x2​) + (x2²​+y2²​)(x1​−x3​) + (x3²​+y3²​)(x2​−x1​)​ ] /d
  yc = ((*Pt1\x * *Pt1\x + *Pt1\y * *Pt1\y) * (*Pt3\x - *Pt2\x) + (*Pt2\x * *Pt2\x + *Pt2\y * *Pt2\y) * (*Pt1\x - *Pt3\x) + (*Pt3\x * *Pt3\x + *Pt3\y * *Pt3\y) * (*Pt2\x - *Pt1\x)) / d
  
  *Out\x = xc
  *Out\y = yc
  ProcedureReturn *OUT
EndProcedure

  Structure TComplex      ; kartesian coordinates
    re.d
    im.d
  EndStructure

  ; Chat GPT
  Procedure.i CSinh(*OUT.TComplex, *Z.TComplex)
    ; Calculate the hyperbolic sine of a complex number
    ; sinh(z) = (e^z - e^(-z)) / 2
    
    ; Calculate e^Z and e^(-Z)
    Protected expZ.TComplex
    Protected expNegZ.TComplex
    
    expZ\re = Exp(*Z\re) * Cos(*Z\im)
    expZ\im = Exp(*Z\re) * Sin(*Z\im)
  
    expNegZ\re = Exp(-*Z\re) * Cos(-*Z\im)
    expNegZ\im = Exp(-*Z\re) * Sin(-*Z\im)
  
    ; Calculate sinh(Z)
    *OUT\re = (expZ\re - expNegZ\re) / 2.0
    *OUT\im = (expZ\im - expNegZ\im) / 2.0
  
    ProcedureReturn *OUT
  EndProcedure
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Test: Chat GPT created BigInt Library for PB

Post by infratec »

Why ask ChatGPT :?: :?: :?:

Ask in Coding Questions :!:

You will get correct working code. :mrgreen: :mrgreen: :mrgreen:
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Test: Chat GPT created BigInt Library for PB

Post by Bisonte »

infratec wrote: Sat Dec 16, 2023 6:41 pm Why ask ChatGPT :?: :?: :?:

Ask in Coding Questions :!:

You will get correct working code. :mrgreen: :mrgreen: :mrgreen:
Right...

Our CI (crowd intelligence) is better than every AI :mrgreen: :mrgreen: :mrgreen:
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Test: Chat GPT created BigInt Library for PB

Post by Mijikai »

There is no I in ChatGPT.
Post Reply