Prev: [ANN]VTD-XML 2.8
Next: Rorth v0.3 released
From: Alex DeCaria on 14 Apr 2010 08:06 > >> Delete any decimals in an exponent: >> '245e7.6' => '2.45e76' >> > > Where did the dot in between 2 and 4 come from? Am I interpreting the > String > or just cleaning it? This was a typo on my part. It should have read: '245e7.6' => '245e76' > > >> Delete any extra or misplaced + or – signs: >> '+45-68+e+45-' => '4568e+45' >> >> Delete any extra or misplaced 'e' or 'E' characters (first occurance of >> '+e4.67e-7' => '+4.67e-7' >> >> > Why does the plus in front of 45 in the first one go away, but the plus > in > front of the e in the second one stays? Again, a typo on my part. It should have been: '+45-68+e+45-' => '+4568e+45' > > This is what I have so far, please check and correct any tests that > should > be different > Thank! I'll check the code you gave me and see how it does. --Alex -- Posted via http://www.ruby-forum.com/.
From: Alex DeCaria on 14 Apr 2010 08:13 > > BTW, I would rather not do any cleaning under the hood: let the user > correct its input himself. For example, give the input to Float() and > if an error is raised (which Float does as opposed to to_f which never > raise an error), rescue it by giving feedback to the user (where you > could use your method to propose an alternative if you want) but do > not continue without letting the user know he has made a mistake and > giving him the ability to change his mind. > > Cheers, I didn't realize the difference between Float() and .to_f. Thanks for the suggestion. The user is still aware if they entered an incorrect string, since they are entering it into a GUI textbox, and the string cleaning is done after each character is entered. Thus, if they try to enter a misplaced + sign or another bad character, they won't see it appear in the textbox, which should cause them to notice it. --Alex -- Posted via http://www.ruby-forum.com/.
From: Alex DeCaria on 14 Apr 2010 09:33 Josh Cheek wrote: > This is what I have so far, please check and correct any tests that > should > be different Josh, Your code works great! I knew there had to be a more elegant way to do this rather than my brute force method. The only test it didn't seem to work on was eliminating extra + or - signs, such as '+45-2+8' => '+4528', but now that I see what you are doing I can probably figure out how to do that. I definitely need to learn more about regular expressions! Thanks for your time and effort. --Alex -- Posted via http://www.ruby-forum.com/.
From: Jean-Julien Fleck on 14 Apr 2010 09:39 Hello Alex, > The user is still aware if they entered an incorrect string, since they > are entering it into a GUI textbox, and the string cleaning is done > after each character is entered. Thus, if they try to enter a misplaced > + sign or another bad character, they won't see it appear in the > textbox, which should cause them to notice it. Well, then you can't use the Float() trick because 1.0e3 is a valid but 1.0e is not. Then there will be a lot of strings your user won't be able to type even if they are valid in the end. Cheers, -- JJ Fleck PCSI1 Lycée Kléber
From: Alex DeCaria on 14 Apr 2010 09:48
Jean-Julien Fleck wrote: > Hello Alex, > >> The user is still aware if they entered an incorrect string, since they >> are entering it into a GUI textbox, and the string cleaning is done >> after each character is entered. �Thus, if they try to enter a misplaced >> + sign or another bad character, they won't see it appear in the >> textbox, which should cause them to notice it. > > Well, then you can't use the Float() trick because 1.0e3 is a valid > but 1.0e is not. > Then there will be a lot of strings your user won't be able to type > even if they are valid in the end. > > Cheers, Yes, there has to be some additional logic to allow a trailing 'e' with the assumption that the user will next enter a valid character afterward. That's what makes it a little complicated (and fun) to figure out. The goal is, as the user is entering data, to not allow them to enter anything that is obviously not going to work as a floating point representation. --Alex -- Posted via http://www.ruby-forum.com/. |