www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ConvExt.d v0.2 please review, comment, suggest, and / or correct.

reply David L. Davis <SpottedTiger yahoo.com> writes:
Well per Walter advice in to below message,

D forum thread: "process improvement for phobos"
http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/20328

I'd like to resubmit to my fellow D developers for comments and review, the
following code that's been submitted to Walter for including into Phobos.

Recently, D v0.120 has forced a number of correction to be made into this code
(a lot of additional cast()s had to put into place...which was kool, a better
compiler means better executing code), but in so doing I found some areas that
needed a bit more tighting up. 

Please free feel to give some suggests and / or correction to the following
code, that's designed to fill in the missing "string to floating-point" function
gap in std.conv. Note I'm in no way a floating-point expert, but just a guy
trying in improve and move D forward...one line or one function at a time if I
possibly can.
























































































































































































































































































































































































































































































































Thanks in advance,
David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Apr 09 2005
next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
Little bit out of scope but I would like to rise question about
string-to-value  function signatures again.

My proposal is to use

int valueOf( string s , out T v, in T defaultValue = 0 )

Where T is a numeric type int, uint, etc.
Functions return number of valid characters processed.

Such functions are flexible enough to allow blind and controllable
conversions.

Andrew.



"David L. Davis" <SpottedTiger yahoo.com> wrote in message 
news:d3903a$vr8$1 digitaldaemon.com...
 Well per Walter advice in to below message,

 D forum thread: "process improvement for phobos"
 http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/20328

 I'd like to resubmit to my fellow D developers for comments and review, 
 the
 following code that's been submitted to Walter for including into Phobos.

 Recently, D v0.120 has forced a number of correction to be made into this 
 code
 (a lot of additional cast()s had to put into place...which was kool, a 
 better
 compiler means better executing code), but in so doing I found some areas 
 that
 needed a bit more tighting up.

 Please free feel to give some suggests and / or correction to the 
 following
 code, that's designed to fill in the missing "string to floating-point" 
 function
 gap in std.conv. Note I'm in no way a floating-point expert, but just a 
 guy
 trying in improve and move D forward...one line or one function at a time 
 if I
 possibly can.









 code


 runtime












 toIfloat(),


 which

 I've




 functions.





  ' --------------------------------------------------------------------------



 function





























































































 char[] ).unittest" );





























 char[] ).unittest" );




















































 real.max ) );

























 char[] ).unittest" );











 cast(real)ifloat.min.im ) );









 123.45i ) );



 456.77i ) );



































 char[] ).unittest" );






 cast(real)idouble.min.re ) );

 cast(real)idouble.min.im ) );





 cast(real)idouble.max.re ) );

 cast(real)idouble.max.im ) );
































 char[] ).unittest" );




 ireal.min ) );





 ireal.max ) );
























 char[] ).unittest" );




 cfloat.min ) );



 cfloat.max ) );





















 char[] ).unittest" );






























 char[] ).unittest" );




 creal.min ) );





 creal.max ) );

 cast(real)creal.max.re ) );

 cast(real)creal.max.im ) );





 cast(real)1.2345e-5+0i.re ) );

 cast(real)1.2345e-5+0i.im ) );



 0.0e-0+0i ) );




 s2 )

















































 Thanks in advance,
 David L.

 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" 
Apr 09 2005
prev sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"David L. Davis" <SpottedTiger yahoo.com> wrote in message 
news:d3903a$vr8$1 digitaldaemon.com...
 Well per Walter advice in to below message,

 D forum thread: "process improvement for phobos"
 http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/20328

 I'd like to resubmit to my fellow D developers for comments and review, 
 the
 following code that's been submitted to Walter for including into Phobos.

 Recently, D v0.120 has forced a number of correction to be made into this 
 code
 (a lot of additional cast()s had to put into place...which was kool, a 
 better
 compiler means better executing code), but in so doing I found some areas 
 that
 needed a bit more tighting up.

 Please free feel to give some suggests and / or correction to the 
 following
 code, that's designed to fill in the missing "string to floating-point" 
 function
 gap in std.conv. Note I'm in no way a floating-point expert, but just a 
 guy
 trying in improve and move D forward...one line or one function at a time 
 if I
 possibly can.
