# C# translatation

15 messages
Open this post in threaded view
|

## C# translatation

 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 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". > 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; > 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 > On Jan 8, 2017, at 5:46 PM, Sven Barth <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 In reply to this post by Sven Barth-2 > On Jan 8, 2017, at 5:46 PM, Sven Barth <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 > On Jan 8, 2017, at 6:55 PM, Ryan Joseph <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 In reply to this post by Ryan Joseph-3 On 08.01.2017 12:55, Ryan Joseph wrote: > >> On Jan 8, 2017, at 5:46 PM, Sven Barth <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 In reply to this post by Ryan Joseph-3 On 08.01.2017 13:01, Ryan Joseph wrote: > >> On Jan 8, 2017, at 6:55 PM, Ryan Joseph <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 In reply to this post by Sven Barth-2 > On Jan 8, 2017, at 7:54 PM, Sven Barth <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 On 08.01.2017 14:05, Ryan Joseph wrote: > >> On Jan 8, 2017, at 7:54 PM, Sven Barth <[hidden email]> 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 In reply to this post by Sven Barth-2 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. If I had to implement that on integers using standard pascal, this would cause me some headache ... Kind regards Bernd _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 El 08/01/2017 a las 17:31, Bernd Oppolzer escribió: > 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 := (b<>0) and (c<>0); -- _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 In reply to this post by Bernd Oppolzer Am 08.01.2017 17:32 schrieb "Bernd Oppolzer" <[hidden email]>: > > 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 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: C# translatation

 On Sun, Jan 8, 2017 at 12:22 PM, Sven Barth wrote: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...On Sun, Jan 8, 2017 at 11:46 AM, José Mejuto  wrote: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 := (b<>0) and (c<>0); 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:TRUE21thanks,Dmitry _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal