<link rel="self" href="https://bugs.ruby-lang.org/issues/15571.atom"/>
<link rel="alternate" href="https://bugs.ruby-lang.org/"/>
<id>https://bugs.ruby-lang.org/</id>
<icon>https://bugs.ruby-lang.org/favicon.ico?1652417286</icon>
<updated>2019-01-30T04:18:22Z</updated>
<author>
<name>Ruby Issue Tracking System</name>
</author>
<entry>
<title>Ruby master - Feature #15571: Add methods: iroot, root, and roots https://bugs.ruby-lang.org/issues/15571?journal_id=765762019-01-30T04:18:22Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Third Party's Issue</i></li></ul><p>This is the (bug/feature) tracker for Ruby the language. Most gems are maintained separately. Issues for bundled gems may occasionally end up here, but the roots gem is independent. In addition, this gem seems to have been created by yourself (see <a href="https://github.com/jzakiya/roots" class="external">https://github.com/jzakiya/roots</a>), so you sure should know how to send yourself a bug report or feature request.</p> Ruby master - Feature #15571: Add methods: iroot, root, and roots https://bugs.ruby-lang.org/issues/15571?journal_id=765772019-01-30T04:26:52Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Status</strong> changed from <i>Third Party's Issue</i> to <i>Open</i></li></ul><p>Sorry, I misread this. The proposal is about including some of the methods from the gem into Ruby itself. Looking at the number of downloads on rubygems.org (8,836 for the latest version), I'm not exactly convinced this needs to be in Ruby itself.</p> Ruby master - Feature #15571: Add methods: iroot, root, and roots https://bugs.ruby-lang.org/issues/15571?journal_id=765952019-01-30T18:17:15Zjzakiya (Jabari Zakiya)
<ul></ul><p>First, I think you should see the number of downloads for the <code>roots</code> gem in a different context. Instead of saying it only has 8837 downloads, you should say, Wow, over 8800 people found it useful enough to download to solve a problem for a niche use case for Ruby. And for the majority of the life of <code>roots</code> it only had the two methods <code>root</code> and <code>roots</code>, for real|complex numbers, until I added <code>iroot</code> and <code>iroot2</code> in 2017.</p>
<p>And I hope the Facebooktizing (how many likes, or popularity) of a proposal hasn't become the defining criteria for its approval, over its merits, utility, and <code>goodness</code>.</p>
<p>I hope you take the time to actually access the underlying problem with Ruby these methods corrects. Ruby 2.5 added <code>Integer.sqrt</code> based on the underlying mathematical deficiencies (errors) it's implementation had that I raised. See the Medium post about it, and the discussion thread that lead to its ultimate inclusion.</p>
<p><a href="https://medium.com/@atul9/using-ruby-2-5s-new-integer-sqrt-cd9cb5955e12" class="external">https://medium.com/@atul9/using-ruby-2-5s-new-integer-sqrt-cd9cb5955e12</a></p>
<p><a href="https://bugs.ruby-lang.org/issues/13219" class="external">https://bugs.ruby-lang.org/issues/13219</a></p>
<p><strong>To state simply, the implementation deficiency I raised to computing integer squareroots still exists for all other integer roots. It is an inherent structural characteristic when using floating point arithmetic to approximate these values.</strong></p>
<p>The method <code>iroot</code> <strong>fixes</strong> that problem for all nth integer roots like <code>Integer.sqrt</code> does just for integer squareroots. In fact, its implementation in <code>roots</code> is just the generalization of the Newton method settled on to implement <code>Integer.sqrt</code>.</p>
<p>The methods <code>roots</code> and <code>roots</code> also provides missing functionality dealing with real and complex numbers, and fixes some errors, and unexpected results.</p>
<p><strong>Examples</strong><br>
For any root n you have n possible distinct root values. All non-real roots occur as complex conjugate pairs. Currently, their is no easy and standard way Ruby provides you to see all the n roots, or any particular one, or know their order, or quadrant it exists in.</p>
<p>Also, when taking an odd nth root of a negative real|integer you expect to get the <code>calculator</code> answer, which is the negative value of the real integer root of the positive value.</p>
<p>Here's what you currently get with straight Ruby.</p>
<pre><code>2.6.0 :> 27 ** (1.0/3) => 3.0
2.6.0 : > -27 ** (1.0/3) => -3.0
2.6.0 : > 1_000 ** (1.0/3) => 9.999999999999998
2.6.0 : > -1_000 ** (1.0/3) => -9.999999999999998
2.6.0 : > 1_000_000 ** (1.0/3) => 99.99999999999997
2.6.0 : > -1_000_000 ** (1.0/3) => -99.99999999999997
Now observe what happens when assigning values to variables.
2.6.0 : > n = 1_000_000; n ** (1.0/3) => 99.99999999999997
2.6.0 : > n = -1_000_000; n ** (1.0/3) => (50.0+86.60254037844383i) # first princple CCW root
Houston, we have a problem!
2.6.0 : > n = 1_000_000; n.root 3 => 100.0
2.6.0 : > n = -1_000_000; n.root 3 => -100.0
2.6.0 : > n = 1_000_000; n.iroot 3 => 100
2.6.0 : > n = -1_000_000; n.iroot 3 => -100
2.6.0 : > n = 1_000_000; n.roots 3 => [(100.0+0.0i), (-50.0+86.60254038i), (-50.0-86.60254038i)]
2.6.0 : > n = -1_000_000; n.roots 3 => [(50.0+86.60254038i), (-100.0+0.0i), (50.0-86.60254038i)]
</code></pre>
<p>We see for negative real values assigned to a variable, we don't get the expected <strong>calculator</strong> answer for ood roots of negative reals|integers.<br>
<strong>This is totally unexpected, inconsistent, and not desired.</strong></p>
<p><strong>The Crux of the Matter</strong></p>
<p>Every Ruby version includes new methods. Many methods are just aliases (like <code>kernel.self</code> to <code>kernel.then</code>) and some come from Rails, or other sources or inspirations. The point is they are seen as necessary to fix bugs, or provide useful functionality, or just for syntactical sugar.</p>
<p>Here, I empirically show, again, explicit deficiencies in the implementation of some fundamental mathematical operations.</p>
<p>I imagine, most people coming to Ruby and trying to do real math|algorithms that use these operations, and seeing they get wrong|unexpected results, will|have just moved on to languages that will give them correct results. Because I love Ruby, I created a gem to fix them, notified people of the deficiency with squareroots, which ended with <code>Integer.sqrt</code> included in 2.5.</p>
<p>Now, I'm requesting you finishing fixing this problem for all other roots.</p>
<p>The issue, I hope you see, is about the integrity of the language not providing known incorrect results.<br>
I could provide as many error cases as there are possible numbers, but one should be enough.</p>
<p>The absolute benefits of these methods makes doing math|science more accurate, standard, and easy for programmers,<br>
which can only enhance Ruby's reputation and utility in these fields.</p>