Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Method to groom a string to floating point representation

Reply
Thread Tools

Method to groom a string to floating point representation

 
 
Alex DeCaria
Guest
Posts: n/a
 
      04-13-2010
I have a program that asks for the user to enter a string that
represents a floating point number. Everytime a new character is typed
I want a method that checks to make sure the string makes sense as a
floating point number, and if not, deletes any bad characters. For
instance, if the user enters '4.5e+6.7' I want the method to delete the
extra decimal place and return '4.5e+67'. Or, if the user enters
something like '4.5+e7' it deletes the misplaced plus sign and returens
'4.7e7'. In short, I want the method to only allow correct
representations of floating point numbers, but I want it to remain as a
string. Anything other than a number or +, -, ., or e or E, should be
deleted.

I wrote a method that works like I want (attached), but it is long and
cumbersome. I'm wondering if anyone has a shorter, better way to do
this.

--Alex

Attachments:
http://www.ruby-forum.com/attachment...string_lite.rb

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      04-13-2010
[Note: parts of this message were removed to make it a legal post.]

On Mon, Apr 12, 2010 at 9:34 PM, Alex DeCaria <
> wrote:


> I have a program that asks for the user to enter a string that
> represents a floating point number. Everytime a new character is typed
> I want a method that checks to make sure the string makes sense as a
> floating point number, and if not, deletes any bad characters. For
> instance, if the user enters '4.5e+6.7' I want the method to delete the
> extra decimal place and return '4.5e+67'. Or, if the user enters
> something like '4.5+e7' it deletes the misplaced plus sign and returens
> '4.7e7'. In short, I want the method to only allow correct
> representations of floating point numbers, but I want it to remain as a
> string. Anything other than a number or +, -, ., or e or E, should be
> deleted.
>
> I wrote a method that works like I want (attached), but it is long and
> cumbersome. I'm wondering if anyone has a shorter, better way to do
> this.
>
> --Alex
>
> Attachments:
> http://www.ruby-forum.com/attachment...string_lite.rb
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

It would probably be easier if you provided a set of tests we could check
our function against, where we could be confident our function was correct
once it passed all the tests.

 
Reply With Quote
 
 
 
 
Alex DeCaria
Guest
Posts: n/a
 
      04-13-2010
Josh Cheek wrote:
> It would probably be easier if you provided a set of tests we could
> check
> our function against, where we could be confident our function was
> correct
> once it passed all the tests.


Here are some examples of what it should do:

Delete any characters other than digits, +, -, e, E, or .:
'-24.5fge4x'5 => '-24.5e45'

Delete any extra decimals:
'2.4.5' => '2.45'
'2..45' => '2.45'

Delete any decimals in an exponent:
'245e7.6' => '2.45e76'

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
'e' or 'E' has precedence unless it doesn't make sense):
'4.67e6e-7' => '4.67e67'
'+e4.67e-7' => '+4.67e-7'

The motivation for this is for a GUI input textbox, so that if the user
enters a bad string it automatically corrects it to a valid
floating-point representation in string form before converting to a
floating-point for calculations. I toyed with just doing
str = str.to_f.to_s
and letting Ruby figure out the floating point respesentation, but I'd
like more control over how the string is converted to floating point
representation. For example, I want
'2..45e9' => '2.45e9', whereas '2..45e9'.to_f.to_s => '2.0'

--Alex


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      04-14-2010
On Tue, Apr 13, 2010 at 6:20 AM, Alex DeCaria <=
u
> wrote:


> Delete any decimals in an exponent:
> '245e7.6' =3D> '2.45e76'
>


Where did the dot in between 2 and 4 come from? Am I interpreting the Strin=
g
or just cleaning it?


> Delete any extra or misplaced + or =96 signs:
> '+45-68+e+45-' =3D> '4568e+45'
>
> Delete any extra or misplaced =91e=92 or =91E=92 characters (first occura=

nce of
> '+e4.67e-7' =3D> '+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?

-----

This is what I have so far, please check and correct any tests that should
be different

def clean_string( str , options =3D Hash.new )
str =3D~ /\A([-+]?)([^eE.]*\.?)([^eE]*)((?:[eE][+-]?)?)([^Z]*)\Z/
posneg , prepre , postpre , e , post =3D $1 , $2 , $3 , $4 , $5
posneg + prepre + postpre.gsub(/[^0-9]/,'') + e + post.gsub(/[^0-9]/,'')
end

require 'test/unit'
class TestCleanString < Test::Unit::TestCase
def test_delete_chars
assert_equal '-24.5e45' , clean_string('-24.5fge4x5')
end
def test_delete_extra_decimal
assert_equal '2.45' , clean_string('2.4.5')
assert_equal '2.45' , clean_string('2..45')
assert_equal '2.45' , clean_string('2...45')
end
def test_delete_extra_decimal_in_exponent
assert_equal '245e76' , clean_string('245e7.6') # you said this should
be '2.45e76' , but where did first dot come from?
end
def test_delete_extra_or_misplaced_pos_and_neg_signs
assert_equal '4568e+45' , clean_string('+45-68+e+45-')
end
def test_delete_extra_or_misplaced_e_or_E
assert_equal '4.67e67' , clean_string('4.67e6e-7')
assert_equal '+4.67e-7' , clean_string('+e4.67e-7')
end
end

 
Reply With Quote
 
Jean-Julien Fleck
Guest
Posts: n/a
 
      04-14-2010
Hello,

2010/4/14 Josh Cheek <>:
> On Tue, Apr 13, 2010 at 6:20 AM, Alex DeCaria <alex.decaria@millersville.=

edu
>> wrote:

>
>> Delete any decimals in an exponent:
>> '245e7.6' =3D> '2.45e76'
>>

>
> Where did the dot in between 2 and 4 come from? Am I interpreting the Str=

ing
> or just cleaning it?


As said Josh, here you are interpreting the string rather than
cleaning it. 245e76 is a valid float, just not in the usual 2.45e78
form.

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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber

 
Reply With Quote
 
Alex DeCaria
Guest
Posts: n/a
 
      04-14-2010
>
>> 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/.

 
Reply With Quote
 
Alex DeCaria
Guest
Posts: n/a
 
      04-14-2010
>
> 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/.

 
Reply With Quote
 
Alex DeCaria
Guest
Posts: n/a
 
      04-14-2010
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/.

 
Reply With Quote
 
Jean-Julien Fleck
Guest
Posts: n/a
 
      04-14-2010
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. =A0Thus, if they try to enter a misplace=

d
> + 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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber

 
Reply With Quote
 
Alex DeCaria
Guest
Posts: n/a
 
      04-14-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating Point Representation (Question) Stefan Ram Java 2 12-26-2012 06:36 PM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Semi OT: Binary representation of floating point numbers Dilip C Programming 8 12-28-2006 06:36 AM
Unknown floating point representation sergei.s.mikhailov@gmail.com Java 3 06-22-2006 11:54 PM
Binary representation of floating point numbers 63q2o4i02@sneakemail.com Python 10 12-15-2005 03:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57