On Monday, February 26, 2018 16:04:59 H. S. Teoh via Digitalmars-d-learn wrote: > On Mon, Feb 26, 2018 at 11:34:06PM +0000, psychoticRabbit via > Digitalmars-d-learn wrote: [...] > > > and what's going on here btw? > > > > assert( 1 == 1.000000000000000001 ); // assertion error in DMD but not > > in LDC > > assert( 1 == 1.0000000000000000001 ); // no assertion error?? > > > > (compiled in 64bit mode) > > A 64-bit double can only hold about 14-15 decimal digits of precision. > Anything past that, and there's a chance your "different" numbers are > represented by exactly the same bits and the computer can't tell the > difference. > > If you want arbitrary precision you'll have to use an arbitrary > precision library like GMP. It will be a lot slower than built-in > floats or doubles, though. > > In general, real numbers have an infinite number of digits, and so would > require infinite memory and infinite time to perform any computations at > all. What we have now is a reasonably practical compromise. :-D But > that also means there will be some inherent inexactness because we're > trying to represent an infinite number of digits in a small, finite > space. > > (There *are* exact representations for certain subsets of irrationals > that allow fast computation that does not lose precision. But generally, > they are only useful for specific applications where you know beforehand > what form(s) your numbers will take. For general arithmetic, you have to > compromise between speed and accuracy.)
No. No. No. Floating point values are just insane. ;) http://dconf.org/2016/talks/clugston.html In all seriousness, floating point values do tend to be highly frustrating to deal with, and personally, I've gotten to the point that I generally avoid them unless I need them. And much as that talks is titled "Using Floating Point Without Losing Your Sanity," I came away from it even more convinced that avoiding floating point values is pretty much the only sane solution - though unfortunately, sometimes, you really don't have a choice. - Jonathan M Davis