Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-01

C# translatation

15 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

I’m going to attempt to translate a perlin noise function (from https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a few things that stopped me from starting.

1) What is & doing in this assignment? I see this many places in assignments.

int h = hash & 15;

2) % is the mod operator in pascal right? So what is the modular assignment %=? num = num mod repeat?

p[x] = permutation[x%256];
if (repeat > 0) num %= repeat;

3) Adding the tertiary operator?

return ((h&1) == 0 ? u : -u)+((h&2) == 0 ? v : -v);

Here’s an easier example. Did I do that correctly?

if h < 8 then
	u := x
else
	u := y;
double u = h < 8 /* 0b1000 */ ? x : y;

Thanks for helping.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 5 lines
I’m going to attempt to translate a perlin noise function (from https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a few things that stopped me from starting. 1) What is & doing in this assignment? I see this many places in assignments. int h = hash & 15;

That's an "and".

Show quoted text 5 lines
2) % is the mod operator in pascal right? So what is the modular assignment %=? num = num mod repeat? p[x] = permutation[x%256]; if (repeat > 0) num %= repeat;

Correct for both.

3) Adding the tertiary operator? return ((h&1) == 0 ? u : -u)+((h&2) == 0 ? v : -v);
if h and 1 = 0 then
  result := u
else
  result := -u;
if h and 2 = 0 then
  result := result + v
else
  result := result - v;
Show quoted text 9 lines
Here’s an easier example. Did I do that correctly? if h < 8 then u := x else u := y; double u = h < 8 /* 0b1000 */ ? x : y;

Correct.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
Show quoted text 10 lines
On Jan 8, 2017, at 5:46 PM, Sven Barth wrote: if h and 1 = 0 then result := u else result := -u; if h and 2 = 0 then result := result + v else result := result - v;

Thanks Sven, I appreciate it. I forgot about binary and operator some how also...

C# is pretty nice language imo. These 2 “Perlin” methods appear to initializers on the class. Does anyone dispute that? I guess the value of repeat should be -1 by default and presumably both methods are called when the class is initialized. Otherwise I don’t see how p could be set.

public class Perlin {

	public int repeat;
	public Perlin(int repeat = -1) {
		this.repeat = repeat;
	}
	private static readonly int[] p;
	static Perlin() {
		p = new int[512];
		for(int x=0;x<512;x++) {
			p[x] = permutation[x%256];
		}
	}

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to Sven Barth: That's an "and". Correct for both. if h and 1 = 0 then result := u else result := -u; if…
Show quoted text 9 lines
On Jan 8, 2017, at 5:46 PM, Sven Barth wrote: > 2) % is the mod operator in pascal right? So what is the modular assignment %=? num = num mod repeat? > > p[x] = permutation[x%256]; > > if (repeat > 0) num %= repeat; Correct for both.

why am I getting this error? Operator is not overloaded: "Double" mod “LongInt”. Mod can’t be used with double and integers like in C?

repeatValue: integer;
x, y, z: double;
	if (repeatValue > 0) then
		begin
			x := x mod repeatValue;
			y := y mod repeatValue;
			z := z mod repeatValue;
		end;

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to Ryan Joseph
On Jan 8, 2017, at 6:55 PM, Ryan Joseph wrote: why am I getting this error? Operator is not overloaded: "Double" mod “LongInt”. Mod can’t be used with double and integers like in C?

Sorry for the repeat. Same error here but "Operator is not overloaded: "Double" and “Byte””. This worked in c as "xi = x & 255"

x: double;
xi: integer;
	xi := x and 255;

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph: why am I getting this error? Operator is not overloaded: "Double" mod “LongInt”. Mod…
Ryan Joseph wrote 23 lines
> On Jan 8, 2017, at 5:46 PM, Sven Barth wrote: > >> 2) % is the mod operator in pascal right? So what is the modular assignment %=? num = num mod repeat? >> >> p[x] = permutation[x%256]; >> >> if (repeat > 0) num %= repeat; > > Correct for both. why am I getting this error? Operator is not overloaded: "Double" mod “LongInt”. Mod can’t be used with double and integers like in C? repeatValue: integer; x, y, z: double; if (repeatValue > 0) then begin x := x mod repeatValue; y := y mod repeatValue; z := z mod repeatValue; end;

Ehm, right. Add the Math unit, it contains an operator overload for floating points. Alternatively you can use (also from the Math unit) the FMod() function.

Regards,
Sven

Reply by email

