问题描述
我开始使用Boost ICL,偶然发现了非常基本的东西.例如,函数contains
应该返回true或false,这取决于给定元素是否在间隔中.但这对[right,left]_open_intervals
有效,但对[open,closed]_inteval
无效(请参见下面的示例).
I started to use Boost ICL and I stumbled upon very basic stuff. For example the function contains
should return true or false depending if a given element is in the interval or not. However that works for [right,left]_open_intervals
but not for [open,closed]_inteval
(see example below).
这似乎太明显了,不能忽略.我正在按预期方式使用图书馆吗?
例如(使用gcc 4.8或clang 3.3和Boost 1.54):
For example (using gcc 4.8 or clang 3.3 and Boost 1.54):
#include <boost/concept_check.hpp> //needed to make this MWE work, boost icl should include it internally
#include<boost/icl/right_open_interval.hpp>
#include<boost/icl/closed_interval.hpp>
#include<boost/icl/open_interval.hpp>
int main(){
boost::icl::right_open_interval<double> roi(6.,7.);
assert(boost::icl::contains(roi, 6.) == true); //ok
assert(boost::icl::contains(roi, 6.) == false); //ok
boost::icl::closed_interval<double> oi(4.,5.); // or open_interval
assert(boost::icl::contains( oi, 4.) == false); //error: "candidate template ignored"
assert(boost::icl::contains( oi, 5.) == false); //error: "candidate template ignored"
}
注意:上面的被称为静态"间隔(因为它们的绑定属性是该类型的一部分).动态间隔按预期工作.
Note: The above are called "static" intervals (because their bound properties are part of the type). Dynamic intervals work as expected.
推荐答案
我想这可以归结为浮点相等测试的相对无用性.
I would guess it comes down to the relative uselessness of floating point equality testing.
您曾经尝试过assert(0.1 + 0.2 == 0.3)
吗?
尝试一下.我会等待.
如果您已经知道答案,那么很清楚为什么封闭间隔不容易正确实现.背景资料:
In case you already know the answer, it'll be clear why a closed interval is not easy to implement correctly. Backgrounders:
- Is floating point math broken?
- Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
- What Every Programmer Should Know About Floating-Point Arithmetic
此外,如果您有两个连续的关闭时间间隔[a,b][b,c]
. b
属于哪个间隔?
Also, if you have two consecutive closed intervals [a,b][b,c]
. in which interval does b
belong?
这篇关于函数“包含"的基本用法包括:在Boost ICL中:是否未实现间隔类型和函数的某些组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!