Ruby中的负数除法

时间:2019-07-09 13:02:22

标签: ruby

任何人都可以解释下面的负数除法结果:

2.6.1 :001 > -25/24
 => -2 
2.6.1 :002 > 25/24
 => 1 

为什么是-2而不是-1

有关奇怪行为的更多示例:

2.6.1 :003 > 24/25
 => 0 
2.6.1 :004 > -24/25
 => -1 

有什么想法吗? 谢谢。

4 个答案:

答案 0 :(得分:4)

您可以考虑整数除法的一种方法是:

  1. 您进行浮点除法。
  2. 您将结果四舍五入。

因此:

    searchController.searchBar.delegate = self

    navigationItem.title = navigationItem.title ?? ci("plan_p")

    tableView.rowHeight = 100.0
    tableView.tableHeaderView = searchController.searchBar

    tableView.setContentOffset(CGPoint(x: 0.0, y: (self.tableView.tableHeaderView?.frame.size.height)!), animated: false)

    guard let projectId = GlobalState.selectedProjectId, let byProject : Results<Structure> = self.by(projectId: projectId) else { return }

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Suche nach Plan"
    definesPresentationContext = true

答案 1 :(得分:4)

不同的编程语言对(负)整数除法使用不同的定义。 Ruby使用Knuth,第p页中的Concrete Mathematics描述的代码。 82.

可以用一个例子来说明:假设我们正在工作数周。我们的星期从星期日开始。数字从零开始,因此我们从第0周,第0天开始:

----- 0 -----
0 1 2 3 4 5 6
S M T W T F S
^

如果我们将向前移动 9天,那么我们将在1的第2天:

----- 0 -----   ----- 1 -----
0 1 2 3 4 5 6   0 1 2 3 4 5 6
S M T W T F S   S M T W T F S
                    ^

这些值由/%返回:

9 / 7 #=> 1
9 % 7 #=> 2

如果我们将向后移动 9天(从0开始),那么我们将在-2周,第5天:

---- -2 -----   ---- -1 -----   ----- 0 -----
0 1 2 3 4 5 6   0 1 2 3 4 5 6   0 1 2 3 4 5 6
S M T W T F S   S M T W T F S   S M T W T F S
          ^

使用/%

-9 / 7 #=> -2
-9 % 7 #=> 5

在该页面的空白处有一个有趣的注释:

  

提防使用其他定义的计算机语言。

答案 2 :(得分:3)

我可以使用括号简单地找到所需的功能,

{
'ID': '1', 
'Name': 'PROF DR BACHTIAR ALY MA', 
'Faction': 'National Democrat Party Faction', 
'Constituency': 'ACEH I', 
'Educational Background': ['Sekolah Rakyat Tahun:  - 1963', ' SMPN 1 Banda Aceh Tahun:  - 1965', 'IPS SMA YPU Bandung Tahun:  - 1968', 'Fakultas Publistik UNPAD Tahun: 1969 - 1971', 'Komunikasi Westfaelishe Wilhemlins Tahun: 1974 - ', 'Philosophische Universitas Westfalischen Jerman Tahun:  - 1984'],
'Working Experience': ['Pokja Pengaduan Dewan Pers Sebagai: Anggota Tahun: 2003 - 2006', 'Anggota Dewan Pers Sebagai: Anggota Tahun: 2000 - 2003', 'Penasehat Ahli Kapolri Sebagai: Penasehat Tahun: 1999 - 2014', 'Konsultan PT Arun Sebagai:  Tahun: 1989 - 1994', 'Kadin Indonesia Komite Cina Sebagai: Staf Ahli Tahun: 1985 - 1986', 'Penasehat Presiden Urusan Aceh Sebagai: Penasehat Tahun:  - '],
'Organizational': ['BAKOM PKB Sebagai: Ketua Tahun: 1990 - 1995', 'PPI & IASI Jerman Sebagai: Ketua Tahun: 1976 - 1982', 'Ketua Warga SMP & Ketua Osis SMA Sebagai:  Tahun: 1965 - 1968', 'KAPPI Daerah Itimewa Aceh Sebagai: Ketua Tahun:  - 1968'], 
'Movement': ['KAPPI Aceh - Ketua Tahun:  - 1968']
}

也许,为什么-25/24使您感到惊讶:

对于浮点除法的实际值,请提供exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { const agent = new WebhookClient({ request, response }); function phonecheck(agent) { const phonec = agent.parameters.phone; if (!phonec) { agent.add(`Please enter phone number`); } } let intentMap = new Map(); intentMap.set('fees_intent', phonecheck); agent.handleRequest(intentMap); });

您使用整数除法得到的结果将向下舍入为-2的负值。这也称为地板部门

答案 3 :(得分:0)

Ruby中的Interger除法返回底数或除法的浮点结果:

https://ruby-doc.org/core-2.6/Numeric.html#method-i-div

  

使用/进行除法,然后将结果转换为整数。数字未定义/运算符;这留给子类。

     

相当于num.divmod(numeric)[0]

divmod使用floor: :

q = floor(x/y)
x = q*y + r