如何在Catch框架中使用浮点公差? [英] How to use floating point tolerances in the Catch framework?

查看:110
本文介绍了如何在Catch框架中使用浮点公差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Catch 测试框架.

介绍性博客文章作者提到了以下功能:

In the introductory blog post the author mentions the following feature:

  • 以易于使用的方式支持浮点公差
  • Floating point tolerances supported in an easy to use way

我找不到有关如何执行此操作的任何文档.如何在Catch中完成?

I couldn't find any documentation on how to do this. How is this done in Catch?

推荐答案

很简单.有一个名为 Approx ,让您以一种非常易读的方式进行此测试:

It's simple. There is a class called Approx that lets you do this test in a very readable manner:

#include <limits>
TEST_CASE("demo/approx", "Approx demo") {
    double a = 1.0;
    double b = a + std::numeric_limits<double>::epsilon();
    REQUIRE_FALSE(b == a);
    REQUIRE(b == Approx(a));
}

可以使用Approx对象的成员函数epsilon()scale()来更改公差,例如:Approx(a).epsilon(e).

The tolerance can be changed by using the member functions epsilon() and scale() of the Approx object, like so: Approx(a).epsilon(e).

这篇关于如何在Catch框架中使用浮点公差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