[#65451] [ruby-trunk - Feature #10333] [PATCH 3/1] optimize: "yoda literal" == string — ko1@...

Issue #10333 has been updated by Koichi Sasada.

9 messages 2014/10/07

[ruby-core:65903] [ruby-trunk - Feature #10425] A predicate method to tell if a number is near another

From: duerst@...
Date: 2014-10-26 01:47:25 UTC
List: ruby-core #65903
Issue #10425 has been updated by Martin D=C3=BCrst.


Such a predicate would indeed be convenient. But sometimes, one doesn't wan=
t to compare with an absolute epsilon, but relatively, e.g. like so:

((other.abs.to_f + abs.to_f) / (other.to_f - to_f).abs) <=3D ????

----------------------------------------
Feature #10425: A predicate method to tell if a number is near another
https://bugs.ruby-lang.org/issues/10425#change-49642

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee:=20
* Category:=20
* Target version:=20
----------------------------------------
A method `near?` like the following would be useful.

~~~ruby
class Numeric
	def near? other, delta: Float::EPSILON
		(other.to_f - to_f).abs <=3D delta.to_f
	end
end

class Time
	def near? other, delta: Float::EPSILON
		to_f.near?(other.to_f, delta: delta)
	end
end
~~~

It can be used to check errors, or whether something is around something.

~~~ruby
23.24324.near?(23.23, delta: 0.5) # =3D> true
t1 =3D t2 =3D Time.now
t3 =3D Time.now
t1.near?(t2) #=3D> true
t1.near?(t3) #=3D> false
5.near?(3, delta: 1) #=3D> true
~~~

Some testing frameworks have something similar to this, but I think this is=
 an elementary concept that Ruby should support at it core.




--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next