title: How to implement floor modulo for every Number type in Kotlin?
body: <p>I'm currently learning Kotlin and trying to create an extension (infix) method that works on all <a href="https://kotlinlang.org/docs/reference/basic-types.html#numbers" rel="nofollow">number types</a> (<code>Byte</code>, <code>Long</code>, <code>Float</code>, etc.). It should work like Python's <code>%</code> operator:</p>
  
  <pre><code> 4   %   3  ==   1      // only this is the same as Java's %
   4   %  -3  ==  -2
  -4   %   3  ==   2
  -4   %  -3  ==  -1
  </code></pre>
  
  <p>...or like Java's <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int-" rel="nofollow"><code>Math.floorMod</code></a>, but it should also work with <code>Double</code> or <code>Float</code>:</p>
  
  <pre><code>-4.3 %  3.2 ==   2.1000000000000005
  </code></pre>
  
  <p>or with any possible combination of these types</p>
  
  <pre><code> 3   %  2.2 ==   0.7999999999999998
   3L  %  2.2f ==   0.7999999999999998
  </code></pre>
  
  <p>The following works as intended, but only for two <code>Double</code> or two <code>Int</code>:</p>
  
  <pre><code>inline infix fun Double.fmod(other: Double): Number {
      return ((this % other) + other) % other
  }
  
  inline infix fun Int.fmod(other: Int): Number {
      return ((this % other) + other) % other
  }
  
  // test
  fun main(args: Array&lt;String&gt;) {
      println("""
              ${-4.3 fmod 3.2} == 2.1000000000000005
  
              ${4 fmod 3} == 1
              ${+4 fmod -3} == -2
              ${-4 fmod 3} == 2
              ${-4 fmod -3} == -1
      """)
  }
  </code></pre>
  
  <p>Replacing <code>Int</code> with <code>Number</code>, I get the following error messages:</p>
  
  <pre><code>Error:(21, 18) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
  @InlineOnly public operator inline fun BigDecimal.mod(other: BigDecimal): BigDecimal defined in kotlin
  Error:(21, 27) Public-API inline function cannot access non-public-API 'internal open fun &lt;ERROR FUNCTION&gt;(): [ERROR : &lt;ERROR FUNCTION RETURN TYPE&gt;] defined in root package'
  Error:(21, 36) Public-API inline function cannot access non-public-API 'internal open fun &lt;ERROR FUNCTION&gt;(): [ERROR : &lt;ERROR FUNCTION RETURN TYPE&gt;] defined in root package'
  </code></pre>
  
  <p>How can I achieve this for every number type without copy-pasting this for every type combination?</p>