我可以在MSVC ++中使用128位整数吗? [英] Can I use 128-bit integer in MSVC++?

查看:315
本文介绍了我可以在MSVC ++中使用128位整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2010编写C ++/MFC应用程序,我需要维护一个累积的running total,该累积值将用于计算平均传输速率,例如:

I'm coding a C++/MFC application using Visual Studio 2010 and I need to maintain a cumulative running total that will be used to calculate an average transfer rate, as such:

//Let's assume that INT128 is a 128-bit integer type
static INT128 iRunningTotal = 0;
static INT128 iCounter = 0;

LONGLONG iteration_get_current_average(LONGLONG iRate)
{
    //May be called repeatedly...
    iRunningTotal += iRate;
    iCounter++;

    //Calculate the current average
    return iRunningTotal / iCounter;
}

我搜索了C++ 128-bit integer,几乎所有人们建议使用Boost库的地方.好吧,这是有可能的,但是我对此并不熟悉,并且不在我的项目中的其他任何地方使用它.

I searched for C++ 128-bit integer and pretty much everywhere people suggest using Boost library. Well, that's a possibility, but I'm not familiar with it and don't use it anywhere else in my project.

因此,我很好奇,除了Boost,有没有办法用纯C/C ++做到这一点?

So Boost aside, I'm curious, is there a way to do this with pure C/C++?

推荐答案

我将不理会这是一个好主意,还是您所测量的物理量甚至在理论上是否可以超过2的值的问题^ 63或10 ^ 19左右.我敢肯定你有你的理由.那么在纯C/C ++中您有什么选择呢?

I shall leave aside the question of whether it's a good idea, or whether the physical quantity you're measuring could even in theory ever exceed a value of 2^63, or 10^19 or thereabouts. I'm sure you have your reasons. So what are your options in pure C/C++?

答案是:不很多.

  • 128位整数不是任何标准的一部分,我知道的编译器也不支持它们.
  • 64位double将为您提供动态范围(10 ^ 308左右).如果您不需要确切的答案,这是一个绝佳的选择.不幸的是,如果您有一个带有足够零的数字并添加一个零,那么它就不会改变.
  • 浮点处理器本身支持80位双精度字,它为您提供63位尾数以及扩展的动态范围.

那么,如何使用自己的128位整数算法?您真的必须成为受虐狂.进行加法和减法(注意携带)非常容易,并且经过一点思考,进行乘法并不难.划分完全是另一回事.这非常困难,可能的结果是类似于1990年代的Pentium错误的错误.

So, how about roll-your-own 128 bit integer arithmetic? You would really have to be a masochist. It's easy enough to do addition and subtraction (mind your carries), and with a bit of thought it's not too hard to do multiplication. Division is another thing entirely. That is seriously hard, and the likely outcome is bugs similar to the Pentium bug of the 1990s.

您可能会很容易地将计数器堆积为两个(或多个)64位整数.然后将它们转换为双精度,以便最后进行计算.那应该不太难.

You could probably accumulate your counters in two (or more) 64 bit integers without much difficulty. Then convert them into doubles for the calculations at the end. That shouldn't be too hard.

那之后,恐怕要去图书馆购物了.您提到过的Boost,但周围还有更多专门的库,例如cpp-bigint.

After that I'm afraid it's off to library shopping. Boost you mentioned, but there are much more specialised libraries around, such as cpp-bigint.

毫不奇怪,此问题已被问过并得到很好的回答:代表C ++中的128位数字.

Not surprisingly, this question has been asked before and has a very good answer: Representing 128-bit numbers in C++.

这篇关于我可以在MSVC ++中使用128位整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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