I’ll try to explain what happend to me without pasting every single piece of my code because it’s a lot.
If I run this query alone it returns 6.0 (the expected behaviour)
select distancia_euclediana_para_imagenes(vector_cuadrante1,
vector_cuadrante2,
vector_cuadrante3,
vector_cuadrante4,
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)])
from imagen,pivotes
where id=id_imagen and indice_pivote=2
and if I run this other query it returns 2 (again the expected behaviour)
with distancia_a_pivote(distancia) as
(select distancia_euclediana_para_imagenes(vector_cuadrante1,
vector_cuadrante2,
vector_cuadrante3,
vector_cuadrante4,
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)])
from imagen,pivotes
where id=id_imagen and indice_pivote=2)
select id from imagen, indice, distancia_a_pivote d
where id=id_imagen and
fqa[2]>= 6-1 and
fqa[2]<= d.distancia+1;
However, with this little change it stops working and returns nothing
with distancia_a_pivote(distancia) as
(select distancia_euclediana_para_imagenes(vector_cuadrante1,
vector_cuadrante2,
vector_cuadrante3,
vector_cuadrante4,
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)],
ARRAY[SQRT(8),SQRT(8)])
from imagen,pivotes
where id=id_imagen and indice_pivote=2)
select id from imagen, indice, distancia_a_pivote d
where id=id_imagen and
fqa[2]>= d.distancia-1 and
fqa[2]<= d.distancia+1;
I’ll be grateful if you could help me because I have no idea what can cause this issue. Besides I’ve been coding in a TDD style, so I have tests to prove the right behaviour and I was pretty it was going to work
Thanks
You say that
fqa[2]is the integer 5 andd.distanciais the floating point value 6.0. That means that you end up looking at this:That 6.0 probably isn’t exact so
6.0 - 1could come out a shade less than 5.0 and your comparison would fail. One solution is to add a little bit wiggle room to account for the usual floating point problems:The 0.00001 is just an example, you’d need to look at your situation to see how much extra you should allow. Adding
ceilandfloorcalls might be another option as they would give you better control over the floating point to integer conversion.