Comparison issue
James Miller
james at aatch.net
Mon Mar 19 00:01:50 PDT 2012
Hey, I'm trying to do some vector based code, and my unittests started
to fail on testing normalize. I'm using the standard algorithm, but
the assert is always return false. I did some investigation, and can
show that this program causes failure:
import std.math : sqrt;
import std.stdio : writeln;
T size(T)(T[] t){
T val = 0;
for(size_t i = 0; i < t.length; i++) {
val += t[i]*t[i];
}
return sqrt(val);
}
void normalize(T)(ref T[] t) {
auto s = size(t);
foreach(ref v; t) {
v /= s;
}
}
void main() {
float[] vector = [1,2,3,4];
normalize(vector);
auto v1 = size!float(vector);
writeln(v1 == 1); //false
writeln(v1 == 1.0); //false
writeln(v1 == 1.0f); //false
writeln(v1+1 == 2.0f); //true
}
I used typeid and can show that the type of `v1` is `float`, as you'd
expect. And the last one passes fine, as does doing `(v1+1)-1 == 1`.
I'm not sure what could be causing this. I believe it may be a bug,
but I would like to see if I'm just wrong instead.
--
James Miller
More information about the Digitalmars-d-learn
mailing list