cool. comments inline


My first comment is the comment style. Does anything else in phobos use this comment style? Looking in std.conv I see comments like /************************* * Convert blah to foo. * Gammar: digit(digit) */ The use of /+ and ' seems non-standard. I'm not sure exactly what you intend to send Walter but if this stuff is to go in std.convext (though I don't see why it can't go into std.conv if you ask me) I would get it into the exact format that you want to appear in phobos. It looks like none of the functions have comments preceding them in the style of std.conv - those comments should be in there to explain just what is allowed and what isn't.






 code


 runtime












 toIfloat(),


 which

 I've




 functions.





  ' --------------------------------------------------------------------------



 function





















































Personally I'd remove these toBit conversions. The strings it recognizes seem somewhat arbitrary. Why "yes" but not "oui"? If anything I would just have toBit recognize what std.format does with a bit, which is print "true" or "false". And given that restriction it would be simpler to just have a comparison with "true" or "false". One trouble with toBit(char c) for example is the numeric nature char might lead people to guess that c!=0 is the test for toBit instead of comparing with some ascii codes.




All these floating point functions seem to be very small wrappers around std.math2.atof. Why not put these in math2? I can see the desire to mimic the std.conv module but IMO that isn't a strong enough reason to make another module. Also if the idea is to follow std.conv then I would have the ones that cast check for overflow etc just like toShort checks for overflow after getting the result of toInt. [snip]






why 0f?

I don't see where the index of the "i" shows up. If you only parse "<float>i" then you can just check the last char instead of doing an ifind.







I'm not sure what f.nan == true is supposed to test. Do you want to see if f is a nan? The isnan function in std.math is for that.





[snip]

 s2 )





why the dup? You shouldn't need to change s so there is no need to make a copy. Also I can't tell exactly what string parse into complex numbers. For example will the string "1-2i" parse correctly? I think it should. In any case documenting what works and what doesn't is important.

































Apr 10 2005
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <d3cq53$19hh$1 digitaldaemon.com>, Ben Hinkle says...
"David L. Davis" <SpottedTiger yahoo.com> wrote in message 
news:d3903a$vr8$1 digitaldaemon.com...


