Re: Fun with floating point

2015-02-08 Thread ketmar via Digitalmars-d-learn
On Sat, 07 Feb 2015 21:33:46 +, Kenny wrote: The above code snippet works correctly when I use LDC compiler (it finds expected 'f' value and prints it to console). I'm wondering is it a bug in DMD? nope, this is a bug in your code. compiler (by the specs) is free to perform intermediate

Re: Fun with floating point

2015-02-08 Thread Kenny via Digitalmars-d-learn
nope, this is a bug in your code. compiler (by the specs) is free to perform intermediate calculations with any precision that is not lower than a highest used type (i.e. not lower that `float`'s one for `while` condition (`f + eps != f`). it may be even infinite precision, so your code may

Re: Fun with floating point

2015-02-08 Thread ketmar via Digitalmars-d-learn
On Sun, 08 Feb 2015 09:05:30 +, Kenny wrote: Thanks, it's clear now. I still have one question in the above post, I would appreciate if you check it too. i've seen that, but i don't know the answer, sorry. here we have to summon Walter to explain what his intentions was, how it should

Re: Fun with floating point

2015-02-08 Thread Kenny via Digitalmars-d-learn
For example, according to IEEE-754 specification if we work with 32bit floating point numbers (floats in D) then the following pseudo code prints 'Works'. F32 f = 16777216.0f; F32 f2 = f + 0.1f; if is_the_same_binary_presentation(f, f2) Print(Works); As I understand D does not guarantee

Re: Fun with floating point

2015-02-08 Thread via Digitalmars-d-learn
On Sunday, 8 February 2015 at 09:19:08 UTC, Kenny wrote: For example, according to IEEE-754 specification if we work with 32bit floating point numbers (floats in D) then the following pseudo code prints 'Works'. F32 f = 16777216.0f; F32 f2 = f + 0.1f; if is_the_same_binary_presentation(f, f2)

Re: Fun with floating point

2015-02-08 Thread Kenny via Digitalmars-d-learn
i think you are mixing two things here. IEEE doesn't specify which internal representation compilers should use, it only specifies the results for chosen representation. so if D specs states that `float` calculations are always performing with `float` precision (and specs aren't), your sample

Re: Fun with floating point

2015-02-08 Thread Kenny via Digitalmars-d-learn
There is no right or wrong when you compare floating point values for equality (and inequality) unless those values can be represented exactly in the machine. 1.0 is famously not representable exactly. (It is similar to how 1/3 cannot be represented in the decimal system.) Here I tested one

Re: Fun with floating point

2015-02-08 Thread ketmar via Digitalmars-d-learn
On Sun, 08 Feb 2015 09:19:06 +, Kenny wrote: I asked more about this case: float f = 16777216.0f; if (f == f + 1.0f) writeln(Works); Although all operands are 32bit FP the result is not guaranteed to be equal to the result for FP32 computations as specified by IEEE-754. i think you

Re: Fun with floating point

2015-02-08 Thread via Digitalmars-d-learn
Also note that denormal numbers is an issue, e.g. D assumes that they are always supported, I think. Which frequently is not the case...

Re: Fun with floating point

2015-02-07 Thread Kenny via Digitalmars-d-learn
And sory for the typos, cannot find edit functionality here..

Re: Fun with floating point

2015-02-07 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 7 February 2015 at 21:33:51 UTC, Kenny wrote: The above code snippet works correctly when I use LDC compiler (it finds expected 'f' value and prints it to console). I'm wondering is it a bug in DMD? p.s. the final code used by both compilers: import std.stdio; import std.conv;

Re: Fun with floating point

2015-02-07 Thread Peter Alexander via Digitalmars-d-learn
On Saturday, 7 February 2015 at 23:06:15 UTC, anonymous wrote: On Saturday, 7 February 2015 at 22:46:56 UTC, Ali Çehreli wrote: 1.0 is famously not representable exactly. 1.0 is representable exactly, though. I think he meant 0.1 :-)

Re: Fun with floating point

2015-02-07 Thread Ali Çehreli via Digitalmars-d-learn
On 02/07/2015 01:33 PM, Kenny wrote: The above code snippet works correctly when I use LDC compiler (it finds expected 'f' value and prints it to console). I'm wondering is it a bug in DMD? p.s. the final code used by both compilers: import std.stdio; import std.conv; int main(string[] argv)

Re: Fun with floating point

2015-02-07 Thread Ali Çehreli via Digitalmars-d-learn
To answer your other question, there is no Edit because this is a newsgroup (see NNTP). The forum interface is supposed to be a convenience but it hides that fact. On 02/07/2015 01:33 PM, Kenny wrote: The above code snippet works correctly There is no right or wrong when you compare

Re: Fun with floating point

2015-02-07 Thread Meta via Digitalmars-d-learn
On Saturday, 7 February 2015 at 16:06:14 UTC, Kenny wrote: Hi, D community! I have this program: import std.stdio; import std.conv; int main(string[] argv) { float eps = 1.0f; float f = 0.0f; while (f + eps != f) f += 1.0f; writeln(eps = ~ to!string(eps) ~

Re: Fun with floating point

2015-02-07 Thread anonymous via Digitalmars-d-learn
On Saturday, 7 February 2015 at 22:46:56 UTC, Ali Çehreli wrote: 1.0 is famously not representable exactly. 1.0 is representable exactly, though.