float out of range

L

lovebyte2000

Hi,

I am reading from a file a list of floating point numbers, some of
which are high precision. When I read such a number, I get a float out
of range warning:

% ff = Kernel.Float("1.0e-309")
% puts ff

test.rb:3: warning: Float 1.0e-309 out of range
0.0

The number is converted to 0.0 which is fine, but I would like to get
rid of this warning within the code.

Could anyone tell me how? Sorry if this is trivial, I am new to ruby,
and I haven't found a solution on the net (-W0 is not what I am looking
for).

Thank you

Denis
 
R

Robert Klemme

Hi,

I am reading from a file a list of floating point numbers, some of
which are high precision. When I read such a number, I get a float
out of range warning:

% ff = Kernel.Float("1.0e-309")
% puts ff

test.rb:3: warning: Float 1.0e-309 out of range
0.0

The number is converted to 0.0 which is fine, but I would like to get
rid of this warning within the code.

Could anyone tell me how? Sorry if this is trivial, I am new to ruby,
and I haven't found a solution on the net (-W0 is not what I am
looking for).

IMHO Float() should raise an exception in these cases; your program will
never be aware of the conversion error and thus cannot take any action
against this. Although this might break some code I'd strongly advocate
to put an exception in there. Unfortunaltely this won't help you right
now...

Kind regards

robert
 
C

Cameron McBride

Morning,

..snip..

IMHO Float() should raise an exception in these cases; your program will
never be aware of the conversion error and thus cannot take any action
against this. Although this might break some code I'd strongly advocate
to put an exception in there. Unfortunaltely this won't help you right
now...

I'd tend to agree. Float() is a strict conversion, which is why
Float('bob') raises an exception.

My first thought was "1.0e-309".to_f, but this just produces the same warni=
ng.=20

Float("bob") # raise ArgumentError =20
"bob".to_f # no warning, =3D0

Float('1e-309') # warning but conversion
"1e-309".to_f # same behavior as Float()

Why would "1.0e-309" (a string without a good numerical float representatio=
n)=20
work significantly differently than "bob"? Assuming this is the
desired behavior, it seems inconsistent to my ignorant brain -- can
someone clarify it for me?

Cameron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,850
Latest member
Brightrs

Latest Threads

Top