Sven Barth
to Ryan Joseph: Sorry for the repeat. Same error here but "Operator is not overloaded: "Double" and…
Ryan Joseph wrote 12 lines
> On Jan 8, 2017, at 6:55 PM, Ryan Joseph wrote: > > why am I getting this error? Operator is not overloaded: "Double" mod “LongInt”. Mod can’t be used with double and integers like in C? Sorry for the repeat. Same error here but "Operator is not overloaded: "Double" and “Byte””. This worked in c as "xi = x & 255" x: double; xi: integer; xi := x and 255;

If you look at the C# code again you'll see this:

=== code begin ===

xi = (int)x & 255;

=== code end ===

Including the information contained in the comment on the right I'd say it should be this:

=== code begin ===

xi := Floor(x) and 255;

=== code end ===

(Floor() is from unit Math)

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth: Ehm, right. Add the Math unit, it contains an operator overload for floating points.…
Show quoted text 5 lines
On Jan 8, 2017, at 7:54 PM, Sven Barth wrote: Ehm, right. Add the Math unit, it contains an operator overload for floating points. Alternatively you can use (also from the Math unit) the FMod() function.

Hmmm, adding Math didn’t help and the FMod function is not found even. http://www.freepascal.org/docs-html/rtl/math/index-5.html doesn’t show this function exists either.

I thought (int)x was just type casting and didn’t actually transform the number. That’s confusing.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 8 lines
> On Jan 8, 2017, at 7:54 PM, Sven Barth wrote: > > Ehm, right. Add the Math unit, it contains an operator overload for > floating points. Alternatively you can use (also from the Math unit) the > FMod() function. Hmmm, adding Math didn’t help and the FMod function is not found even. http://www.freepascal.org/docs-html/rtl/math/index-5.html doesn’t show this function exists either.

Ah, my bad. That's a recent addition in trunk (I tested with trunk). However you can simply use the implementation from the commit for your own code (see here:
http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/objpas/math.pp?r1=33130&r2=34968 )

I thought (int)x was just type casting and didn’t actually transform the number. That’s confusing.

Well, it's the same in C++...

Regards,
Sven

Reply by email

Bernd Oppolzer
to Sven Barth: That's an "and". Correct for both. if h and 1 = 0 then result := u else result := -u; if…
Sven Barth wrote 7 lines
On 08.01.2017 10:55, Ryan Joseph wrote: > I’m going to attempt to translate a perlin noise function (from https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a few things that stopped me from starting. > > 1) What is & doing in this assignment? I see this many places in assignments. > > int h = hash & 15; That's an "and".

Please keep in mind that & is a bitwise and in C
whereas && is a logical and;
this makes much difference and has to be implemented or converted differently
with Pascal, IMO.

the closest equivalence to bitwise and in Pascal are set intersections, IMO.

If I had to implement that on integers using standard pascal, this would cause me some headache ...

Kind regards

Bernd

Reply by email

José Mejuto
to Bernd Oppolzer
Bernd Oppolzer wrote 9 lines
Please keep in mind that & is a bitwise and in C whereas && is a logical and; this makes much difference and has to be implemented or converted differently with Pascal, IMO. the closest equivalence to bitwise and in Pascal are set intersections, IMO. If I had to implement that on integers using standard pascal, this would cause me some headache ...

Both bitwise operations (vars are integers).

C / C++:

a = b & c;

Pascal:

a := b and c;

Both work the same way but pascal does not have the assignment and testing mess that can be written in C like:

C / C++:

a = b && c;

Pascal:

a := (b0) and (c0);

Reply by email

Sven Barth
to Bernd Oppolzer: Please keep in mind that & is a bitwise and in C whereas && is a logical and; this makes…
"Bernd Oppolzer" wrote 6 lines
Am 08.01.2017 um 11:46 schrieb Sven Barth: > > On 08.01.2017 10:55, Ryan Joseph wrote: >> >> I’m going to attempt to translate a perlin noise function (from

https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a few things that stopped me from starting.

>> >> 1) What is & doing in this assignment? I see this many places in

assignments.

Show quoted text 9 lines
>> >> int h = hash & 15; > > That's an "and". Please keep in mind that & is a bitwise and in C whereas && is a logical and; this makes much difference and has to be implemented or converted

differently

with Pascal, IMO. the closest equivalence to bitwise and in Pascal are set intersections,

IMO.

The closest equivalence to bitwise and in Pascal is bitwise and. The operators "and", "or", "xor" and "not" are logical or bitwise depending on their arguments.

Regards,
Sven

Reply by email

Dmitry Boyarintsev
to Sven Barth
Sven Barth wrote 5 lines
The closest equivalence to bitwise and in Pascal is bitwise and. The operators "and", "or", "xor" and "not" are logical or bitwise depending on their arguments.

