summaryrefslogtreecommitdiffstats
path: root/helpcontent2/source/text/sbasic/shared/03070600.xhp
blob: ed510a94cd265320a63a467f3ce75b3cc27dc9f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->

<meta>
  <topic id="textsbasicshared03070600xml" indexer="include" status="PUBLISH">
    <title id="tit" xml-lang="en-US">Mod Operator</title>
    <filename>/text/sbasic/shared/03070600.xhp</filename>
  </topic>
</meta>

<body>

<section id="mod">
<bookmark xml-lang="en-US" branch="index" id="bm_id3150669">
  <bookmark_value>MOD  operator (mathematical)</bookmark_value>
</bookmark>

<h1 id="hd_id3150669"><variable id="MOD_h1"><link href="text/sbasic/shared/03070600.xhp" name="Mod Operator">Mod Operator</link></variable></h1>
<paragraph id="par_id3148686" role="paragraph" xml-lang="en-US">The <literal>MOD</literal> operator takes in two numeric expressions and returns the remainder of the division.</paragraph>
</section>
<paragraph id="par_id3148004" role="paragraph" xml-lang="en-US">For example, the result of <literal>21 MOD 6</literal> is <literal>3</literal> because after dividing 21 by 6, the remainder of the division is 3.</paragraph>
<paragraph role="paragraph" id="par_id111617300964049">If the <literal>MOD</literal> operation involves non-integer values, both operands are rounded to the nearest integer values. Hence, the value returned by a <literal>MOD</literal> operation will always be an integer number.</paragraph>
<paragraph role="paragraph" id="par_id561617302820104">For example, the expression <literal>16.4 MOD 5.9</literal> is evaluated as follows:</paragraph>
<list type="ordered">
    <listitem>
        <paragraph id="par_id151617302878527" role="listitem">The value 16.4 is rounded to 16.</paragraph>
    </listitem>
    <listitem>
        <paragraph id="par_id351617303087259" role="listitem">The value 5.9 is rounded to 6.</paragraph>
    </listitem>
    <listitem>
        <paragraph id="par_id91617303114774" role="listitem">The operation <literal>16 MOD 6</literal> returns 4, which is the remainder after dividing 16 by 6.</paragraph>
    </listitem>
</list>
<note id="par_id921617302349290">Beware that Basic's <literal>MOD</literal> operator and Calc's <link href="text/scalc/01/04060106.xhp#bm_id3158247" name="MOD Function">MOD Function</link> behave differently. In Calc, both operands can be decimal values and they're not rounded before division, thus the resulting remainder may be a decimal value.</note>

<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<bascode>
<paragraph id="par_id3147560" role="bascode" xml-lang="en-US">Result = Expression1 MOD Expression2</paragraph>
</bascode>

<embed href="text/sbasic/shared/00000003.xhp#functvalue"/>
<paragraph id="par_id3153380" role="paragraph" xml-lang="en-US">Integer</paragraph>

<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph id="par_id3145172" role="paragraph" xml-lang="en-US"> <emph>Result:</emph> Any numeric variable that contains the result of the <literal>MOD</literal> operation.</paragraph>
<paragraph id="par_id3151042" role="paragraph" xml-lang="en-US"> <emph>Expression1, Expression2:</emph> Any numeric expressions for which you want to calculate the remainder after the division of <literal>Expression1</literal> by <literal>Expression2</literal>.</paragraph>

<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph id="par_idm1340853360" role="bascode" localize="false">Sub ExampleMod</paragraph>
<paragraph id="par_id3161832" role="bascode" localize="false">    Dim a As Double, b as Double</paragraph>
<paragraph id="par_id3150011" role="bascode" localize="false">    a = 10 : b = 4</paragraph>
<paragraph id="par_id3149483" role="bascode">    Print a Mod b 'Returns 2</paragraph>
<paragraph id="par_id3151114" role="bascode" localize="false">    a = 18 : b = 3.2</paragraph>
<paragraph id="par_id31494778" role="bascode">    Print a Mod b 'Returns 0</paragraph>
<paragraph id="par_id3146922" role="bascode" localize="false">    a = 16.4 : b = 5.9</paragraph>
<paragraph id="par_id3145273" role="bascode">    Print a Mod b 'Returns 4</paragraph>
<paragraph id="par_idm1340841712" role="bascode" localize="false">End Sub</paragraph>
</bascode>

<section id="relatedtopics">
  <paragraph role="paragraph" id="par_id771617305550403"><link href="text/scalc/01/04060106.xhp#bm_id3158247" name="Calc MOD Function">MOD Function</link></paragraph>
</section>
</body>
</helpdocument>