<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged math</title>
<link>https://ask.clojure.org/index.php/tag/math</link>
<description></description>
<item>
<title>.pow bigdec vs bigint</title>
<link>https://ask.clojure.org/index.php/14640/pow-bigdec-vs-bigint</link>
<description>&lt;p&gt;Consider this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (.pow (bigdec 2) 3)
8M

user&amp;gt; (.pow (bigint 2) 3)
Execution error (IllegalArgumentException) at foo/eval10898 (REPL:1093).
No matching method pow found taking 1 args for class clojure.lang.BigInt
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Is that an intentional choice in the language?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Note that this works:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (.pow (biginteger 2) 3)
8
user=&amp;gt; (type (.pow (biginteger 2) 3))
java.math.BigInteger
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Except the type is not &lt;code&gt;clojure.lang.BigInt&lt;/code&gt;, but &lt;code&gt;java.math.BigInteger&lt;/code&gt;, which are different types.&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14640/pow-bigdec-vs-bigint</guid>
<pubDate>Sat, 26 Jul 2025 09:15:29 +0000</pubDate>
</item>
<item>
<title>clojure.math/scalb docstring error</title>
<link>https://ask.clojure.org/index.php/13458/clojure-math-scalb-docstring-error</link>
<description>&lt;p&gt;The docstring for &lt;code&gt;scalb&lt;/code&gt; links to the documentation for &lt;code&gt;nextDown&lt;/code&gt;, presumably just a mistake.&lt;/p&gt;
&lt;p&gt;It links here: &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#nextDown-double-&quot;&gt;https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#nextDown-double-&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But should link here: &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#scalb-double-int-&quot;&gt;https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#scalb-double-int-&lt;/a&gt;&lt;/p&gt;
</description>
<category>Docs</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13458/clojure-math-scalb-docstring-error</guid>
<pubDate>Sat, 11 Nov 2023 13:08:46 +0000</pubDate>
</item>
<item>
<title>clojure.math/floor docstring error</title>
<link>https://ask.clojure.org/index.php/13417/clojure-math-floor-docstring-error</link>
<description>&lt;p&gt;In the docstring for &lt;code&gt;clojure.math/floor&lt;/code&gt; it says:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;If a is less than zero but greater than -1.0 =&amp;gt; -0.0&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;but that's obviously not true:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(math/floor -0.3)
;; =&amp;gt; -1.0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I guess it was intended to add that line to the docstring of &lt;code&gt;clojure.math/ceil&lt;/code&gt; but added it to floor by mistake?&lt;/p&gt;
</description>
<category>Docs</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13417/clojure-math-floor-docstring-error</guid>
<pubDate>Mon, 30 Oct 2023 12:55:24 +0000</pubDate>
</item>
<item>
<title>Some corner cases that might be considered bugs in Ratio implementation</title>
<link>https://ask.clojure.org/index.php/11521/some-corner-cases-that-might-considered-ratio-implementation</link>
<description>&lt;p&gt;I was looking through the new &lt;code&gt;clojure.core/abs&lt;/code&gt; implementation on type Ratio, and it seemed that there is an assumption in the code that uses the internal details of type clojure.lang.Ratio that the denominator is always positive (e.g. see the definitions of isNeg and isPos methods for type Ratio).&lt;/p&gt;
&lt;p&gt;I started looking for cases where that assumption might be violated, and found the following, all of which might be distinctive to values of type Ratio produced by an expression with Long/MIN_VALUE as the denominator and an integer with type Long or smaller as the numerator:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version &quot;1.11.0-alpha4&quot;}}}'
Clojure 1.11.0-alpha4

user=&amp;gt; (/ 1 Long/MIN_VALUE)
-1/-9223372036854775808
user=&amp;gt; (&amp;lt; (/ 1 Long/MIN_VALUE) 0)         ;; This gives correct numerical answer
true
user=&amp;gt; (&amp;lt; (* 1 (/ 1 Long/MIN_VALUE)) 0)        ;; This does not
false
user=&amp;gt; (abs (/ 1 Long/MIN_VALUE))            ;; Gives incorrect numerical answer
1/-9223372036854775808
user=&amp;gt; (&amp;lt; (abs (/ 1 Long/MIN_VALUE)) 0)        ;; correct numerical answer
false
user=&amp;gt; (&amp;lt; (* 1 (abs (/ 1 Long/MIN_VALUE))) 0)       ;; incorrect numerical answer
true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have carefully checked other occurrences of Ratio in source file Numbers.java in Clojure's implementation, and as far as I can tell the only issue is with the method named divide in the LongOps class.  If that method avoided this problem by checking for the special case when the numerator and/or denominator is equal to Long/MIN_VALUE and behaved a little differently in that case, I do not see any other bugs.&lt;/p&gt;
&lt;p&gt;Here is another issue if the numerator is equal to Long/MIN_VALUE, where the root cause is in the same LongOps divide method:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (/ Long/MIN_VALUE -3)   ;; should return positive value, but returns negative value
-9223372036854775808/3
user=&amp;gt; (&amp;lt; (/ Long/MIN_VALUE -3) 0)
true
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11521/some-corner-cases-that-might-considered-ratio-implementation</guid>
<pubDate>Sun, 30 Jan 2022 02:24:18 +0000</pubDate>
</item>
<item>
<title>Should other math wrappers be considered?</title>
<link>https://ask.clojure.org/index.php/11516/should-other-math-wrappers-be-considered</link>
<description>&lt;p&gt;With the &lt;code&gt;java.lang.Math&lt;/code&gt; operations all being wrapped, then is it also desirable to do the same with the operations static methods on &lt;code&gt;Double&lt;/code&gt; and &lt;code&gt;Long&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;Some of these functions have simple Clojure(Script) implementations. For instance, &lt;code&gt;Double/isNaN&lt;/code&gt; can be achieved with &lt;code&gt;#(identical? ##NaN %)&lt;/code&gt; but it seems that if the language works with values like &lt;code&gt;##NaN&lt;/code&gt;, then there should be a built-in way of recognizing it. (I note that some people may not realize that &lt;code&gt;#(= ##NaN %)&lt;/code&gt; will not work).&lt;/p&gt;
&lt;p&gt;Each of these functions can also be implemented in ClojureScript. This was demonstrated while reimplementing the &lt;code&gt;clojure.math&lt;/code&gt; namespace for ClojureScript, since many of these methods from Long and Double were needed.&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11516/should-other-math-wrappers-be-considered</guid>
<pubDate>Wed, 26 Jan 2022 19:35:54 +0000</pubDate>
</item>
</channel>
</rss>