Project

General

Profile

Actions

Bug #19713

closed

Off-by-one error when computing very large Integer numbers

Added by bannable (Joe Truba) 11 months ago. Updated 11 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:113773]

Description

Ruby computes this Elliptic Curve result incorrectly when using Integer operations, but has the correct result when using Rational:

a = 154476802108746166441951315019919837485664325669565431700026634898253202035277999
b = 36875131794129999827197811565225474825492979968971970996283137471637224634055579
c = 4373612677928697257861252602371390152816537558161613618621437993378423467772036
int = a / (b + c) + b / (a + c) + c / (a + b)

a = a.to_r
rational = a / (b + c) + b / (a + c) + c / (a + b)
puts [RUBY_VERSION, int == 4, rational == 4].join(' ')

Unfortunately, I'm not sure how to minimize this to something simpler than an EC computation.

Actual

➜  ~ asdf local ruby latest && ruby foo.rb
3.2.1 false true
➜  ~ asdf local ruby 3.1.3 && ruby foo.rb
3.1.3 false true
➜  ~

Expected

➜  ~ asdf local ruby latest && ruby foo.rb
3.2.1 true true
➜  ~ asdf local ruby 3.1.3 && ruby foo.rb
3.1.3 true true
➜  ~

Updated by mame (Yusuke Endoh) 11 months ago

  • Status changed from Open to Rejected

It is by design. Integer#/ does integer division.

p 1 / 2 #=> 0

Updated by nobu (Nobuyoshi Nakada) 11 months ago

You can use Integer#quo.

a.quo(b+c) + b.quo(a+c) + c.quo(a+b) #=> (4/1)
Actions

Also available in: Atom PDF

Like0
Like0Like0