epsilon

C++ next float with numeric_limits / epsilon?

落爺英雄遲暮 提交于 2019-12-10 16:59:40
问题 Consider a "normal" real number TREAL x in C++ (not subnormal and not NaN/Infinite) ( TREAL = float , double , long double ) Is the following the good solution to find the previous and next x from a floating-point point of view ? TREAL xprev = (((TREAL)(1.)) - std::numeric_limits<TREAL>::epsilon()) * x; TREAL xnext = (((TREAL)(1.)) + std::numeric_limits<TREAL>::epsilon()) * x; Thank you very much. 回答1: C99 and C++11 have nextafter, nextafterl and nextafterf functions in <math.h> and <cmath> .

Python epsilon is not the smallest number

陌路散爱 提交于 2019-12-09 16:26:30
问题 What does sys.float_info.epsilon return? On my system I get: >>> sys.float_info.epsilon 2.220446049250313e-16 >>> sys.float_info.epsilon / 2 1.1102230246251565e-16 >>> 0 < sys.float_info.epsilon / 2 < sys.float_info.epsilon True How is this possible? EDIT: You are all right, I thought epsilon does what min does. So I actually meant sys.float_info.min . EDIT2 Everybody and especially John Kugelman, thanks for your answers! Some playing around I did to clarify things to myself: >>> float.hex

Get Machine epsilon in Microsoft Excel

ⅰ亾dé卋堺 提交于 2019-12-08 19:47:36
问题 This seems like a question better directed at those with some programming experience rather than just general Excel users, hence my asking on here as opposed to Superuser. Is there any way, preferably through a function, to return epsilon (i.e. the smallest non-zero number representable in Excel's calculations)? If it's not retrievable through a function, is there a quick way to calculate it through a compact function? To be clear, I'm not looking for a VBA-based solution, I'd like an Excel

IEqualityComparer<double> with a tolerance; how to implement GetHashCode?

痞子三分冷 提交于 2019-12-08 18:51:02
问题 I'm implementing a reusable DoubleEqualityComparer (with a custom tolerance: the "epsilon" constructor parameter) to ease the usage of LINQ with sequences of double. For example: bool myDoubleFound = doubles.Contains(myDouble, new DoubleEqualityComparer(epsilon: 0.01)); What is the right way to implement GetHashCode? Here's the code: public class DoubleEqualityComparer : IEqualityComparer<double>, IEqualityComparer<double?> { private readonly double epsilon; public DoubleEqualityComparer

Java: Double machine epsilon is not the smallest x such that 1+x != 1?

别等时光非礼了梦想. 提交于 2019-12-05 12:50:15
问题 I am trying to determine the double machine epsilon in Java, using the definition of it being the smallest representable double value x such that 1.0 + x != 1.0 , just as in C/C++. According to wikipedia, this machine epsilon is equal to 2^-52 (with 52 being the number of double mantissa bits - 1). My implementation uses the Math.ulp() function: double eps = Math.ulp(1.0); System.out.println("eps = " + eps); System.out.println("eps == 2^-52? " + (eps == Math.pow(2, -52))); and the results are

Python epsilon is not the smallest number

老子叫甜甜 提交于 2019-12-04 04:16:43
What does sys.float_info.epsilon return? On my system I get: >>> sys.float_info.epsilon 2.220446049250313e-16 >>> sys.float_info.epsilon / 2 1.1102230246251565e-16 >>> 0 < sys.float_info.epsilon / 2 < sys.float_info.epsilon True How is this possible? EDIT: You are all right, I thought epsilon does what min does. So I actually meant sys.float_info.min . EDIT2 Everybody and especially John Kugelman, thanks for your answers! Some playing around I did to clarify things to myself: >>> float.hex(sys.float_info.epsilon) '0x1.0000000000000p-52' >>> float.hex(sys.float_info.min) '0x1.0000000000000p

Java: Double machine epsilon is not the smallest x such that 1+x != 1?

别说谁变了你拦得住时间么 提交于 2019-12-03 22:35:11
I am trying to determine the double machine epsilon in Java, using the definition of it being the smallest representable double value x such that 1.0 + x != 1.0 , just as in C/C++. According to wikipedia, this machine epsilon is equal to 2^-52 (with 52 being the number of double mantissa bits - 1). My implementation uses the Math.ulp() function: double eps = Math.ulp(1.0); System.out.println("eps = " + eps); System.out.println("eps == 2^-52? " + (eps == Math.pow(2, -52))); and the results are what I expected: eps = 2.220446049250313E-16 eps == 2^-52? true So far, so good. However, if I check

Why my double can contain a value below the machine epsilon?

落爺英雄遲暮 提交于 2019-12-01 05:34:20
问题 I was solving an equation using double precision and I got -7.07649e-17 as a solution instead of 0 . I agree it's close enough that I can say it's equal but I've read that the machine epsilon for the C++ double type is 2^-52 which is larger than the value I get. So why do I have an inferior value than the machine epsilon? Why isn't the value rounded to zero? It's not a big deal but when I do a logical test it appears that my value is not zero... 回答1: There are two different constants in this

Is the use of machine epsilon appropriate for floating-point equality tests?

感情迁移 提交于 2019-11-30 09:11:24
问题 This is a follow-up to Testing for floating-point value equality: Is there a standard name for the “precision” constant?. There is a very similar question Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to. It is well known that an equality test for two floating-point values x and y should look more like this (rather than a straightforward =): abs( x - y ) < epsilon , where epsilon is some very small value. How to choose a value for epsilon ?

epsilon for various float values

一世执手 提交于 2019-11-29 18:13:55
There is FLT_MIN constant that is nearest to zero. How to get nearest to some number value? As an example: float nearest_to_1000 = 1000.0f + epsilon; // epsilon must be the smallest value satisfying condition: // nearest_to_1000 > 1000.0f I would prefer numeric formula without using special functions. Eric Postpischil Caution: Bugs were found in this code while working on another answer. I hope to update this later. In the meantime, it fails for some values involving subnormals. C provides a function for this, in the <math.h> header. nextafterf(x, INFINITY) is the next representable value