What am I doing wrong here - canFind with iota is not working

Kingsley via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 26 03:17:10 PST 2015


On Thursday, 26 February 2015 at 11:12:42 UTC, Kingsley wrote:
> On Thursday, 26 February 2015 at 11:04:58 UTC, Baz wrote:
>> On Thursday, 26 February 2015 at 10:55:43 UTC, Kingsley wrote:
>>> float oneDegree = (PI / 180.0);
>>> float first = -(oneDegree * 10.0);
>>> float second = (oneDegree * 10.0);
>>> float step = 0.000001;
>>> float[] r = iota(first,second,step).array;
>>>
>>> writeln(r);
>>>
>>> float item = 0.174531;
>>> writeln(r.canFind(item));
>>>
>>>
>>> // returns false for canFind - even though that float is in 
>>> the array ???
>>
>> also mark your float litteral with the f postfix. By default 
>> FP litterals are double...IIRC
>>
>> float oneDegree = (PI / 180.0f);
>> float first = -(oneDegree * 10.0f);
>> float second = (oneDegree * 10.0f);
>> float step = 0.000001f;


Hardcoding the double[] does work as expected - so why doesn't it 
work with the iota generated array?

double oneDegree = (PI / 180.0);
double first = -(oneDegree * 10.0);
double second = (oneDegree * 10.0);
double step = 0.000001;
double[] r = iota(first,second,step).array;

writeln(r);

double[] hardCoded = [ 0.174521, 0.174522, 0.174523, 0.174524, 
0.174525, 0.174526, 0.174527, 0.174528, 0.174529, 0.17453, 
0.174531, 0.174532];

double item = 0.174531;
writeln(r.canFind(item));  // false - I expect true!!!!

writeln(hardCoded.canFind(item)); // true - as I expect
>> float[] r = iota(first,second,step).array;
>>
>> writeln(r);
>>
>> float item = 0.174531f;
>> writeln(r.canFind(item));
>
> Adding the f still produces a false result. Also I tried 
> changing all to double but still not working - I always get 
> false back. Interestingly if I harcode the output of the iota 
> into an array - then things start to work as I expect.



More information about the Digitalmars-d mailing list