Re: floating point conversion

2014-06-02 Thread Gary Willoughby via Digitalmars-d-learn
I have a couple of functions that you may find useful for comparing floats. https://github.com/nomad-software/dunit/blob/master/source/dunit/toolkit.d#L42 https://github.com/nomad-software/dunit/blob/master/source/dunit/toolkit.d#L134

Re: floating point conversion

2014-06-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sun, 01 Jun 2014 14:42:34 + Famous via Digitalmars-d-learn wrote: > On Sunday, 1 June 2014 at 12:45:26 UTC, bearophile wrote: > > It's a bad question. > > Actually, Martin's question is a good one. > > Initializing a variable of type float via a literal or as > conversion from string shoul

Re: floating point conversion

2014-06-01 Thread Martin Krejcirik via Digitalmars-d-learn
On 1.6.2014 18:02, Famous wrote: > This is different. The decimal 1.234 cannot be exacly represented as > radix-2 floating-point number. In your example above, a and b are not > equal. They are both approximations to 1.234 but the value of b is Still feels iffy to me. If you add: printf("%.70f

Re: floating point conversion

2014-06-01 Thread Famous via Digitalmars-d-learn
I have compiled some cases at http://dpaste.dzfl.pl/5611b8bce8e3 This implies that floating-point constants do not have fixed but minimum precision. Particularly, the literal 1.23 describes a floating-point value with either double or real precision depending on what it is compared to. Thi

Re: floating point conversion

2014-06-01 Thread Wanderer via Digitalmars-d-learn
The General rule is not to compare floats for equality, (is 0.0==-0.0, etc.). Use a epsilon based comparision scheme instead or a wrapper around it. That's not exactly true. You cannot (and should not) compare floating points for equality, and use epsilon-based comparison instead, only in one

Re: floating point conversion

2014-06-01 Thread Famous via Digitalmars-d-learn
On Sunday, 1 June 2014 at 15:31:53 UTC, Martin Krejcirik wrote: On 1.6.2014 16:42, Famous wrote: from string should be the same, exacly, always. Casting a float to double should be deterministic as well. void main() { float a = 1.234f; double b = 1.234; assert (a == cast(float) b)

Re: floating point conversion

2014-06-01 Thread Martin Krejcirik via Digitalmars-d-learn
On 1.6.2014 16:42, Famous wrote: > from string should be the same, exacly, always. Casting a float to > double should be deterministic as well. void main() { float a = 1.234f; double b = 1.234; assert (a == cast(float) b); // fails in DMD x86, works in GDC, LDC } Maybe enhancement req

Re: floating point conversion

2014-06-01 Thread Famous via Digitalmars-d-learn
float a = 1.234f; float b = to!float("1.234"); assert (a == b); assert (a == to!float("1.234")); // is allowed to fail due to constant folding

Re: floating point conversion

2014-06-01 Thread Famous via Digitalmars-d-learn
On Sunday, 1 June 2014 at 12:45:26 UTC, bearophile wrote: It's a bad question. Actually, Martin's question is a good one. Initializing a variable of type float via a literal or as conversion from string should be the same, exacly, always. Casting a float to double should be deterministic as

Re: floating point conversion

2014-06-01 Thread Qox via Digitalmars-d-learn
So it's just a coincidence, that GDC, LDC x86 and also DMD x86_64 doesn't fail ? (Equality) Compareision of float/float or float/double are machine dependent (depends even on x86 on the generated code (SSE/SSE2/AVX/AVX2), rounding settings, maybe event the cpu vendor etc.). On other platfor

Re: floating point conversion

2014-06-01 Thread Martin Krejcirik via Digitalmars-d-learn
On 1.6.2014 14:45, bearophile wrote: > It's a bad question. Generally to compare floating point values for > equality use std.math.feqrel. So it's just a coincidence, that GDC, LDC x86 and also DMD x86_64 doesn't fail ? And this bug https://issues.dlang.org/show_bug.cgi?id=12831 should be marked

Re: floating point conversion

2014-06-01 Thread bearophile via Digitalmars-d-learn
Martin Krejcirik: float a = 1.23f; double b = to!float("1.23"); assert (a == b); // ??? } Should the assert fail or not ? (Please reply without trying first). It's a bad question. Generally to compare floating point values for equality use std.math.feqrel. Bye, bearophile