Topic: How to lead calculation only within the limits of type float, instead of dou

Greetings! There are such functions: static double GetDblEpsilon () {double d = 1; while (1.0 + d / 2! = 1.0) {d = d / 2;} return d;} static float GetFltEpsilon () {float d = 1f; while (1.0f + d / 2f! = 1.0f) {d = d / 2f;} return d;}... Console. WriteLine (GetDblEpsilon ()); Console. WriteLine (GetFltEpsilon ()); Deduce 2.22044604925031E-16 2.220446E-16 Though on my concepts should deduce 2.22044604925031E-16 1.192093E-07 Why and how to achieve calculation within the limits of type float in the second function, instead of double c a ghost to float? (If who did not understand, it is attempt to receive value DBL_EPSILON and FLT_EPSILON which for any reason did not deliver in )

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: O> (If who did not understand, it is attempt to receive value DBL_EPSILON and FLT_EPSILON which for any reason did not deliver in ) Console. WriteLine (float. Epsilon); Console. WriteLine (double. Epsilon);

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, Qbit86, you wrote: Q> Hello, oziro, you wrote: O>> (If who did not understand, it is attempt to receive value DBL_EPSILON and FLT_EPSILON which for any reason did not deliver in ) Q> Console. WriteLine (float. Epsilon); Q> Console. WriteLine (double. Epsilon); I marked, DBL_EPSILON it at all that double. Epsilon. Somewhere, approximately so, on 300 orders, only.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: O> (If who did not understand, it is attempt to receive value DBL_EPSILON and FLT_EPSILON which for any reason did not deliver in ) https://msdn.microsoft.com/ru-ru/librar … le.epsilon (v=vs.110).aspx https://msdn.microsoft.com/en-us/librar … le.epsilon (v=vs.110).aspx

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: O> Greetings! O> (if who did not understand, it is attempt to receive value DBL_EPSILON and FLT_EPSILON which for any reason did not deliver in ) Simply lead calculations with type float and all intermediate results add in temporary variables static float eps () {float x = 1/3f, y = 3*x-1; return 2*y;} static float eps1 () {float x = 1, y = 1, t; for (; {t = x + y; if (t (x) return y; y / = 2;}}

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, kov_serg, you wrote: _> Simply lead calculations with type float and all intermediate results add in temporary variables Smartly, it ! static float GetFltEpsilon () {float d = 1; float f1 = 1.0f + d / 2.0f; while (f1! = 1.0f) {d = d / 2f; f1 = 1.0f + d / 2.0f;} return d;}

Re: How to lead calculation only within the limits of type float, instead of dou

Prompt, please internal static class Utils {private static double GetDblEpsilon () {double d = 1; while (1.0 + d / 2! = 1.0) {d = d / 2;} return d;} internal static readonly double DBL_EPSILON = GetDblEpsilon (); private static float GetFltEpsilon () {float d = 1; float f1 = 1.0f + d / 2.0f; while (f1! = 1.0f) {d = d / 2f; f1 = 1.0f + d / 2f;} return d;} internal static readonly float FLT_EPSILON = GetFltEpsilon ();} whether Will be here Utils. DBL_EPSILON it is once initialized and as though a constant? Thanks

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: O> Prompt, please O> O> internal static class Utils O> {O> private static double GetDblEpsilon () O> {O> double d = 1; O> while (1.0 + d / 2! = 1.0) O> {O> d = d / 2; O>} O> return d; O>} O> internal static readonly double DBL_EPSILON = GetDblEpsilon (); O> private static float GetFltEpsilon () O> {O> float d = 1; O> float f1 = 1.0f + d / 2.0f; O> while (f1! = 1.0f) O> {O> d = d / 2f; O> f1 = 1.0f + d / 2f; O>} O> return d; O>} O> internal static readonly float FLT_EPSILON = GetFltEpsilon (); O>} whether O> O> Will be here Utils. DBL_EPSILON it is once initialized and as though a constant? Yes.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: _>> Simply lead calculations with type float and all intermediate results add in temporary variables O> Smartly, it ! Only not in temporary variables, and in an array, or in class fields. It not , is the CLI specification (, on Clark it is indistinguishable from magic): Storage locations for floating-point numbers (statics, array elements, and fields of classes) are of fixed size. The supported storage sizes are float32 and float64. Everywhere else (on the evaluation stack, as arguments, as return types, and as local variables) floating-point numbers are represented using an internal floating-point type. In each such instance, the nominal type of the variable or expression is either float32 or float64, but its value can be represented internally with additional range and/or precision. The size of the internal floating-point representation is implementation-dependent, can vary, and shall have precision at least as great as that of the variable or expression being represented. If carries - the Author leads to special effects: Sinix Date: 25.06.14. And, yes, it is not forgotten about lovelier nuance. Particulars.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, kov_serg, you wrote: _> Simply lead calculations with type float and all intermediate results add in temporary variables And if with the full optimization to collect, does not break? There more low in classes and arrays advise to push, in secret hope that the optimizer there does not reach, but too it is unreliable. In gcc for this purpose there is a key-ffloat-store, and in sharps can be to anybody such it is not required.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, cures, you wrote: Cs> There more low in classes and arrays advise to push, in secret hope that the optimizer there does not reach, but too it is unreliable. Does not reach, even with.net native. It is unique, I do not warrant that position does not exchange somewhere through pair releases. By the way, forgot to ask. What for the task such, what "superfluous" accuracy hinders?

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: whether O> Will be here Utils. DBL_EPSILON it is once initialized and as though a constant? Will be, and as a constant the Author: Sinix Date: 27.03.16 to the fact. Other question - that you with this constant are going to do? To compare numbers so for the obvious reason of sense is not present. UPD especially with the registration here it: If you create a custom algorithm that determines whether two floating-point numbers can be considered equal, we do not recommend that you base your algorithm on the value of the Epsilon constant to establish the acceptable absolute margin of difference for the two values to be considered equal. (Typically, that margin of difference is many times greater than Epsilon.) For information about comparing two double-precision floating-point values, see Double and Equals (Double). Platform Notes On ARM systems, the value of the Epsilon constant is too small to be detected, so it equates to zero. You can define an alternative epsilon value that equals 2.2250738585072014E-308 instead. : try to guess whence

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, Sinix, you wrote: S> Other question - that you with this constant are going to do? DBL_EPSILON is not Double. Epsilon. DBL_EPSILON the minimum positive number such that 1.0 + DBL_EPSILON! = 1.0. In vain it is not present in library, it is equal for double 2.2204460492503131E-16 And my algorithm - is clear, at least to me. static bool Equal (double a, double b, uint precisionFactor) {//in case they are Infinities (then epsilon check does not work) if (a == b) return true; double epsilon = precisionFactor * DBL_EPSILON * (Math. Abs (a) + Math. Abs (b)); return Math. Abs (a - b) <= epsilon;} precisionFactor we take 16, these are 4 bits or approximately 1 least significant decimal digit of a mantissa. Thereby general-purpose enough method of comparing. Well, at comparing with zero there are nuances, clearly, made specialized functions. But I considerably consider that Double. Epsilon (and  MIN_NORMAL) not a place here is perfect, I will not understand, why them push on all Internet. By the way, decided not to consider value DBL_EPSILON as in the first message, and to hammer with hands. It is less than code of less problems. It is possible generally stupidly 1e-15 to set, on idea too norms will be.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: O> In vain it is not present in library, it is equal for double 2.2204460492503131E-16 I in course. A problem that correct comparing from the mathematical point of view in practice works with surprises. static double GetDblEpsilon () {double d = 1; while (1.0 + d / 2! = 1.0) {d = d / 2;} return d;} private static readonly double DBL_EPSILON = GetDblEpsilon (); static bool Equal (double a, double b, uint precisionFactor) {//in case they are Infinities (then epsilon check does not work) if (a == b) return true; double epsilon = precisionFactor * DBL_EPSILON * (Math. Abs (a) + Math. Abs (b)); return Math. Abs (a - b) <= epsilon;} static void Main (string [] args) {var a = 1e-320; var b = 0d; Console. WriteLine (a. ToString ("R")); Console. WriteLine (b. ToString ("R")); Console. WriteLine (Equal (a, b, 1024));//false Console. WriteLine (); a = 1; b = 1 * (1 + 1e-15); Console. WriteLine (a. ToString ("R")); Console. WriteLine (b. ToString ("R")); Console. WriteLine (Equal (a, b, 1024));//true} comparing with sensitivity to errors in 10-320, which thus  a difference in 10-15 - not that piece which should be had in the business code. However, a variant in MSDN: static bool IsApproximatelyEqual (double value1, double value2, double epsilon) {//If they are equal anyway, just return True. if (value1.Equals (value2)) return true;//Handle NaN, Infinity. if (Double. IsInfinity (value1) | Double. IsNaN (value1)) return value1.Equals (value2); else if (Double. IsInfinity (value2) | Double. IsNaN (value2)) return value1.Equals (value2);//Handle zero to avoid division by zero double divisor = Math. Max (value1, value2); if (divisor. Equals (0)) divisor = Math. Min (value1, value2); return Math. Abs (value1 - value2) / divisor <= epsilon;} is even worse. var a = 1e-320; var b = 0d; Console. WriteLine (IsApproximatelyEqual (a, b, 0.0000001));//false Console. WriteLine (IsApproximatelyEqual (-a, b, 0.0000001));//true Ugu compare double can not only only all.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, Sinix, you wrote: S> Ugu compare double can not only only all. Aha, troubles are present. Generally, at me static bool Equal (double a, double b) {return Equal (a, b, 16);//it is aforementioned Equal ()} it is used on a default in all places. And for zero - a special situation. bool IsZero (double a, double estimateValue) {double epsilon = 16 * DBL_EPSILON * Math. Abs (estimateValue); return Math. Abs (a) <= epsilon;} Since in one situation comparing 1e-320 c in zero should give True, and friend False, depending on calculations. Therefore I transfer estimate value in function. And then False == IsZero (1e-320, 1e-310);//1e-320! = 0. (1e-310 - , one of the values participating in calculations) True == IsZero (1e-320, 1e-100);//1e-320 == 0 (1e-100 - it is similar, the approximate value participating in ) is shorter, with zero it is necessary to compare to the specified estimation is, likely, always. Well, or to do default on a type range 1e-9. 1e9, but always to understand that then it will not work in many cases, and these are a true rake because forget and will use. Therefore IsZero with . In parameter-estimation. UPD I, of course, was mistaken. 1e-320 can be presented in double Fixed

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, oziro, you wrote: O> is shorter, with zero it is necessary to compare to the specified estimation is, likely, always. Well, or to do default on a type range 1e-9. 1e9, but always to understand that then it will not work in many cases, and these are a true rake because forget and will use. Therefore IsZero with . In parameter-estimation. Well. In practice it is all it is reduced to  which perfectly work with __ scenarios, and  on all remaining. For example, for the fixed value range with approximately one order of values the absolute error somewhere in one million from max value can work. Somewhere it is necessary to enter a combo from relative and absolute errors, for any values as a result it can appear with decimal is easier to work and approximate to n th sign after a comma ... Perhaps, the only thing that I yet did not see - circuits of comparing which would suit all

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, Sinix, you wrote: S> By the way, forgot to ask. What for the task such, what "superfluous" accuracy hinders? It probably not absolutely to me, is faster to  a question From the experience, saw such in mathematical libraries, for that determination, with what accuracy basically it makes sense to conduct calculations. For example, in QR-algorithm it is necessary to understand, to what level it is possible  turning out  elements. In our code-ffloat-store it is used for (bit-by-bit) reproducibility of result on different platforms, it is necessary at sanity check of libraries and equipments.

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, cures, you wrote: a C> Hello, Sinix, you wrote: S>> By the way, forgot to ask. What for the task such, what "superfluous" accuracy hinders? The C> It probably not absolutely to me, is faster to  a question I at once I will tell: I such did not speak, in view of had no, and not so I understand sense. So is not present, such question is not necessary to me And on the questions I received answers, all thanks!

Re: How to lead calculation only within the limits of type float, instead of dou

Hello, Sinix, you wrote: Concerning the last two links: I correctly understand, what in the representation float\double can use to 80 bits which will be cut off to 32\64 bit at saving attempt in storage? Well, big mistake and the source of an enormous number of questions at SO. The idea was scrapped when Intel implemented the next generation floating point hardware, the XMM and YMM registers are 64-bit. True registers, not a stack. The x64 jitter uses them. Making your program running in 64-bit mode produce different results from when it runs in 32-bit mode. It will take another ten years before that stops hurting Krut what, I also did not know that there all so is cheerful. Especially  x87...