A suggested approach to calculate machine epsilon using Java is as follows,
private static float calculateMachineEpsilonFloat() {
float machEps = 1.0f;
do
machEps /= 2.0f;
while ((float) (1.0 + (machEps / 2.0)) != 1.0);
return machEps;
}
How to compute epsilon for scala.Double
using Scala in an idiomatic / functional style from this suggested code?
Is that OK?
scala> val s: Stream[Float] = 1.0f #:: s.map(f => f / 2.0f)
s: Stream[Float] = Stream(1.0, ?)
scala> val eps = s.takeWhile(e => e + 1.0f != 1.0f).last
eps: Float = 1.1920929E-7
To get another epsilon (2^{-24}), one can use dropWhile
(and, consequently, head
) instead.