On Tuesday, 25 August 2015 at 11:14:35 UTC, Steven Schveighoffer
wrote:
On 8/24/15 5:34 PM, "=?UTF-8?B?Ik3DoXJjaW8=?= Martins\"
<[email protected]>\"" wrote:
On Monday, 24 August 2015 at 21:03:50 UTC, Steven
Schveighoffer wrote:
I understand the inconsistency, and I agree it is an issue
that should
be examined. But the issue is entirely avoidable by not using
incorrect methods to convert from floating point to integer
after
floating point operations introduce some small level of error.
Perhaps there is some way to make it properly round in this
case, but
I guarantee it will not fix all floating point errors.
What is the correct way to truncate, not round, a
floating-point value
to an integer?
auto result = cast(ulong)(x * 10.0 + x.epsilon);
-Steve
That will work in this case (or maybe not, as Marcio's other post
shows) but it's still not a general solution. You're imposing the
assumption that anything sufficiently close to an integer value
is that integer. Truncating a floating point number is not a
well-defined exercise because you only know an interval that
holds the true value.