https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112021-09-20T06:48:04ZRuby Issue Tracking SystemRuby master - Feature #18179: Add Math methods to Numerichttps://bugs.ruby-lang.org/issues/18179?journal_id=937652021-09-20T06:48:04Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>I support this. <code>x.sqrt</code> is indeed more object-oriented that <code>Math.sqrt x</code>. In an earlier discussion, it was pointed out that for Mathematicians, <code>sqrt(x)</code> is more natural than <code>x.sqrt</code>. Mathematicians can still use that notation, but also having the object-oriented notation in Ruby would indeed be great.</p> Ruby master - Feature #18179: Add Math methods to Numerichttps://bugs.ruby-lang.org/issues/18179?journal_id=937792021-09-22T01:44:45Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>I'm negative to this proposal. I don't think <code>Math.sqrt</code> is the behavior or the property of a Numeric object. It is the positive square root function that maps from/to the set of non-negative real numbers.</p>
<p>If we introduce <code>Numeric#sqrt</code>, we should expand its domain to negative numbers and Complex numbers. Also, it is better to consider other kinds of numbers, such as Quaternion.</p> Ruby master - Feature #18179: Add Math methods to Numerichttps://bugs.ruby-lang.org/issues/18179?journal_id=958982022-01-12T00:56:54Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Has duplicate</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/18477">Feature #18477</a>: Float#sqrt and Integer#sqrt</i> added</li></ul> Ruby master - Feature #18179: Add Math methods to Numerichttps://bugs.ruby-lang.org/issues/18179?journal_id=970112022-03-24T04:44:24Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>mrkn (Kenta Murata) wrote in <a href="#note-2">#note-2</a>:</p>
<blockquote>
<p>I'm negative to this proposal. I don't think <code>Math.sqrt</code> is the behavior or the property of a Numeric object. It is the positive square root function that maps from/to the set of non-negative real numbers.</p>
</blockquote>
<p>This is how Mathematicians think, and we don't want to take this possibility away from them. But Ruby is mainly for Ruby programmers. And most Ruby programmers may not think like Mathematicians. Ruby programmers should be able to use Ruby the Ruby way, even for such functionality.</p>
<p>For example, if I have an array <code>a</code>, and want to create an array of square roots from it, currently I have to write:</p>
<pre><code>a.map { |n| Math.sqrt n }
</code></pre>
<p>With this proposal, it would be possible to write:</p>
<pre><code>a.map(&:sqrt)
</code></pre>
<p>This expresses the intent (map the squareroot function) in a much more concise and functional way.</p>
<blockquote>
<p>If we introduce <code>Numeric#sqrt</code>, we should expand its domain to negative numbers and Complex numbers. Also, it is better to consider other kinds of numbers, such as Quaternion.</p>
</blockquote>
<p>Now that Complex is built-in, producing an error on <code>Math.sqrt(-2)</code> indeed doesn't look good. That's independent of this proposal, but it could be implemented together.</p> Ruby master - Feature #18179: Add Math methods to Numerichttps://bugs.ruby-lang.org/issues/18179?journal_id=970122022-03-24T07:13:22ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>For the Complex Problem, maybe add an optional parameter like <code>:raise_on_complex</code> or something so when you try:</p>
<p><code>(-2).sqrt</code> it returns complex,<br>
but <code>(-2).sqrt(raise_on_complex: true)</code> makes the old Exception?</p> Ruby master - Feature #18179: Add Math methods to Numerichttps://bugs.ruby-lang.org/issues/18179?journal_id=970132022-03-24T14:54:44ZEregon (Benoit Daloze)
<ul></ul><p>Out of all Math methods:</p>
<pre><code> acos, acosh, asin, asinh, atan, atan2, atanh, cbrt, cos, cosh, erf,
erfc, exp, frexp, gamma, hypot, ldexp, lgamma, log, log10, log2, sin,
sinh, sqrt, tan, tanh
</code></pre>
<p>I think for me only sqrt would maybe feel natural as <code>num.sqrt</code>.</p>
<p>We already have <code>Integer.sqrt</code> (but no <code>#sqrt</code>), so that might be confusing.<br>
<code>Math</code> always deals with Float numbers, so that's consistent, but having it on numeric is less clear, should <code>5.sqrt</code> be <code>2</code> (like <code>Integer.sqrt</code>) or <code>2.23606797749979</code> (like <code>Math.sqrt</code>)?<br>
It's also not clear what should be <code>Rational(a, b).sqrt</code>, a Float, a Rational? The point of Rational is to be exact, converting to Float somewhat implicitly doesn't seem good.</p>
<p>At least I feel <code>num.sin</code>, <code>num.cos</code>, <code>num.acos</code>, <code>num.cosh</code>, etc, all look weird.<br>
I would think many people would generally agree to that, but maybe I'm wrong.</p>
<p><code>Math.hypot</code> seems also a good example why that should not be an instance method, because it takes two arguments as equal, there is no "receiver" and "operand" distinction.</p>
<hr>
<p>Moving these methods to <code>Float</code> (instead of <code>Numeric</code>) would at least make it clear they use Float operands and return a Float, and sounds like a better change to me.<br>
That would mean <code>integer.to_f.sqrt</code> if one wants the Float square root of an integer.</p>
<p>It seems Rust defines most of the Ruby Math methods on f64 (<a href="https://doc.rust-lang.org/std/primitive.f64.html" class="external">https://doc.rust-lang.org/std/primitive.f64.html</a>) and much less (e.g., no sqrt) on i64 (<a href="https://doc.rust-lang.org/std/primitive.i64.html" class="external">https://doc.rust-lang.org/std/primitive.i64.html</a>).</p>