With that said...

José Mejuto wrote 12 lines
Both work the same way but pascal does not have the assignment and testing mess that can be written in C like: C / C++: a = b && c; Pascal: a := (b0) and (c0);

There's another approach to resolved that.

The following code works as expected

{$mode objfpc}
var
  b,c : integer;
  a   : Boolean;
  ai  : integer;
begin
  b:=2;
  c:=3;
  ai:=b and c;
  a:=boolean(b) and boolean(c);
  writeln(a);
  writeln(ai);
  writeln(byte(a));
end.

output:
TRUE
2
1

thanks,
Dmitry

Reply by email

Bernd Oppolzer
to Sven Barth: https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a few things that…
Sven Barth wrote 37 lines
Am 08.01.2017 17:32 schrieb "Bernd Oppolzer" < >: > > Am 08.01.2017 um 11:46 schrieb Sven Barth: >> >> On 08.01.2017 10:55, Ryan Joseph wrote: >>> >>> I’m going to attempt to translate a perlin noise function (from https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a few things that stopped me from starting. >>> >>> 1) What is & doing in this assignment? I see this many places in assignments. >>> >>> int h = hash & 15; >> >> That's an "and". > > > Please keep in mind that & is a bitwise and in C > whereas && is a logical and; > this makes much difference and has to be implemented or converted differently > with Pascal, IMO. > > the closest equivalence to bitwise and in Pascal are set intersections, IMO. The closest equivalence to bitwise and in Pascal is bitwise and. The operators "and", "or", "xor" and "not" are logical or bitwise depending on their arguments. Regards, Sven

This depends on the Pascal compiler you use;
the old Wirth "Standard" yields syntax errors, if you use and etc. on types other than boolean:

1 LINE # D/NEST LVL < STANFORD PASCAL, OPPOLZER VERSION OF 01.2017 > 18:32:47 08.01.2017 PAGE 1
------ ------ --- ---- ---

         1           ) program TESTAND ( OUTPUT ) ;
         2           )
         3           ) var X , Y : INTEGER ;
         4    360D  1)
         5    360D  1) begin (* HAUPTPROGRAMM *)
         6           )   X := 12 ;
         7      1N  1)   Y := 25 ;
         8      1N  1)   WRITELN ( 'x and y' , X and Y ) ;
      ****                                              E134
         9      1N  1) end (* HAUPTPROGRAMM *) .
      ****       1 SYNTAX ERROR(S) DETECTED.
      ****       9 LINE(S) READ,    0 PROCEDURE(S) COMPILED,
      ****      17 P_INSTRUCTIONS GENERATED,   0.02 SECONDS IN COMPILATION.
      ****  LAST ERROR/WARNING ON LINE -->   8
      ****  ERROR/WARNING CODES FOR THIS PROGRAM :
      ****   134 : ILLEGAL TYPE OF OPERAND(S).

I agree that bitwise and etc. is a useful extension and will put it on my to-do list.

Have a nice day.

Kind regards

Bernd

Reply by email

Sven Barth
to Bernd Oppolzer
Bernd Oppolzer wrote 42 lines
Am 08.01.2017 um 18:22 schrieb Sven Barth: > > Am 08.01.2017 17:32 schrieb "Bernd Oppolzer" > < >: > > > > Am 08.01.2017 um 11:46 schrieb Sven Barth: > >> > >> On 08.01.2017 10:55, Ryan Joseph wrote: > >>> > >>> I’m going to attempt to translate a perlin noise function (from > https://gist.github.com/Flafla2/f0260a861be0ebdeef76) but there are a > few things that stopped me from starting. > >>> > >>> 1) What is & doing in this assignment? I see this many places in > assignments. > >>> > >>> int h = hash & 15; > >> > >> That's an "and". > > > > > > Please keep in mind that & is a bitwise and in C > > whereas && is a logical and; > > this makes much difference and has to be implemented or converted > differently > > with Pascal, IMO. > > > > the closest equivalence to bitwise and in Pascal are set > intersections, IMO. > > The closest equivalence to bitwise and in Pascal is bitwise and. > The operators "and", "or", "xor" and "not" are logical or bitwise > depending on their arguments. > > Regards, > Sven > > This depends on the Pascal compiler you use; the old Wirth "Standard" yields syntax errors, if you use and etc. on types other than boolean:

We're here on the FPC mailing list, so I personally only care about the capabilities of FPC and that definitely supports bitwise operations (and so do TP and Delphi).

Regards,
Sven

Reply by email