type comparisons
Bill Baxter
dnewsgroup at billbaxter.com
Sat Jan 26 10:53:46 PST 2008
Denton Cockburn wrote:
> On Sat, 26 Jan 2008 18:36:41 +0000, BCS wrote:
>
>> Reply to Denton,
>>
>>> [quoted text muted]
>> rounding error?
>>
>> rounding 0.6584 (base 10) to real and then converting to double may round
>> different than rounding it directly to double.
>
> I can't think of a reason that would be acceptable.
> This means I can't reliably do operations with converted reals/doubles :(
>
> This is also weird:
> double a = 0.65;
> real b = a + 0.1;
> b -= 0.1;
> assert(b == a); // fails
>
> real c = a;
> c += 0.1;
> c -= 0.1;
> assert(c == a); // passes
You should never compare floating point values using ==. It's not D's
fault, it's the lossy nature of floating point calculations themselves.
Instead do something like
assert(abs(c-a) < rounding_tolerance);
--bb
More information about the Digitalmars-d-learn
mailing list