#326 closed defect (fixed)
psVectorSort doesn't
| Reported by: | Paul Price | Owned by: | |
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | types | Version: | unspecified |
| Severity: | critical | Keywords: | |
| Cc: |
Description
Calling psVectorSort with a psVector of type F32 yields a vector with the
elements in the same order as the input:
Input vector:
0.186915 0.951040 0.545436 0.074626 0.229219 0.522971 0.092768
Sorted vector:
0.186915 0.951040 0.545436 0.074626 0.229219 0.522971 0.092768
This is because the comparison function is effectively:
int psCompareF32(float a, float b) {
return a - b;
}
Note that, for the above list of numbers (the absolute difference between any of
which is always less than unity), this comparison function will always return 0
(meaning that all the values are equal). The comparison function should be
something like:
int psCompareF32(float a, float b)
{
float diff = a - b;
return (diff > 0.0) ? +1 : ((diff < 0.0) ? -1 : 0);
}
Code used for test:
#include <stdio.h>
#include "pslib.h"
#define SIZE 7
int main(int argc, char *argv[])
{
psTraceSetLevel(".", 10);
psRandom *random = psRandomAlloc(PS_RANDOM_TAUS, 1);
psVector *vector = psVectorAlloc(SIZE, PS_TYPE_F32);
printf("Input vector:\n");
for (int i = 0; i < SIZE; i++) {
vector->data.F32[i] = psRandomUniform(random);
printf("%f ", vector->data.F32[i]);
}
printf("\n");
psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN);
(void)psVectorStats(stats, vector, NULL, NULL, 0);
printf("Median: %f\n", stats->sampleMedian);
psVector *sorted = psVectorAlloc(SIZE, PS_TYPE_F32);
sorted = psVectorSort(sorted, vector);
printf("Sorted vector:\n");
for (int i = 0; i < SIZE; i++) {
printf("%f ", sorted->data.F32[i]);
}
printf("\n");
for (int i = 0; i < SIZE - 1; i++) {
printf ("%d ", psCompareF32(&sorted->data.F32[i], &sorted->data.F32[i+1]));
}
printf("\n");
}
Change History (4)
comment:1 by , 21 years ago
| Status: | new → assigned |
|---|
comment:2 by , 21 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
I rewrote the F32 and F64 versions of the compare functions to something along
the line of your example. I used FLT_EPSILON / DBL_EPSILON instead of 0.0,
though, as if two floats/doubles are within epsilon of each other, they are to
be considered 'equal'.
I'll update Ross's tests for regression purposes as well. The fix will be in
the next release.

That sounds serious; I'll need to look into what Ross did on that one.