Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return nearest element from an ordered set?
    primarykey
    data
    text
    <p>I have a vector which contains some floating point values reasonably apart from each other and sorted according to some function. For example,</p> <pre><code>double foo(double x) { return 199.1*x; } double x = 3000000.3157; double y = x + DBL_EPSILON; std::vector&lt;double&gt; s { y,y+10}; std::sort(s.begin(),s.end(),[](double x,double y) { return foo(x) &lt; foo(y) ;} ); </code></pre> <p>Now someone has a key which is close to the one I have in <code>s</code>, such as <code>x</code>. In the era of <a href="https://en.wikipedia.org/wiki/Lambda_calculus" rel="nofollow">lambda</a>, all have their own little function to search, like</p> <pre><code>std::cout&lt;&lt;std::distance(s.begin(),std::lower_bound(s.begin(),s.end(),x, [] (double x,double y) { return foo(x) &lt; foo(y);}))&lt;&lt;std::endl; std::cout&lt;&lt;std::distance(s.begin(),std::lower_bound(s.begin(),s.end(),x, [] (double x,double y) { double f1 = foo(x); double f2 = foo(y); return f1 &lt; f2;}))&lt;&lt;std::endl; </code></pre> <p>and gets different position (and the corresponding values are very different).</p> <p>While looking at the usage, it appeared, they are related to <strong>finding for a key <code>k</code></strong></p> <ul> <li><strong>Nearest element from an ordered set of well-placed floating point values.</strong></li> <li><strong>A ratio, <code>r</code>, which (ideally should be [0,1] ) attached to consecutive values <code>x1</code> &amp; <code>x2</code> such that return value of a function <code>f(x1,x2,r)</code> is approximately equal to <code>k</code>.</strong></li> </ul> <p>Both of them looks like related, and related to interpolation. How do I implement them?</p> <p>NOTE:</p> <p>In the short code below</p> <pre><code>double f1 = foo(x); double f2 = foo(y); bool l = foo(x) &lt; foo(y); std::cout&lt;&lt;std::boolalpha&lt;&lt;(f1&lt;f2)&lt;&lt; " "&lt;&lt;l&lt;&lt;" "&lt;&lt;(f1 == f2) &lt;&lt; std::endl; std::cout &lt;&lt; std::boolalpha &lt;&lt; (foo(x) &lt; foo(y)) &lt;&lt; " "&lt;&lt; (foo(y) &lt; foo(x)) &lt;&lt; " "&lt;&lt;(foo(x) == foo(y) )&lt;&lt;std::endl; std::cout &lt;&lt; std::boolalpha &lt;&lt; std::isless(foo(x) , foo(y)) &lt;&lt; " "&lt;&lt; std::isless(foo(y) , foo(x)) &lt;&lt;std::endl; </code></pre> <p>I get output with <a href="http://en.wikipedia.org/wiki/GNU_Compiler_Collection" rel="nofollow">GCC</a> on a X86 machine as</p> <pre><code>false true true true true false false false </code></pre> <p>While my guess is that GCC does higher precision (80bit) on the fly unless I force it to store the result, resulting different result for <code>l</code> &amp; <code>(f1&lt;f2)</code> (which caused the problem stated above) . I will also be interested to know why <code>foo(x) &lt; foo(y)</code> and <code>foo(y) &lt; foo(x)</code> both says <code>true</code>!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload