Re: C++ more efficient than C?



In article <c74c2$47fd1188$24555@xxxxxxxxxxxxxxxxx>,
Dann Corbit <dcorbit@xxxxxxxxx> wrote:

Suppose we have x, x+e, and x+2e, x+3e, x+4e. Your comparison
function may sort them as x+4e, x+3e, x+2e, x+e, x. Now you search
for x. bsearch() will compare it with x+2e, and find it's less. It
will then compare it with x+3e and x+4e, and conclude that it is not
present in the array. It will give the wrong answer.

Assuming that this is the wrong answer is a mistake. If you are searching
for:
x+2e
and you find:
x+4e
then you found the number that you were searching for, if it is floating
point. If you are not happy with that answer then you should not use
floating point.

Read more carefully. It doesn't find the wrong entry. It wrongly
says there is no such value in the array. According to you, there are
several, but it doesn't find *any* of them.

It says it must be a total order. Yours isn't.

Achieving total order is literally not possible, because floating point is
inexact. This statement is absolutely true.

Then you'd better not use floating point with qsort().

-- Richard
--
:wq
.



Relevant Pages

  • Re: rounding off double values
    ... while the second array is result of some calculation ... compare elements of both arrays .How do i do the rounding off? ... That's where floating points becomes scary, ... zero than far from zero, but this seems to measure whether the program ...
    (comp.lang.java.programmer)
  • Re: comparison on non-integer types
    ... http://c-faq.com/thatisall about floating point numbers. ... mathematically but which do not compare the same. ... if you're writing a qsort compar function for an array of floats. ...
    (comp.lang.c)
  • A binary search algorithm that searches for an element similar to the key
    ... This function should return a pointer to the element of the array that is the greatest among the element with a lower value than key. ... I started from a bsearch() implementation... ... binsearch_low (const void *key, const void *array, size_t count, size_t size, comparison_fn_t compare) ...
    (comp.lang.c)
  • Re: A binary search algorithm that searches for an element similar to the key
    ... You are expecting a pointer, not the value the pointer points to. ... I started from a bsearch() implementation... ... size, comparison_fn_t compare) ... This will return a pointer to the array element that matches key. ...
    (comp.lang.c)
  • hi
    ... contains floating points value.i have to store the data by using array ... of structure.i have to compare the previous value and check the ...
    (microsoft.public.vc.mfc)