Author Topic: Pascal  (Read 5037 times)

0 Members and 1 Guest are viewing this topic.

Offline Ryster

  • Newbie
  • Posts: 77
    • View Profile
Pascal
« on: February 25, 2019, 03:49:06 pm »
Welcome.
Would any of the honorable Forum members undertake to translate the code written in Pascal to QB64 ?. The code has about 60 lines.
Regards Ryster

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Pascal
« Reply #1 on: February 25, 2019, 04:03:54 pm »
Can you share it?  It’s been a while, but I used to program in pascal ages ago.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Pascal
« Reply #2 on: February 25, 2019, 04:28:15 pm »
Pascal is very similar to BASIC. I tried it several years go, when it looked like QB was headed to the graveyard. Fortunately QB64 got started before I spent much time in Pascal. My opinion was you can do more with '64.

Yes, share the code and what the program is supposed to do.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Pascal
« Reply #3 on: February 25, 2019, 06:11:11 pm »
@Pete
Pascal = QB ?
in my experience (TurboPascal) this is not quite right!
1. very restrictive rules to code...  at first phase  declaration at second phase coding the algoritm (so often while I was starting to declare each variable I had already forgotten the main idea how to do something and  my fantasy was stopped very quickly!)
2. fine to have  inline assembler, calling to OS functions like interrupts (no coito interruptus censored), pointers, OOP (starting from 5.5 to up)

