using ChatGPT to convert PB program to O2

Started by Chris Chancellor, April 05, 2023, 11:01:32 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello All
i have tried to convert the PB program below to O2

#COMPILE EXE
#REGISTER NONE
#DIM ALL

FUNCTION CalculatePhi(X AS EXT, Y AS EXT) AS EXT
    ' These corrections are needed to make Phi take a full
    ' circle i.e. 360 degrees.
    ' Without the corrections Phi will only vary 180 degrees
    ' (-90 to 90) corresponding to the range for arcus tangens (ATN).
'   https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/9236-atn2-function-i-e-de-ambiguized-atan?p=542737#post542737
    LOCAL Ph AS EXT
    IF ABS(X) < 1E-1000## THEN X = 1E-1000##
    Ph = ATN(Y/X)
    IF Ph < 0## THEN Ph = Ph + 3.14159265358979324##
    IF Y  < 0## THEN Ph = Ph + 3.14159265358979324##
    IF Y = 0 AND X < 0 THEN
        Ph = Ph + 3.14159265358979324##
    END IF
    FUNCTION = Ph
END FUNCTION

FUNCTION PBMAIN
    LOCAL A##,t$,X##,Y##,i##
    FOR i = 0## TO 7.9##*ATN(1) STEP ATN(1)*0.5##
        X=COS(i) : Y=SIN(i)
        A = CalculatePhi(X, Y)
        t=t+"X="+FORMAT$(X,"##.####")+"    Y="+FORMAT$(Y,"##.####")+ _
        "       true angle="+FORMAT$(i*57.29577951308232##,"###.######")+ _
        "       angle from X and Y="+FORMAT$(A*57.29577951308232##,"###.######")+$CRLF
    NEXT
    MSGBOX t,,"Get angle in DEGREES from X and Y"
END FUNCTION


ChatGPT said

As you can see, the general structure of the program remains the same.
However, there are some key differences:

In OxygenBasic, you need to use the Include directive to include any
 necessary libraries or headers. In this case, we're including the
Console.inc file, which provides access to console I/O functions.

Variable declarations need to include the data type, as in X As Double.

Instead of using PRINT to output text to the console, you need to use the
Console.Print method.

Instead of using MSGBOX to display a message box, we simply use
 Console.Print.

The program is executed by calling the Main function at the end of
 the file.

Note that these are just some of the differences between PowerBASIC
and OxygenBasic, and the exact changes needed to convert a program
 will depend on the specifics of the code you're working with.

and the code :

Include "Console.inc"

Function CalculatePhi(X As Double, Y As Double) As Double
    ' These corrections are needed to make Phi take a full
    ' circle i.e. 360 degrees.
    ' Without the corrections Phi will only vary 180 degrees
    ' (-90 to 90) corresponding to the range for arcus tangens (ATN).
    Local Ph As Double
    If Abs(X) < 1E-1000# Then X = 1E-1000#
    Ph = Atan(Y / X)
    If Ph < 0# Then Ph = Ph + 3.14159265358979324#
    If Y < 0# Then Ph = Ph + 3.14159265358979324#
    If Y = 0 And X < 0 Then
        Ph = Ph + 3.14159265358979324#
    End If
    Function = Ph
End Function

Sub Main()
    Local A As Double, t As String, X As Double, Y As Double, i As Double
    For i = 0 To 7.9 * Atan(1) Step Atan(1) * 0.5
        X = Cos(i) : Y = Sin(i)
        A = CalculatePhi(X, Y)
        t = t + "X=" + Format$(X, "##.####") + "    Y=" + Format$(Y, "##.####") + _
            "       true angle=" + Format$(i * 57.29577951308232, "###.######") + _
            "       angle from X and Y=" + Format$(A * 57.29577951308232, "###.######") + $CRLF
    Next
    Console.Print(t)
    Console.WaitForUser()
End Sub

Main()


  •  

Chris Chancellor

i haven't compile the results in O2 and i think it works out?
can somebody please test this program out in O2 as i haven't use O2 for some time now

Thanxx a lot my friends
  •  

Charles Pegge

#2
Not bad but still needs the human touch. ChatGPT produces a sort of collective human response. It  is not a true AI yet.

supplying missing format function, which I should add to future console.inc
uses Console

def crlf cr
def waitforuser wait

function format(double v, string f) as string
  string s,sg
  int de=instr(f,".")
  int le=len(f)
  int dp=le-de
  sg=" "
  s=str round(v*pow(10,dp))
  if v<0
    s=mid(s,2)
    sg="-"
  endif
  int ls=len(s)+1
  if ls>le
    le=ls
  endif
  s= right(string(le-ls,"0")+s,le-1)
  s=left(s,de-1)+"."+mid(s,dp)
  return sg+s
end function


Function CalculatePhi(X As Double, Y As Double) As Double
    ' These corrections are needed to make Phi take a full
    ' circle i.e. 360 degrees.
    ' Without the corrections Phi will only vary 180 degrees
    ' (-90 to 90) corresponding to the range for arcus tangens (ATN).
    Local Ph As Double
    If Abs(X) < 1E-1000 Then X = 1E-1000
    Ph = Atn(Y / X)
    If Ph < 0 Then Ph = Ph + 3.14159265358979324
    If Y < 0 Then Ph = Ph + 3.14159265358979324
    If Y = 0 And X < 0 Then
        Ph = Ph + 3.14159265358979324
    End If
    Function = Ph
End Function

Sub Main()
    local A As Double, t As String, X As Double, Y As Double, i As Double
    For i = 0 To 7.9 * Atn(1) Step Atn(1) * 0.5
        X = Cos(i) : Y = Sin(i)
        A = CalculatePhi(X, Y)
        t = t + "X=" + Format(X, "##.####") + "    Y=" + Format(Y, "##.####") + _
            "       true angle=" + format(i * 57.29577951308232, "###.######") + _
            "       angle from X and Y=" + Format(A * 57.29577951308232, "###.######") + CRLF
    Next
    Print(t)
    WaitForUser()
End Sub

Main()

Chris Chancellor

Thanxx a lot, Charles

I think i will use ChatGPT for the first round translation from PB to O2  and then clear off the errors later on.
It is definitely smarter than myself
  •  

Nicola

Hi Charles,
It seems to me that the Format function is not very good... or am I wrong?
Cheers

uses console

function Format(double v, string f) as string
  string s,sg
  int de=instr(f,".")
  int le=len(f)
  int dp=le-de
  sg=" "
  s=str round(v*pow(10,dp))
  if v<0
    s=mid(s,2)
    sg="-"
  endif
  int ls=len(s)+1
  if ls>le
    le=ls
  endif
  s= right(string(le-ls,"0")+s,le-1)
  s=left(s,de-1)+"."+mid(s,dp)
  return sg+s
end function
 
double x
x=3.055
Print Format(x, "##.####") 
wait

'= 03.550

Charles Pegge

It was an ad-hoc. For the console I think it might be better to use C printf string formats, whic can be obtained through MSVCRT.

Theo Gottwald

@Charles, let me remind you that there are many C-Compiler out there but we still look for a good Powerbasic successor that should be able to compile PB Code with as few changes as possible.
This includes the FORMAT Command.
Of course the question is, if O2 is in the run for a PB Successor or is planned as something new "as an alternative to C" - which can be difficult due to the large amount of such tools.

Charles Pegge


Hi Theo,
One of the motives for developing o2 was to escape the limitations of PB (and FB to some extent). So getting involved in PB again is like returning to jail. But maybe someone else would like to produce an open library of the higher PB functions, and maybe a translator for the PB metalanguage.

If all goes well I have about 20 years of useable life remaining on planet Earth, so I must choose my projects very carefully :)

Theo Gottwald

#8
Then you should consider Kryia Yoga alternative to programming :-)?

