May 25, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | "Carlos" <carlos8294@msn.com> wrote in message news:acmglh$1cbb$1@digitaldaemon.com... > I could fix it. But the solution leads me to give you a suggestion: though you claim (in all your right) that resizing arrays is easy, resizing multidimensional arrays is really hard. I had to do this: > > int size=2; //or any value > double a[][]; > a.length=size; > for (int i=0;i<a.length;i++) > a[i].length=size; > > Too complicated. I can't think about an easy way to do it, but probably you > should. You're right, but I'm going to defer that for the moment. > Besides, there's no way to do this: > > void foo(double [][]a) { ... } > > double [2][2] a; > foo(a); > > The compiler says there's no compatibility. I hope it's a bug because if it's not, then you should do something about it. I'm afraid that D is stuck with that - the problem is that rectangular static arrays are laid out differently in memory than rectangular dynamic arrays. There's no easy conversion like there is for one dimensional arrays. > The compiler also says that it can't be done: > double a; > a=abs(a); > Because there's no difference between abs ( extended ) and abs ( int ). > Isn't it ridiculous? D follows the rule that a match must be unambiguous. I wish to avoid the mess C++ has gotten into with its layers of confusing rules about which is a "better" match. Trying to even understand the rule (so I could implement it) for template "better" matches about caused my brain to explode. D is not going to go down that merry path, no matter how sweet the poppies smell at first <g>. |
May 25, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | I think he's bringing up the point about floating point numbers that they teach you in school, that comparisons for equality of floats can fail even though the numbers are exceedingly similar. 1.0 != 1.0000000000000001 I'd love to have a "is approximately equal to" operator in D. The double-squiggly thing in math. ~ I don't think FP hardware usually has such an operation though, but it essentially boils down to this: bool approx_equal(float a,float b) { static const float epsilon = 1e-12f; // or whatever return fabs(b-a)<epsilon; } or if you want to get fancy, this is probably a better test (works for a wider range of values, but slower): bool approx_equal(float a,float b) { static const float epsilon = 1e-12f; // or whatever return fabs(b-a) <= max(fabs(a),fabs(b))*epsilon; } Deciding on a good epsilon is the difficult part. Different applications will need different epsilons, which is probably why nobody has standardized this yet. Sean "Walter" <walter@digitalmars.com> wrote in message news:acn8p8$1vri$1@digitaldaemon.com... > > "Carlos" <carlos8294@msn.com> wrote in message news:acmid0$1dtp$1@digitaldaemon.com... > > Due to the same program, this has come to me: how does D compare floating > > point numbers: C-like (floatingPoint1==floatingPoint2 is always false) or > > differently (sometimes floatingPoint1==floatingPoint2 is true)? For > example, > > > > double a=1; > > double b=1; > > if (a==b) ... ; > > > > It should be true. > > You're right, it should be true. Is it not in D? |
May 25, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | "Carlos" <carlos8294@msn.com> wrote in message news:acn27k$1qmc$1@digitaldaemon.com... > I understand that, but what about a swap function? It also has to be overloaded for ALL types and the parameters have to be out or inout, but then it has to cast float or double and the compiler says (because it also happened to me) that cast(extended) { something I don't remember well } is not a lvalue. Probably you (or Walter) should reconsider your ideas about the program. This is okay. Since function actually takes _pointer_ to argument, it needs to know the size of the value being pointed to. And if you would be able to cast float to extended, it would get 4 bytes where it expects to see 10... So, for swap(), the ONLY way to write it properly is to overload it for all types, or to make it template if language supports this... |
May 25, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <seanpalmer@earthlink.net> wrote in message news:acne4m$25k1$1@digitaldaemon.com... > Deciding on a good epsilon is the difficult part. Different applications will need different epsilons, which is probably why nobody has standardized > this yet. Epsilon is the wrong approach. The best way would be to have a function that rounds each number to a specified number of bits of precision, and then compare for equality. |
May 26, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> escribió en el mensaje news:acngg7$27os$1@digitaldaemon.com... > "Carlos" <carlos8294@msn.com> wrote in message news:acn27k$1qmc$1@digitaldaemon.com... > > > I understand that, but what about a swap function? It also has to be overloaded for ALL types and the parameters have to be out or inout, but then it has to cast float or double and the compiler says (because it also > > happened to me) that cast(extended) { something I don't remember well } is > > not a lvalue. Probably you (or Walter) should reconsider your ideas about > > the program. > > This is okay. Since function actually takes _pointer_ to argument, it needs to know the size of the value being pointed to. And if you would be able to cast float to extended, it would get 4 bytes where it expects to see 10... > > So, for swap(), the ONLY way to write it properly is to overload it for all types, or to make it template if language supports this... > Yes, but if I explicitely write (to try): char a,b; swap ( (char) a, (char) b ); (swap is a function that I wrote) it works. |
May 26, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> escribió en el mensaje news:acn93k$2028$1@digitaldaemon.com... > > > The compiler also says that it can't be done: > > double a; > > a=abs(a); > > Because there's no difference between abs ( extended ) and abs ( int ). > > Isn't it ridiculous? > > D follows the rule that a match must be unambiguous. I wish to avoid the mess C++ has gotten into with its layers of confusing rules about which is a > "better" match. Trying to even understand the rule (so I could implement it) > for template "better" matches about caused my brain to explode. D is not going to go down that merry path, no matter how sweet the poppies smell at first <g>. > Yes, but I don't think it's ambiguous to do that, because double should automatically go with extended not with int, because double isn't like an int. |
May 26, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | "Carlos" <carlos8294@msn.com> wrote in message news:acp9cj$2602$1@digitaldaemon.com... > Yes, but if I explicitely write (to try): > > char a,b; > swap ( (char) a, (char) b ); > > (swap is a function that I wrote) it works. And how does your swap() look? |
May 26, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | "Carlos" <carlos8294@msn.com> wrote in message news:acmglh$1cbb$1@digitaldaemon.com... > I could fix it. But the solution leads me to give you a suggestion: though you claim (in all your right) that resizing arrays is easy, resizing multidimensional arrays is really hard. I had to do this: > > int size=2; //or any value > double a[][]; > a.length=size; > for (int i=0;i<a.length;i++) > a[i].length=size; > > Too complicated. I can't think about an easy way to do it, but probably you > should. > Besides, there's no way to do this: > > void foo(double [][]a) { ... } > > double [2][2] a; > foo(a); > > The compiler says there's no compatibility. I hope it's a bug because if > it's not, then you should do something about it. > The compiler also says that it can't be done: > double a; > a=abs(a); > Because there's no difference between abs ( extended ) and abs ( int ). > Isn't it ridiculous? > I think this was discussed before. double and extended are two seperate types, so if only abs(int) and abs(extended) are defined the compiler does not know which one to use... You need to define abs(double): double abs (in double dValue) { return (cast (double) abs (cast (extended) dValue)); } I typed this in Outlook, so I hope I got the syntax right... -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
May 26, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> escribió en el mensaje news:acq2sn$2u4n$1@digitaldaemon.com... > "Carlos" <carlos8294@msn.com> wrote in message news:acp9cj$2602$1@digitaldaemon.com... > > > Yes, but if I explicitely write (to try): > > > > char a,b; > > swap ( (char) a, (char) b ); > > > > (swap is a function that I wrote) it works. > > And how does your swap() look? > It can't be simpler: void swap ( char a, char b) { char z=a; a=b; b=z; } and for int and extended it's exactly the same. |
May 27, 2002 Re: error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos | "Carlos" <carlos8294@msn.com> wrote in message news:acrm97$17s8$1@digitaldaemon.com... > > > char a,b; > > > swap ( (char) a, (char) b ); > It can't be simpler: > > void swap ( char a, char b) > { > char z=a; a=b; b=z; > } > > and for int and extended it's exactly the same. Since you've defined it for chars, it'll work for chars. There is nothing wrong in casting a char to char - it still continues to be an lvalue. |
Copyright © 1999-2021 by the D Language Foundation