or do you think about https://it.wikipedia.org/wiki/COMAL COMAL?
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Pascal
« Reply #4 on: February 25, 2019, 06:27:23 pm »
It's coding statements are very similar to BASIC. The other stuff? WHOGAS!

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Pascal
« Reply #5 on: February 25, 2019, 06:38:19 pm »
I'm absolutely agreed with you!
(sob I don't remember who is WHOGAS! :-)  )
And I found very similar also the FORTRAN to the BASIC statements.

But how hard Pascal  is to force you to declare and then use something I find no equal in my little experience!
Good night ( at my clock) !
Programming isn't difficult, only it's  consuming time and coffee

Offline Ryster

  • Newbie
  • Posts: 77
    • View Profile
Re: Pascal
« Reply #6 on: February 26, 2019, 10:59:01 am »
Thank you for your interest.
Pram deals with the conversion of Julian Day (JD) to the Hebrew calendar. Input data: (JD) e.g. 2458541. Output data: day, month, year. As for JD, there is no need to use the Input function because JD will be downloaded automatically from another subprogram. Thank you very much to those who will change the code.

program J2Jewish;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  math;
procedure JD2Jewish(JD:integer; var j,m,d:integer);
function c4(x1,x3:integer):integer;
function c1(x1:integer):integer;
begin
c1:=floor((235*x1+1)/19);
end;
function q(x1:integer):integer;
begin
q:=floor(c1(x1)/1095);
end;
function r(x1:integer):integer;
begin
r:=c1(x1) mod 1095;
end;
function v1(x1:integer):integer;
begin
v1:=32336*q(x1) + floor((15*q(x1)+765433*r(x1)+12084)/25920);
end;
function v2(x1:integer):integer;
begin
v2:=v1(x1) + floor(6*(v1(x1)/7)) mod 2;
end;
function L2(x1:integer):integer;
begin
L2:=v2(x1+1) - V2(x1);
end;
function c2(x1:integer):integer;
var v3,v4:integer;
begin
v3:=2*(floor((L2(x1)+19)/15) mod 2);
v4:=floor((L2(x1-1)+7)/15) mod 2;
c2:=v2(x1)+v3+v4
end;
var L,c8,c9,c3:integer;
begin
L:=c2(x1+1)-c2(x1);
c8:=floor((L+7)/2) mod 15;
c9:=-(floor((385-L)/2) mod 15);
c3:=floor((384*x3+7)/13) + c8*floor((x3+4)/12) + c9*floor((x3+3)/12);
c4:=c2(x1)+c3;
end;

var
    y4,y1p,gam1,ksi1,mi1,ksi2,mi2,c41p,q,r,dz1,gam2,ksi3,c42p,c43p,dz2,gam3,mi3,z4,c,x3,x1:integer;

begin
y4:=JD-347821;
q:=floor(y4/1447);
r:=y4 mod 1447;
y1p:=49*q+floor((23*q+25920*r+13835)/765433);
gam1:=y1p+1;
ksi1:=floor((19*gam1+17)/235);
mi1:=gam1-floor((235*ksi1+1)/19);
c41p:=c4(ksi1,mi1);
dz1:=y4-c41p;
gam2:=gam1 + floor(dz1/33);
ksi2:=floor( (19*gam2+17)/235 );
mi2:=gam2-floor( (235*ksi2+1)/19 );
c42p:=c4(ksi2,mi2);
dz2:=y4-c42p;
gam3:=gam2 + floor(dz2/33);
ksi3:=floor( (19*gam3+17)/235 );  x1:=ksi3;
mi3:=gam3-floor( (235*ksi3+1)/19 ); x3:=mi3;
c43p:=c4(ksi3,mi3);
z4:=y4-c43p;
c:=floor((12-x3)/7);
j:=x1+1-c;
m:=x3+1;
d:=z4+1;
end;
var JD:integer;
    j,m,d:integer;
begin
writeln(' np. JD=2057986');
write('JD ='); readln(JD);
//JD:=2057986;
JD2Jewish(JD,  j,m,d);
writeln(j,' ',m,' ',d);
readln;
end.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Pascal
« Reply #7 on: February 26, 2019, 01:57:34 pm »
I refactored the code to make it easier to follow and to translate to QB64
Code: QB64: [Select]
  1. program J2Jewish;
  2. //{$APPTYPE CONSOLE}
  3. uses
  4.         SysUtils,
  5.         math;
  6.  
  7. begin
  8.         c1:=floor((235*x1+1)/19);
  9.  
  10. begin
  11.         q:=floor(c1(x1)/1095);
  12.  
  13. begin
  14.         r:=c1(x1) mod 1095;
  15.  
  16. begin
  17.         v1:=32336*q(x1) + floor((15*q(x1)+765433*r(x1)+12084)/25920);
  18.  
  19. begin
  20.         v2:=v1(x1) + floor(6*(v1(x1)/7)) mod 2;
  21.  
  22. begin
  23.         L2:=v2(x1+1) - V2(x1);
  24.  
  25. var v3,v4:integer;
  26. begin
  27.         v3:=2*(floor((L2(x1)+19)/15) mod 2);
  28.         v4:=floor((L2(x1-1)+7)/15) mod 2;
  29.         c2:=v2(x1)+v3+v4
  30.  
  31. var L,c8,c9,c3:integer;
  32. begin
  33.         L:=c2(x1+1)-c2(x1);
  34.         c8:=floor((L+7)/2) mod 15;
  35.         c9:=-(floor((385-L)/2) mod 15);
  36.         c3:=floor((384*x3+7)/13) + c8*floor((x3+4)/12) + c9*floor((x3+3)/12);
  37.         c4:=c2(x1)+c3;
  38.  
  39. procedure JD2Jewish(JD:integer; var j,m,d:integer);
  40.  
  41. var
  42.     y4,y1p,gam1,ksi1,mi1,ksi2,mi2,c41p,q,r,dz1,gam2,ksi3,c42p,c43p,dz2,gam3,mi3,z4,c,x3,x1:integer;
  43.  
  44. begin
  45.         y4:=JD-347821;
  46.         q:=floor(y4/1447);
  47.         r:=y4 mod 1447;
  48.         y1p:=49*q+floor((23*q+25920*r+13835)/765433);
  49.         gam1:=y1p+1;
  50.         ksi1:=floor((19*gam1+17)/235);
  51.         mi1:=gam1-floor((235*ksi1+1)/19);
  52.         c41p:=c4(ksi1,mi1);
  53.         dz1:=y4-c41p;
  54.         gam2:=gam1 + floor(dz1/33);
  55.         ksi2:=floor( (19*gam2+17)/235 );
  56.         mi2:=gam2-floor( (235*ksi2+1)/19 );
  57.         c42p:=c4(ksi2,mi2);
  58.         dz2:=y4-c42p;
  59.         gam3:=gam2 + floor(dz2/33);
  60.         ksi3:=floor( (19*gam3+17)/235 );  x1:=ksi3;
  61.         mi3:=gam3-floor( (235*ksi3+1)/19 ); x3:=mi3;
  62.         c43p:=c4(ksi3,mi3);
  63.         z4:=y4-c43p;
  64.         c:=floor((12-x3)/7);
  65.         j:=x1+1-c;
  66.         m:=x3+1;
  67.         d:=z4+1;
  68.  
  69. var JD:integer;
  70.     j,m,d:integer;
  71. begin
  72.         writeln(' np. JD=2057986');
  73.         write('JD ='); readln(JD);
  74.         //JD:=2057986;
  75.         JD2Jewish(JD,  j,m,d);
  76.         writeln(j,' ',m,' ',d);
  77.         readln;
  78.  

Offline Ryster

  • Newbie
  • Posts: 77
    • View Profile
Re: Pascal
« Reply #8 on: February 26, 2019, 02:09:55 pm »
Thanks, it looks nicer now.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Pascal
« Reply #9 on: February 26, 2019, 02:40:28 pm »
Most of these are pretty straightforward to translate:

function c1(x1:integer):integer;
begin
   c1:=floor((235*x1+1)/19);
end;


FUNCTION c1% (x1 AS INTEGER)
    c1% = (235 * x1 + 1) \ 19
END FUNCTION

*************

If nobody else gets around to it before me, I’ll work on translating it later tonight/tomorrow.  Today I’m booked up doing stuff with the wife, but my schedule should hopefully clear by tomorrow, I’m thinking.  (I can never be 100% certain until my wife verifies my schedule though.  After 26 years of marriage, I know my free time depends a lot more on HER schedule than it does mine.)  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Pascal
« Reply #10 on: February 26, 2019, 03:05:35 pm »
there's something you should know, integer is 16-bit
so how this is supposed to work when 2057986 overflows a 16-bit integer, I don't know.

Offline Ryster

  • Newbie
  • Posts: 77
    • View Profile
Re: Pascal
« Reply #11 on: February 26, 2019, 03:21:07 pm »
SMcNeill
You're right, your wife is more important.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Pascal
« Reply #12 on: February 26, 2019, 03:22:54 pm »
there's something you should know, integer is 16-bit
so how this is supposed to work when 2057986 overflows a 16-bit integer, I don't know.

LONGs would work, if INTEGER doesn’t.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Ryster

  • Newbie
  • Posts: 77
    • View Profile
Re: Pascal
« Reply #13 on: February 26, 2019, 03:34:36 pm »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Pascal
« Reply #14 on: February 26, 2019, 03:49:34 pm »
I can't get QB64 to produce the right answer
Code: QB64: [Select]
  1. ' program J2Jewish
  2.  
  3. DIM JD, j, m, d AS LONG
  4.  
  5. PRINT " np. JD=2057986"
  6. 'NPUT "JD = "; JD
  7. JD = 2057986
  8. JD2Jewish JD, j, m, d
  9. PRINT j; " "; m; " "; d
  10.  
  11.  
  12. FUNCTION c1% (x1 AS LONG)
  13.     c1% = INT((235 * x1 + 1) / 19)
  14.  
  15. FUNCTION q1% (x1 AS LONG)
  16.     q1% = INT(c1%(x1) / 1095)
  17.  
  18. FUNCTION r1% (x1 AS LONG)
  19.     r1% = c1%(x1) MOD 1095
  20.  
  21. FUNCTION v1% (x1 AS LONG)
  22.     v1% = 32336 * q1%(x1) + INT((15 * q1%(x1) + 765433 * r1%(x1) + 12084) / 25920)
  23.  
  24. FUNCTION v2% (x1 AS LONG)
  25.     v2% = v1%(x1) + INT(6 * (v1%(x1) / 7)) MOD 2
  26.  
  27. FUNCTION L2% (x1 AS LONG)
  28.     L2% = v2%(x1 + 1) - v2(x1)
  29.  
  30. FUNCTION c2% (x1 AS LONG)
  31.     DIM v3, v4 AS LONG
  32.     v3 = 2 * (INT((L2%(x1) + 19) / 15) MOD 2)
  33.     v4 = INT((L2%(x1 - 1) + 7) / 15) MOD 2
  34.     c2 = v2%(x1) + v3 + v4
  35.  
  36. FUNCTION c4% (x1 AS LONG, x3 AS LONG)
  37.     DIM L, c8, c9, c3 AS LONG
  38.     L = c2%(x1 + 1) - c2%(x1)
  39.     c8 = INT((L + 7) / 2) MOD 15
  40.     c9 = -(INT((385 - L) / 2) MOD 15)
  41.     c3 = INT((384 * x3 + 7) / 13) + c8 * INT((x3 + 4) / 12) + c9 * INT((x3 + 3) / 12)
  42.     c4 = c2%(x1) + c3
  43.  
  44. SUB JD2Jewish (JD AS LONG, j AS LONG, m AS LONG, d AS LONG)
  45.  
  46.     DIM y4, y1p, gam1, ksi1, mi1, ksi2, mi2, c41p, q, r, dz1, gam2, ksi3, c42p, c43p, dz2, gam3, mi3, z4, c, x3, x1 AS LONG
  47.  
  48.     y4 = JD - 347821
  49.     q = INT(y4 / 1447)
  50.     r = y4 MOD 1447
  51.     y1p = 49 * q + INT((23 * q + 25920 * r + 13835) / 765433)
  52.     gam1 = y1p + 1
  53.     ksi1 = INT((19 * gam1 + 17) / 235)
  54.     mi1 = gam1 - INT((235 * ksi1 + 1) / 19)
  55.     c41p = c4(ksi1, mi1)
  56.     dz1 = y4 - c41p
  57.     gam2 = gam1 + INT(dz1 / 33)
  58.     ksi2 = INT((19 * gam2 + 17) / 235)
  59.     mi2 = gam2 - INT((235 * ksi2 + 1) / 19)
  60.     c42p = c4(ksi2, mi2)
  61.     dz2 = y4 - c42p
  62.     gam3 = gam2 + INT(dz2 / 33)
  63.     ksi3 = INT((19 * gam3 + 17) / 235)
  64.     x1 = ksi3
  65.     mi3 = gam3 - INT((235 * ksi3 + 1) / 19)
  66.     x3 = mi3
  67.     c43p = c4(ksi3, mi3)
  68.     z4 = y4 - c43p
  69.     c = INT((12 - x3) / 7)
  70.     j = x1 + 1 - c
  71.     m = x3 + 1
  72.     d = z4 + 1
  73.  
however, the following FreeBasic code gives the same answer as that of the pascal version, namely 17, 1, 28
Code: QB64: [Select]
  1. ' program J2Jewish
  2.  
  3. function c1( byval x1 as short) as short
  4.         c1=int((235*x1+1)/19)
  5.  
  6. function q1(byval x1 as short) as short
  7.         q1=int(c1(x1)/1095)
  8.  
  9. function r1(byval x1 as short) as short
  10.         r1=c1(x1) mod 1095
  11.  
  12. function v1(byval x1 as short) as short
  13.         v1=32336*q1(x1) + int((15.0*q1(x1)+765433.0*r1(x1)+12084)/25920)
  14.  
  15. function v2(byval x1 as short) as short
  16.         v2=v1(x1) + int(6.0*(v1(x1)/7)) mod 2
  17.  
  18. function L2(byval x1 as short) as short
  19.         L2=v2(x1+1) - V2(x1)
  20.  
  21. function c2(byval x1 as short) as short
  22.         dim as short v3, v4
  23.         v3=2*(int((L2(x1)+19)/15) mod 2)
  24.         v4=int((L2(x1-1)+7)/15) mod 2
  25.         c2=v2(x1)+v3+v4
  26.  
  27. function c4(byval x1  as short, x3 as short) as short
  28.         dim as short L, c8, c9, c3
  29.         L=c2(x1+1)-c2(x1)
  30.         c8=int((L+7)/2) mod 15
  31.         c9=-(int((385-L)/2) mod 15)
  32.         c3=int((384.0*x3+7)/13) + c8*int((x3+4)/12) + c9*int((x3+3)/12)
  33.         c4=c2(x1)+c3
  34.  
  35. sub JD2Jewish(byval JD as short, byref j as short, byref m as short, byref d as short)
  36.  
  37.         dim as short y4,y1p,gam1,ksi1,mi1,ksi2,mi2,c41p,q,r,dz1,gam2,ksi3,c42p,c43p,dz2,gam3,mi3,z4,c,x3,x1
  38.  
  39.         y4=JD-347821
  40.         q=int(y4/1447)
  41.         r=y4 mod 1447
  42.         y1p=49*q+int((23.0*q+25920.0*r+13835)/765433)
  43.         gam1=y1p+1
  44.         ksi1=int((19.0*gam1+17)/235)
  45.         mi1=gam1-int((235.0*ksi1+1)/19)
  46.         c41p=c4(ksi1,mi1)
  47.         dz1=y4-c41p
  48.         gam2=gam1 + int(dz1/33)
  49.         ksi2=int( (19.0*gam2+17)/235 )
  50.         mi2=gam2-int( (235.0*ksi2+1)/19 )
  51.         c42p=c4(ksi2,mi2)
  52.         dz2=y4-c42p
  53.         gam3=gam2 + int(dz2/33)
  54.         ksi3=int( (19.0*gam3+17)/235 )
  55.         x1=ksi3
  56.         mi3=gam3-int( (235.0*ksi3+1)/19 )
  57.         x3=mi3
  58.         c43p=c4(ksi3,mi3)
  59.         z4=y4-c43p
  60.         c=int((12-x3)/7)
  61.         j=x1+1-c
  62.         m=x3+1
  63.         d=z4+1
  64.  
  65. 'main
  66.         dim as short JD, j, m, d
  67.  
  68.         print " np. JD=2057986"
  69.         'input "JD = "; JD
  70.         JD=2057986
  71.         JD2Jewish(JD,  j, m, d)
  72.         print j;" "; m; " "; d
  73.