template bug?
Denton Cockburn
diboss at hotmail.com
Fri Feb 29 11:24:00 PST 2008
On Fri, 29 Feb 2008 10:46:23 -0800, Marius Muja wrote:
> Because of the nature of floating point numbers, you shouldn't use
> equality(==) to compare floating point numbers.
>
> Instead of n==1 you should write something like abs(n-1)<eps (where eps
> is a small number)
>
That wouldn't explain why it works in 1.027 and not 2.011.
The next thing, it doesn't explain why:
template factorial(real n)
{
static if (n == 1.0L)
const real factorial = 1.0L;
else
const real factorial = 5.0L;
}
void main()
{
writefln(factorial!(5.0L));
}
which is using full real comparisons, and in either case should execute
the else even if it doesn't equate, produces an output of -1.49167e-154.
1.027 produces the correct output of 5 again.
P.S. Isn't that how == is defined for floating point numbers already? If
not, shouldn't it be that way?
More information about the Digitalmars-d
mailing list