exclude min value in Range

B

Brian Buckley

------=_Part_4195_15342759.1113857559051
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello,

Is there a way to create a Range object which excludes the min value when=
=20
values in the Range are continuous? (If the values were discrete one could=
=20
just start with the succ value.)

For example, range =3D (0.0...1.0) is a range of Floats that includes the=
=20
value 0.0. How would one create such a Range but which excluded 0.0?

Thanks from a relatively new user of ruby,

Brian

------=_Part_4195_15342759.1113857559051--
 
D

Dave Burt

Floats aren't that accurate - be careful when comparing them (see recent
thread "when 1.6 != 1.6? -- newbie"). Don't rely on the difference between
".." and "..." ranges when dealing with floats.

Pick a number (let's call it epsilon) that is smaller than the smallest
delta you care about, and use that.

Float::EPSILON, the smallest possible difference between any two floats, is
about 2e-16. You need to use a number bigger than that.

EPSILON = 1e-8
range = (0.0 + EPSILON .. 1.0 - EPSILON)

Cheers,
Dave


Hello,

Is there a way to create a Range object which excludes the min value when
values in the Range are continuous? (If the values were discrete one could
just start with the succ value.)

For example, range = (0.0...1.0) is a range of Floats that includes the
value 0.0. How would one create such a Range but which excluded 0.0?

Thanks from a relatively new user of ruby,

Brian
 
B

Brian Buckley

------=_Part_4315_18033928.1113868350202
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Dave, Thanks for that. I understand, as least for the most part. The EPSILO=
N=20
technique should work for me.

Hmmm... it seems to me that useful methods for Float (say "succ" and "prev"=
)=20
would return the nearest values above and below self that ruby is able to=
=20
represent. I'm just thinking aloud (and maybe/probably also illustrating=20
that I don't really understand this :) ).

Brian

------=_Part_4315_18033928.1113868350202--
 
D

Dave Burt

Dave, Thanks for that. I understand, as least for the most part. The
EPSILON
technique should work for me.

Hmmm... it seems to me that useful methods for Float (say "succ" and
"prev")
would return the nearest values above and below self that ruby is able to
represent. I'm just thinking aloud (and maybe/probably also illustrating
that I don't really understand this :) ).

I don't see that they would be that useful. The issue is really with
comparison, and the solution is just using an epsilon that's good enough for
your comparison's purposes.

If Float isn't accurate enough, or if you'd just prefer 100% accuracy for
less speed, you can use BigDecimal.

Please read this recent thread:

http://tinyurl.com/btqo6 [1]

In particular, the following reference (cited there) covers this topic well:

"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>

Cheers,
Dave

[1]
http://groups.google.com.au/groups?...irefox-a&rls=org.mozilla%3Aen-US%3Aofficial_s
 

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
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top