What limitations of PB did you escape from?
Tell us. Make a Sticky thread where you list all the points that you think, O2 is more advanced and better then XY - for example PowerBasic.

If you want to make 20 more Years, Spain would possibly be a better place then England, because there are rumors it may get flooded as well as northern Germany.

FBP_1186.jpg

2017-05-27 09_18_35-Russlands Waffen_ Tektonische und Tsunami-Superwaffe _Status-6_ - YouTube.png

Charles Pegge

I taught TM for a while back in the 70s (Maharishi) before computer yoga :). Yogananda had a very interesting life but only lived for 59 years so maybe Kriya yoga is not the solution for longevity.
Maybe it's the 'blue zones' of the world where the food and social life is excellent.

With regard to PowerBasic, the least stressful escape route might be to do some preliminary work, removing PB overlay code in favour of direct calls to the Windows API functions. Then you will have working archival quality code which will easily translate to any procedural language without getting locked-in.

Writing about o2 advantages is a project in itself but the main themes are compact function set and extensive flexibilty.

Bernard Kunzy

QuoteWith regard to PowerBasic, the least stressful escape route might be to do some preliminary work, removing PB overlay code in favour of direct calls to the Windows API functions. Then you will have working archival quality code which will easily translate to any procedural language without getting locked-in.

So true, that is why SDK coders have moved away from PB for more than a decade.
Direct call to the core API, is the only common denominator to the different languages I learned along the years.
PB's code should be kept into the museum section, because there is no future for it.
And the beauty of procedural code, is that it can still fit onto a single floppy disk, even using 64-bit.  :)
O2 could be considered as a good transition between Basic and C.

  •  

Theo Gottwald

That may be true for most people where their code is just a collection of API Calls.
For me this is not the case as i do construct must things where possible myself and only use API where needed.
The lifespan of someone is not a finalparameter, i recommend taht you read his Book to get a better conclusion about his life.
Here is a saying "the best die first" and there is something about it.
Warming4.jpg

Charles Pegge

This image comes from an institution which used to  burn people alive when they challenged its authority. What creator-being would really be so sadistic?

On the subject of longevity I note that Vivekananda managed 39 and the illustrious Shankara 32 years!


I think AI will be very good at program translation, and useful even when slightly wrong. We can ask: is the AI wrong or is it the language which needs correction.