草庐IT

c++ - 为平台上可用的所有整数定义一个函数

coder 2024-02-23 原文

我想为签名为 INT 的不同整数类型编写一系列函数

INT safe_product(INT a, INT b, bool& error);

它接受两个整数 a 和 b,如果 a * b 没有溢出则返回 a * b,如果 a * b 溢出则返回 0 并将错误设置为真。我也希望这个功能高效,我希望它在 32 位和 64 位平台上运行。

我正在考虑使用 std::int32_tstd::uint32_tstd::int64_tstd 重载 safe_product::uint64_t 等。我相信 std::int64_t 并不总是用 32 位编译器定义的。有没有办法在编译时知道它是否被定义?

此外,如果我们在 64 位平台上,在 2 个 32 位整数之间实现安全乘积的最佳方法如下:

std::int32_t safe_product(std::int32_t a, std::int32_t b,
                          bool& error) {
  const std::int64_t a_64 = a;
  const std::int64_t b_64 = b;
  const std::int64_t ab_64 = a_64 * b_64;
  if (ab_64 > std::numeric_limits<std::int32_t>::max() ||
      ab_64 < std::numeric_limits<std::int32_t>::min()) {
    error = true;
    return 0;
  } else {
    error = false;
    return static_cast<std::int32_t>(ab_64);
  }
}

但如果我们是 32 位平台,最快的算法可能意味着计算一些整数除法。

所以我有两个问题:

  • 我如何声明我的 safe_product,以便为我的平台上可用的所有整数类型定义它(显然不是为那些不存在的整数类型)?

  • 如何使用我知道的算法使其在 32 位和 64 位上都高效?

最佳答案

以完全可移植的方式推导出最快的整数类型并不是一项简单的任务。您可能会考虑使用 int_fastXX_t 系列类型,但不能保证它们就是您想要的。您还可以查看 void* 的大小,并引入您自己的逻辑来推导您要使用的整数类型。为简单起见,我将 intunsigned int 定义为最快的整数。

首先,定义我们的“最快”整数类型和一个辅助特征,以了解类型是否小到可以提升。正如您在示例中所做的那样,任何更小的东西都会被提升为“最快”的整数类型。任何大小相等或更大的东西都将使用整数除法来预测溢出。

#include <cstdint>
#include <limits>
#include <type_traits>

// Define the fastest types for our case
using t_fast_int = int;
using t_fast_uint = unsigned int;

// Helper trait, to indicate if a type is small enough to promote
template<class T>
struct t_is_small : std::bool_constant<sizeof(T) < sizeof(t_fast_int)> {};

其次,定义一个泛型函数并使用enable_if ([link( http://en.cppreference.com/w/cpp/types/enable_if )) 只为小类型启用它。这使用您在问题中描述的方法。

template<class T>
std::enable_if_t<t_is_small<T>::value, T>
safe_product(T a, T b, bool& error)
{
    // Should we use intmax_t or uintmax_t in this case?
    using t_large = std::conditional_t<std::is_signed<T>::value, t_fast_int, t_fast_uint>;

    const t_large a_64 = a;
    const t_large b_64 = b;
    const t_large ab_64 = a_64 * b_64;
    if (ab_64 > std::numeric_limits<T>::max() ||
        ab_64 < std::numeric_limits<T>::min())
    {
        error = true;
        return 0;
    }
    else
    {
        error = false;
        return static_cast<T>(ab_64);
    }
}

最后,为大整数类型添加另一个重载。请注意,enable_if 条件已反转。我使用整数除法来预测上溢或下溢。

template<class T>
std::enable_if_t<t_is_small<T>::value == false, T>
safe_product(T a, T b, bool& error)
{
    if(b == 0) {
        // The result will be zero (avoids division by zero below)
        error = false;
    }
    else {
        // Calculate the largest `a` that would not result in an overflow
        constexpr auto max_int = std::numeric_limits<T>::max();
        auto max_a = max_int / b;

        // Calculate the smallest `a` that would not result in underflow
        constexpr auto min_int = std::numeric_limits<T>::min();
        auto min_a = min_int / b;

        // If a is greater than max_a an overflow would occur
        // If a is less than min_a an undeflow would occur
        if(b > 0) {
            error = (a > max_a) || (a < min_a);
        }
        else {
            error = (a < max_a) || (a > min_a);
        }
    }
    if(error) {
        return 0;
    }
    else {
        return a * b;
    }
}

关于c++ - 为平台上可用的所有整数定义一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41891582/

有关c++ - 为平台上可用的所有整数定义一个函数的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  3. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  8. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  9. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  10. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

随机推荐