Small Basic - About Math.Remainder

I think you know about Math.Remainder operation of Small Basic.  But the detail seems not so popular.

Usually, this function is used for natural numbers.  But it can be used for such as negative numbers or decimal numbers.

Small Basic doesn't have Math.Int operation.  But old BASIC and some other languages have this function.  Following code shows about Int operation.

Sub ``Math_Int

  ``' param q

  ``' return int

  `` If `` 0 ``<`` = `` q ``Then

    `` int `` = ``Math``.``Floor``(``q``)

  ``Else

    `` int `` = ``Math``.``Ceiling``(``q``)

  ``EndIf

EndSub

Using Int, we can write following identical equation.

dividend = Int(dividend / divisor) * divisor + Math.Remainder(dividend, divisor)

You can confirm the equation above with following code.

While ``"True"

  ``TextWindow``.``Write``(``"dividend? "``)

  `` dividend `` = ``TextWindow``.``Read``(``)

  ``TextWindow``.``Write``(``"divisor? "``)

  `` divisor `` = ``TextWindow``.``Read``(``)

  `` r `` = ``Math``.``Remainder``(``dividend``,``divisor``)

  ``TextWindow``.``WriteLine``(`` "r=Math.Remainder(dividend,divisor)=" `` + ``r``)

  `` q `` = `` dividend `` / ``divisor

  ``TextWindow``.``WriteLine``(`` "q=dividend/divisor=" `` + ``q``)

  ``Math_Int``(``)

  ``TextWindow``.``WriteLine``(`` "Int(q)=" `` + ``int``)

  ``TextWindow``.``WriteLine``(`` "Int(q)*divisor+r=" `` + ``(``int``*``divisor``+``r``)``)

  ``TextWindow``.``WriteLine``(``""``)

EndWhile

And in order to understand Math.Remainder more deeply, please look at following graph y = Math.Remainder(x, -1.5).  This graph is drawn with a program RQX345-1,

We can also write following identical equation.

Math.Remainder(dividend, divisor) = Math.Remainder(dividend, -divisor)


See Also

Following TechNet Wiki article contains graphs such as Math.Floor(x), Math.Ceiling(x), and Math.Remainder(x, 2).