为什么标准库中有'div()' [英] Why is there 'div()' in the standard lib

查看:70
本文介绍了为什么标准库中有'div()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道div()的实施原因吗?


-

Joe Wright

一切都应该是尽可能简单,但并不简单。

---阿尔伯特爱因斯坦---

Does anybody know why div() was implemented?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

推荐答案

Joe Wright说:
Joe Wright said:

有人知道div()的实现原因吗?
Does anybody know why div() was implemented?



那些已经实现了自己的任意精确算术库的人将会特别注意到执行一个部门的事实

你剩余免费。因此,如果你需要一个除法的结果

*和*一个余数,而不是写w = x / y; r = x%y;至少

一些机器你可以只用一个机器的成本...如果只有

你有它的语法。 div函数提供了该语法。 (在

引擎盖下,毫无疑问它会做一些特定于机器的事情。)


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Those who have implemented their own arbitrary precision arithmetic library
will be particularly aware of the fact that performing a division gives
you a remainder for free. So if you need both the result of a division
*and* a remainder, instead of writing w = x / y; r = x % y; on at least
some machines you could have them both for the cost of only one... if only
you had the syntax for it. The div function provides that syntax. (Under
the hood, doubtless it will do something nastily machine-specific.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


11月27日下午6:03,Joe Wright< joewwri ... @ comcast.netwrote:
On Nov 27, 6:03 pm, Joe Wright <joewwri...@comcast.netwrote:

有人知道div()的实现原因吗?
Does anybody know why div() was implemented?



在茶壶中看起来似乎是暴风雨同时获得div和mod

结果,但我想他们想要一个便携式的方法来做并计算了

,图书馆实施者可以比平均每个b $ b b更快地完成它。


结果是适用于时间计算之类的事情(例如

将一万亿蒲式耳转换为年/月/日/小时/分钟/秒)。

It does seem a tempest in a teapot to get simultaneous div and mod
results, but I guess they wanted a portable way to do that and figured
that the library implementor would be able to do it faster than
average.

It turns out to be useful for things like time calculations (e.g.
convert a bushel of seconds into year/month/day/hour/min/sec).


Joe Wright< joewwri ... @ comcast.netwrote:
Joe Wright <joewwri...@comcast.netwrote:

有人知道div()的实现原因吗?
Does anybody know why div() was implemented?



不,但是其他帖子尽管如此,值得注意的是

除以div返回的除法和余数不必是

与C89下的/和%返回的相同(根据

我可能已更改的草稿)。


-

彼得

No, but other posts notwithstanding, it''s worth noting that
the division and remainder returned by div need not be the
same as that returned by / and % under C89 (according to
the draft I have which may have changed).

--
Peter


这篇关于为什么标准库中有'div()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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