mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Next And Previous Floats as a ruby til.
This commit is contained in:
@@ -111,6 +111,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md)
|
- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md)
|
||||||
- [Limit Split](ruby/limit-split.md)
|
- [Limit Split](ruby/limit-split.md)
|
||||||
- [Listing Local Variables](ruby/listing-local-variables.md)
|
- [Listing Local Variables](ruby/listing-local-variables.md)
|
||||||
|
- [Next And Previous Floats](ruby/next-and-previous-floats.md)
|
||||||
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
||||||
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
||||||
- [Passing Arbitrary Methods As Blocks](ruby/passing-arbitrary-methods-as-blocks.md)
|
- [Passing Arbitrary Methods As Blocks](ruby/passing-arbitrary-methods-as-blocks.md)
|
||||||
|
|||||||
38
ruby/next-and-previous-floats.md
Normal file
38
ruby/next-and-previous-floats.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Next And Previous Floats
|
||||||
|
|
||||||
|
The `Float` class has two interesting methods for stepping forward or
|
||||||
|
backwards through the numbers that can actually be *represented* by floats.
|
||||||
|
This is handy since floats are not evenly spaced.
|
||||||
|
|
||||||
|
Use `#next_float` to go forward
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> 2.0
|
||||||
|
=> 2.0
|
||||||
|
> _.next_float
|
||||||
|
=> 2.0000000000000004
|
||||||
|
> _.next_float
|
||||||
|
=> 2.000000000000001
|
||||||
|
> _.next_float
|
||||||
|
=> 2.0000000000000013
|
||||||
|
> _.next_float
|
||||||
|
=> 2.0000000000000018
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `#prev_float` to go backwards
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> 2.0
|
||||||
|
=> 2.0
|
||||||
|
> _.prev_float
|
||||||
|
=> 1.9999999999999998
|
||||||
|
> _.prev_float
|
||||||
|
=> 1.9999999999999996
|
||||||
|
> _.prev_float
|
||||||
|
=> 1.9999999999999993
|
||||||
|
> _.prev_float
|
||||||
|
=> 1.9999999999999991
|
||||||
|
```
|
||||||
|
|
||||||
|
I cannot think of any practical use cases, but it is fun to know they are
|
||||||
|
there if you need them.
|
||||||
Reference in New Issue
Block a user