[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

exclude min value in Range

Brian Buckley

4/18/2005 8:53:00 PM

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
3 Answers

Dave Burt

4/18/2005 9:31:00 PM

0

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


"Brian Buckley" <briankbuckley@gmail.com> wrote in message
news:a7b31d7105041813525c1e9b8c@mail.gmail.com...
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


Brian Buckley

4/18/2005 11:53:00 PM

0

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 :) ).

Brian

Dave Burt

4/19/2005 2:19:00 PM

0

> 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... [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_goldber...

Cheers,
Dave

[1]
http://groups.google.com.au/groups?hl=en&lr=&safe=off&client=firefox-a&rls=org.mozilla:en-US:official_s&threadm=8f11c0901c6067ab81ebcff48132ba4a%40att.net&rnum=1&prev=/groups%3Fq%3Dwhe...!%253D%2B1.6%2Bnewbie%26hl%3Den%26btnG%3DGoogle%2BSearch%26client%3Dfirefox-a%26rls%3Dorg.mozilla%253Aen-US%253Aofficial_s