Re: writefln on interface array
On Wed, 07 Apr 2010 07:13:07 -0400, strtr st...@spam.com wrote: Is it possible to have this output [null,1] in stead of Error: std.format formatArg? interface I{} class C:I{ int index; char[] toString(){ return toString(index) } } I[2] two_i; I[1] = new C(); writefln(two_i); Would be handy for debugging ;) Don't think this is exactly what you are looking for, but have you tried writeln? writefln requires the first argument to be a string, it didn't used to, but it was determined that the resulting behavior when you want to just print a string is too confusing. -Steve
Re: 'Undefined reference' linking errors
Thanks to everyone for the responses. I'll respond to Bearophile's detailed comments: Few notes: - opCall() of AvgWeighted was abstract. - keep in mind that in D classes are CamelCase; - variable names are written like weightSum (but once in a while a underscore doesn't kill). I think it's obvious from my syntax that my background is with C; I'm not experienced with Java, C# etc. This may explain some of the problems I'm having. Regarding opCall I was following the syntax described here: http://www.digitalmars.com/d/2.0/operatoroverloading.html#FunctionCall ... but clearly without understanding it properly. What I was aiming for was a bit smartarse -- to have a class which could in some cases be treated as a function. Each of these classes (later ones will be more sophisticated) is meant to be a data analysis tool which takes a dataset of user-object ratings and user and object reputation values and helps aggregate the ratings and in the process update the reputation values. The aim was that if you just wanted a once-off analysis you could use the class in a throwaway fashion -- hence the use of, avg_weighted(..); rather than avg_weighted aw(.); The aim is that you would use the second if you were interested in employing the analysis multiple times, and that the class will have other functions that can be used for different or secondary analyses from the main one. It's maybe not the best way to approach what I want to do, but since D is a new language for me, I thought I would be playful with it and try and bend it around in some interesting ways. - Be careful because ref arguments are tricky. The choice is deliberate here, because the arrays passed to the constructor (or opCall) are meant to be modified. - There is a line like foreach (r; reputationUser) r = 1; that can be a bug. I guess that I should put a 'double' in front of the r, no? In any case, I guess there is a better way of setting all elements of an array equal to 1.0. - foreach (objectID, rating; reputationObject) rating /= weightSum[objectID]; can be another bug. ... so should be uint objectID, double rating ... ? I think it's obvious that I want each the value of each element of reputationObject to be divided by the value of the corresponding element of weightSum -- is there a more intelligent way of doing this? Reading Andrei Alexandrescu's article on Dr Dobb's gave me the impression something could be done using chain(), but I couldn't work out how (and probably misunderstood). - Use better attribute names in Rating struct, when you need to comment a variable name then it's often a wrong name. - To create structs you can most times use the syntax I've used in the main. - In methods/functions divide your code into paragraphs; - keep your indentations more coherent It's nice to see the stress in D on well-written code. Thanks for taking the time to clean up mine. :-) - I suggest to add contracts and unittests. As you might have guessed, I'm not a developer -- can you provide more info? Thanks best wishes, -- Joe
Re: 'Undefined reference' linking errors
Joseph Wakeling wrote: - opCall() of AvgWeighted was abstract. - keep in mind that in D classes are CamelCase; - variable names are written like weightSum (but once in a while a underscore doesn't kill). I think it's obvious from my syntax that my background is with C; I'm not experienced with Java, C# etc. This may explain some of the problems I'm having. Regarding opCall I was following the syntax described here: http://www.digitalmars.com/d/2.0/operatoroverloading.html#FunctionCall ... but clearly without understanding it properly. I have experience with C++ and still don't understand why opCall exists. :) I think I heard that opCall was needed to create struct objects before structs had constructors in D. Now structs do have constructors, which sometimes conflict with opCall. :) What I was aiming for was a bit smartarse -- to have a class which could in some cases be treated as a function. I consider myself a function-happy programmer. To me, not everything is a class. :) Each of these classes (later ones will be more sophisticated) is meant to be a data analysis tool which takes a dataset of user-object ratings and user and object reputation values and helps aggregate the ratings and in the process update the reputation values. The aim was that if you just wanted a once-off analysis you could use the class in a throwaway fashion -- hence the use of, avg_weighted(..); It could be a function that instantiates on object, that would be thrown away. It's maybe not the best way to approach what I want to do, but since D is a new language for me, I thought I would be playful with it and try and bend it around in some interesting ways. No harm in that. :) - Be careful because ref arguments are tricky. The choice is deliberate here, because the arrays passed to the constructor (or opCall) are meant to be modified. D has reference types. When you pass a class object to a function by-value, it is actually passed-by-reference. I think this is the same in Java. You can imagine the function parameter being a pointer behind the scenes. ClassType variable = new ClassType; ClassType variable2 = variable; You have a single object created with new, and two variables that refer to that object. - There is a line like foreach (r; reputationUser) r = 1; that can be a bug. I guess that I should put a 'double' in front of the r, no? In any case, I guess there is a better way of setting all elements of an array equal to 1.0. You would put 'ref' in front of the foreach variables. Otherwise they are copies in the foreach loop. - foreach (objectID, rating; reputationObject) rating /= weightSum[objectID]; can be another bug. ... so should be uint objectID, double rating ... ? Same: Should probably be 'ref rating' if you want to modify reputationObject. I think it's obvious that I want each the value of each element of reputationObject to be divided by the value of the corresponding element of weightSum -- is there a more intelligent way of doing this? - I suggest to add contracts and unittests. As you might have guessed, I'm not a developer -- can you provide more info? They are of the greater features of D. :) You can define function pre- and post-conditions and struct and class invariants. You can have unittest blocks... Great stuff! :) http://digitalmars.com/d/2.0/unittest.html http://digitalmars.com/d/2.0/dbc.html http://digitalmars.com/d/2.0/class.html#Invariant Ali
string to real conversion losing data
Greetings and salutations! Will someone be so kind as to explain why this is happening? import std.stdio; import std.conv; void main() { char[][] strRealVals = [ 14539.34,1230.00,361.62,1613.10,,,0.00 ]; real rTotal = 0; foreach (char[] s; strRealVals) { writefln(Real value is: ~ s); real r = 0.00; if (s != ) r = std.conv.toReal(s); rTotal += r; } writefln(std.string.toString(rTotal)); writefln(rTotal); } When I run this program, I get this: 16:51:35.54realtest Real value is: 14539.34 Real value is: 1230.00 Real value is: 361.62 Real value is: 1613.10 Real value is: Real value is: Real value is: 0.00 17744.1 17744.1 If I add these numbers, the outcome should be 17744.06. Any ideas? I am using Digital Mars D Compiler v1.046. thanks, josé
Re: string to real conversion losing data
W dniu 08.04.2010 23:02, jicman pisze: Greetings and salutations! Will someone be so kind as to explain why this is happening? import std.stdio; import std.conv; void main() { char[][] strRealVals = [ 14539.34,1230.00,361.62,1613.10,,,0.00 ]; real rTotal = 0; foreach (char[] s; strRealVals) { writefln(Real value is: ~ s); real r = 0.00; if (s != ) r = std.conv.toReal(s); rTotal += r; } writefln(std.string.toString(rTotal)); writefln(rTotal); } When I run this program, I get this: 16:51:35.54realtest Real value is: 14539.34 Real value is: 1230.00 Real value is: 361.62 Real value is: 1613.10 Real value is: Real value is: Real value is: 0.00 17744.1 17744.1 If I add these numbers, the outcome should be 17744.06. Any ideas? I am using Digital Mars D Compiler v1.046. thanks, jos� it is looking ok to me, try this one and thing why it is like that :) import std.stdio; import std.conv; void main() { writefln(17744.06); }
Re: string to real conversion losing data
If I add these numbers, the outcome should be 17744.06. Any ideas? I am using Digital Mars D Compiler v1.046. josé This prints the same values, using latest D2: import std.stdio, std.conv; void main() { real tot = 0; foreach(el; [14539.34,1230.00,361.62,1613.10,0.00]) tot += to!real(el); writefln(%.5f\n, tot); // 17744.06000 tot = 0; foreach(el; [14539.34,1230.00,361.62,1613.10,0.00]) tot += to!real(el); writefln(%.5f\n, tot); // 17744.06000 } Bye, bearophile
Re: string to real conversion losing data
Hello bearophile, writefln(%.5f\n, tot); // 17744.06000 Never trust your output function :) (e.i. always check to see if it's doing what you think it is.) Back in the bad old days, a guy I knew spent a while debugging a problem that turned out to be that he was loading data as float but storing it into an int (think an invalid scanf format string). Every time he checked things though things seems OK because he had the same bug in his output as well (think printf). -- ... IXOYE
Re: string to real conversion losing data
wrzosk Wrote: W dniu 08.04.2010 23:02, jicman pisze: it is looking ok to me, try this one and thing why it is like that :) import std.stdio; import std.conv; void main() { writefln(17744.06); } Ok, I'll bite... I don't get it. does that mean that writefln is buggy? How can I format and write this to a file? Is there a formatting library? thanks, jose
Re: string to real conversion losing data
Ali Çehreli wrote: Try for example %.18f for 'real'. Well... That will be too many digits unless the number of digits before the decimal point is zero. The decimal digits before and after the point should be 18 for most accurate representation. But the OP probably doesn't need that many digits anyway. Here are more options: http://digitalmars.com/d/2.0/phobos/std_format.html Ali