整数div和mod在一起? [英] Integer div and mod together?

查看:88
本文介绍了整数div和mod在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道我可以使用/运算符进行整数除法,并使用%运算符获得

模数,但是有没有一次计算两个
值的函数?为了获得这两个值,两次分割

的时间似乎相当浪费,当它很容易从一个函数中返回它们时。


我看到某人的代码使用汇编程序实现了一个divmod()函数。

但它看起来不像是内置的在C ++中这样做的功能,

正确吗?看到我如何进行大量的数学计算,我想是自己写一个,但是在汇编程序中编写一个是特定于平台的,并且写了一个在C ++中似乎不合逻辑。我认为它应该是C ++标准的一部分,并由编译器实现
供应商来定位他们支持的特定平台。


有什么想法吗? (或许我应该把它带到comp.std.c ++?)


-Howard

Hi,

I know I can do integer division with the / operator, and get the
modulus with the % operator, but is there any function that calculates both
values in one shot? It seems quite wasteful in time to do the division
twice in order to get both of those values, when it would be quite easy to
return them both from a single function.

I saw someone''s code implementing a divmod() function using assembler.
But it doesn''t look like there is a built-in function for doing that in C++,
correct? Seeing as how I do a lot of mathematical calculations, I''m
thinking of writing one myself, but writing one in assembler is
platform-specific, and writing one in C++ doesn''t seem logical. It seems to
me that it ought to part of the C++ standard, and implemented by compiler
vendors to target the specific platform(s) they support.

Any thoughts? (Or perhaps I should take this to comp.std.c++?)

-Howard

推荐答案

* Howard:
我知道我可以使用/运算符进行整数除法,并使用%运算符获得
模数,但是有任何函数可以计算两者/>一次性值?为了获得这两个值,两次进行划分似乎非常浪费,当它从单个函数返回时很容易。
我看到某人的代码使用汇编程序实现了一个divmod()函数。
但它看起来没有内置函数在C ++中这样做,
正确吗?看到我如何进行大量的数学计算,我想要自己编写一个,但是用汇编语言编写一个是特定于平台的,用C ++编写一个看起来不合逻辑。似乎我认为它应该是C ++标准的一部分,并由编译器供应商实现,以针对他们支持的特定平台。

有什么想法吗? (或许我应该把这个带到comp.std.c ++?)
I know I can do integer division with the / operator, and get the
modulus with the % operator, but is there any function that calculates both
values in one shot? It seems quite wasteful in time to do the division
twice in order to get both of those values, when it would be quite easy to
return them both from a single function.

I saw someone''s code implementing a divmod() function using assembler.
But it doesn''t look like there is a built-in function for doing that in C++,
correct? Seeing as how I do a lot of mathematical calculations, I''m
thinking of writing one myself, but writing one in assembler is
platform-specific, and writing one in C++ doesn''t seem logical. It seems to
me that it ought to part of the C++ standard, and implemented by compiler
vendors to target the specific platform(s) they support.

Any thoughts? (Or perhaps I should take this to comp.std.c++?)




在优化之前测量。


你可能令人惊讶。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



Measure before you optimize.

You might be surprized.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Howard写道:
我知道我可以用/运算符进行整数除法,并用%运算符得到
模数,但是在那里任何一次计算两个值的函数?为了获得这两个值,两次进行划分似乎非常浪费,当从单个函数返回它们时很容易。


您可以尝试连续两个语句,看看

编译器是否设法正确优化它们。

我看到了某人使用汇编程序实现divmod()函数的代码。
但是看起来没有内置函数可以用C ++来实现,
正确吗?看到我如何进行大量的数学计算,我想要自己写一个,但是用汇编语言写一个是特定于平台的,


为什么*会阻止你?例如,标准库是平台和特定于实现的全部。没什么大不了。只需在你需要的每个

平台上进行操作。

并用C ++编写一个看起来不合逻辑。似乎我认为它应该是C ++标准的一部分,并由编译器供应商实现,以针对他们支持的特定平台。

有什么想法吗? (或许我应该把它带到comp.std.c ++?)
I know I can do integer division with the / operator, and get the
modulus with the % operator, but is there any function that calculates both
values in one shot? It seems quite wasteful in time to do the division
twice in order to get both of those values, when it would be quite easy to
return them both from a single function.
You might try doing it in two consecutive statements and see if the
compiler manages to optimise them properly.
I saw someone''s code implementing a divmod() function using assembler.
But it doesn''t look like there is a built-in function for doing that in C++,
correct? Seeing as how I do a lot of mathematical calculations, I''m
thinking of writing one myself, but writing one in assembler is
platform-specific,
And why is *that* stopping you? The Standard library, for example, is all
platform- and implementation-specific. No big deal. Just do it on every
platform where you need it.
and writing one in C++ doesn''t seem logical. It seems to
me that it ought to part of the C++ standard, and implemented by compiler
vendors to target the specific platform(s) they support.

Any thoughts? (Or perhaps I should take this to comp.std.c++?)




如果你打算添加这样的功能,我宁愿去''comp .std.c''。

但是我不确定所有的CPU都可以做到这一点,虽然IIRC,我每个人都写了一个装配程序,但是。 />

在C99中有三个''remquo''函数的化身,但对于浮点数它们是




V



If you propose to have such function added, I''d rather to to ''comp.std.c''.
But I am not sure all CPUs can do that, although IIRC, all I every wrote
an assembly program for, did.

There are three incarnations of ''remquo'' functions in C99, but they are
for floating point numbers.

V


Howard写道:


我知道我可以用/运算符进行整数除法,并使用%运算符获得
模数,但是有没有函数
一次计算两个值?
Hi,

I know I can do integer division with the / operator, and get the modulus with the % operator, but is there any function that calculates both values in one shot?




div函数这两者都有,但它不是(甚至接近)保证

它会比使用/和%更有效。


-

后来,

杰瑞。


宇宙是一个无花果自己的想象力。



The div function does both, but it''s not (even close to) guaranteed
that it''ll be any more efficient than using / and %.

--
Later,
Jerry.

The universe is a figment of its own imagination.


这篇关于整数div和mod在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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