My first comment is the comment style. Does anything else in phobos use this comment style? Looking in std.conv I see comments like /************************* * Convert blah to foo. * Gammar: digit(digit) */
Yeah, the comment style is what I started to use on code on my web-site (something I remembere picking up from my programming classes back in my college days), but I don't really expect Walter to keep them. :) And since I've passed all rights of the code to him, he can make the changes he needs to make.
The use of /+ and ' seems non-standard. I'm not sure exactly what you intend 
to send Walter but if this stuff is to go in std.convext (though I don't see 
why it can't go into std.conv if you ask me) I would get it into the exact 
format that you want to appear in phobos. It looks like none of the 
functions have comments preceding them in the style of std.conv - those 
comments should be in there to explain just what is allowed and what isn't.
One of the nice things about D is that it gives you choices...in this case two multi-commenting blocks like "/* */" and "/+ +/". But it's hard to find a color-code syntax within the text editor to support more than one multi-comment block set of symbols. So, I picked to setup the "/+ +/" within the editor. And personally I like it a lot more than the "/* */".





















Personally I'd remove these toBit conversions. The strings it recognizes seem somewhat arbitrary. Why "yes" but not "oui"? If anything I would just have toBit recognize what std.format does with a bit, which is print "true" or "false". And given that restriction it would be simpler to just have a comparison with "true" or "false". One trouble with toBit(char c) for example is the numeric nature char might lead people to guess that c!=0 is the test for toBit instead of comparing with some ascii codes.
The toBit() functions were a bit of me thinking out loud I guess, cause I really think that the std.conv module should cover all the datatypes. But here again, Walter will pick what he wants to add...and even then he will rewrite it if neccessary.




All these floating point functions seem to be very small wrappers around std.math2.atof. Why not put these in math2? I can see the desire to mimic the std.conv module but IMO that isn't a strong enough reason to make another module. Also if the idea is to follow std.conv then I would have the ones that cast check for overflow etc just like toShort checks for overflow after getting the result of toInt.
It true that these are mainly wrapper functions that rely heavily upon std.math2 module, but it just makes more sense to me that std.conv is the module that converts strings (char[]s) back to numbers (both whole numbers and floating-point). But I'm beginning understand more as to why you were suggesting in the fromString() functions within some comments a few weeks ago in a different thread. It would be a nice way to have the toString() mirrored with the fromString() functions which could be overloaded with whatever numercial datatype that were passed into them. I'll have to take another look at the toShort() and toInt() functions in regards to the overflow checking. Thanks for the heads up on that.
[snip]






why 0f?
I thought it better to cast the numercial literal with a suffix ("f" for float). Is that the wrong way to do it? I could have used cast(float)0 instead, would this had been better?

I don't see where the index of the "i" shows up. If you only parse "<float>i" then you can just check the last char instead of doing an ifind.
You're right, the "i" would be at the end, if it were passed in. Of course the could "i" be missing from string all together, so instead of writing an if statement I used the ifind() function. Which on second thought, maybe I should've used the "if" statement. Thanks for pointing it out.







I'm not sure what f.nan == true is supposed to test. Do you want to see if f is a nan? The isnan function in std.math is for that.
I wanted to put in code to check for a nan value, but to tell the truth I may not totally understand the use behind them. I thinking that a NaN is much like a null, but only for floating-point. Also, I looked at using the isnan() function, but it can't handle the <float>i (the "i" part of the floating-point). Seems like there should be a isnani() function. I did play around with the f.nan == 1 (true) and f.nan == 0 (false) in testing, and it seemed to be correct at the time, but I guess I should double-check that logic again.





[snip]

 s2 )





why the dup? You shouldn't need to change s so there is no need to make a copy. Also I can't tell exactly what string parse into complex numbers. For example will the string "1-2i" parse correctly? I think it should. In any case documenting what works and what doesn't is important.
I'm afraid it's a bad habit I've seemed to have picked up, "When in doubt...dup it!" Since Basic is my primary language and my D my secondary, I see the "out" in D as ByRef in Basic...and the fact that "s" is a local variable adds to the confusion (I have this great fear of a pointer referencing a memory location that disappears without warning...which has happen to me a few times in C).

































Well, Ben I'd like to thanks you for the time you spent looking things over! Did you have any time to test it in any of your D code? Thanks again. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Apr 10 2005
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"David L. Davis" <SpottedTiger yahoo.com> wrote in message 
news:d3d1bp$1gle$1 digitaldaemon.com...
 In article <d3cq53$19hh$1 digitaldaemon.com>, Ben Hinkle says...
"David L. Davis" <SpottedTiger yahoo.com> wrote in message
news:d3903a$vr8$1 digitaldaemon.com...


My first comment is the comment style. Does anything else in phobos use this comment style? Looking in std.conv I see comments like /************************* * Convert blah to foo. * Gammar: digit(digit) */
Yeah, the comment style is what I started to use on code on my web-site (something I remembere picking up from my programming classes back in my college days), but I don't really expect Walter to keep them. :) And since I've passed all rights of the code to him, he can make the changes he needs to make.
Your chances of getting something done to phobos goes up if you make it easy for him. It also makes it easier for us readers to have an idea what actually is intended to be in the final shipping library. I just assumed what you posted was what you expected to be accepted.
[snip]






why 0f?
I thought it better to cast the numercial literal with a suffix ("f" for float). Is that the wrong way to do it? I could have used cast(float)0 instead, would this had been better?
ifind returns the int index of the first occurance of the requested char. No need to compare with a float.
 Well, Ben I'd like to thanks you for the time you spent looking things 
 over! Did
 you have any time to test it in any of your D code?
no problem. I want to see phobos improve, too. Thanks for posting your change request. I didn't try running the code.
Apr 11 2005