modulo % with non integers operands

Discussion in 'Javascript' started by ast, Feb 21, 2011.

  1. ast

    ast Guest

    Hi

    when a and b are both integers, a % b is the reminder r of the
    division of a by b. (a = bq+r, 0 <= r < b)

    but % works with non integers data too, ie
    5.6 % 3.21 = 2.39

    Does anyone know what % does in this situation ?
     
    ast, Feb 21, 2011
    #1
    1. Advertising

  2. ast

    Luuk Guest

    On 21-02-11 15:02, ast wrote:
    > Hi
    >
    > when a and b are both integers, a % b is the reminder r of the
    > division of a by b. (a = bq+r, 0 <= r < b)
    >
    > but % works with non integers data too, ie
    > 5.6 % 3.21 = 2.39
    >
    > Does anyone know what % does in this situation ?
    >
    >
    >
    >


    5.6 - 3.21 = 2.39

    so, its the remainder after dividing by an integer...

    i'll bett 10 % 3.21 = 0.37
    because 10 - (3* 3.21) = 0.37
    (and i did not check....)

    --
    Luuk
     
    Luuk, Feb 21, 2011
    #2
    1. Advertising

  3. ast

    ast Guest

    "ast" <> a écrit dans le message de news:4d62707b$0$18569$...
    > Hi
    >
    > when a and b are both integers, a % b is the reminder r of the
    > division of a by b. (a = bq+r, 0 <= r < b)
    >
    > but % works with non integers data too, ie
    > 5.6 % 3.21 = 2.39
    >
    > Does anyone know what % does in this situation ?
    >


    i just realize that 5.6 = 3.21 + 2.39
    strange
     
    ast, Feb 21, 2011
    #3
  4. ast

    ast Guest

    "Luuk" <> a écrit dans le message de news:...
    > On 21-02-11 15:02, ast wrote:
    >> Hi
    >>
    >> when a and b are both integers, a % b is the reminder r of the
    >> division of a by b. (a = bq+r, 0 <= r < b)
    >>
    >> but % works with non integers data too, ie
    >> 5.6 % 3.21 = 2.39
    >>
    >> Does anyone know what % does in this situation ?
    >>
    >>
    >>
    >>

    >
    > 5.6 - 3.21 = 2.39
    >
    > so, its the remainder after dividing by an integer...
    >
    > i'll bett 10 % 3.21 = 0.37
    > because 10 - (3* 3.21) = 0.37
    > (and i did not check....)


    that's correct
     
    ast, Feb 21, 2011
    #4
  5. On Mon, 21 Feb 2011 at 15:02:32, in comp.lang.javascript, ast wrote:
    >Hi
    >
    >when a and b are both integers, a % b is the reminder r of the
    >division of a by b. (a = bq+r, 0 <= r < b)


    Remember, though, that sometimes you want a different rule, e.g:
    11 + 1 = 12 o'clock
    11 + 2 = 1 o'clock


    >but % works with non integers data too, ie
    >5.6 % 3.21 = 2.39
    >
    >Does anyone know what % does in this situation ?


    Ideally, your browser does what it says in the ECMAScript standard, but
    programmers sometimes get it wrong when handling negative numbers (as
    you did when writing the constraint).

    John
    --
    John Harris
     
    John G Harris, Feb 21, 2011
    #5
  6. ast

    Evertjan. Guest

    ast wrote on 21 feb 2011 in comp.lang.javascript:

    >
    > "ast" <> a écrit dans le message de
    > news:4d62707b$0$18569$...
    >> Hi
    >>
    >> when a and b are both integers, a % b is the reminder r of the
    >> division of a by b. (a = bq+r, 0 <= r < b)
    >>
    >> but % works with non integers data too, ie
    >> 5.6 % 3.21 = 2.39
    >>
    >> Does anyone know what % does in this situation ?
    >>

    >
    > i just realize that 5.6 = 3.21 + 2.39
    > strange


    Not strange at all.

    What is modulo doing?

    ===============================
    function modulo(a,b) {
    while (a > b) a = b - a;
    return a;
    };

    alert(modulo(5.6, 3.21));
    =============================

    So as long as the substraction is only executed once,
    a = returnedValue + b

    Mind that the returned value is not exactly 2.39,
    due to the root-2 math conversion used in Javascript.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 21, 2011
    #6
  7. ast wrote:

    > when a and b are both integers, a % b is the reminder r of the
    > division of a by b. (a = bq+r, 0 <= r < b)
    >
    > but % works with non integers data too, ie
    > 5.6 % 3.21 = 2.39
    >
    > Does anyone know what % does in this situation ?


    Yes. (That is a stupid question!¹)

    ,-[ECMAScript Language Specification, Edition 5 Final²]
    |
    | 11.5.3 Applying the % Operator
    |
    | The % operator yields the remainder of its operands from an implied
    | division; the left operand is the dividend and the right operand is
    | the divisor.
    |
    | […]
    | The result of an ECMAScript floating-point remainder operation is
    | determined by the rules of IEEE arithmetic:
    |
    | • If either operand is NaN, the result is NaN.
    | • The sign of the result equals the sign of the dividend.
    | • If the dividend is an infinity, or the divisor is a zero, or both,
    | the result is NaN.
    | • If the dividend is finite and the divisor is an infinity,
    | the result equals the dividend.
    | • If the dividend is a zero and the divisor is finite, the result is
    | the same as the dividend.
    | • In the remaining cases, where neither an infinity, nor a zero, nor
    | NaN is involved, the floating-point remainder r from a dividend n
    | and a divisor d is defined by the mathematical relation
    | r = n − (d * q) where q is an integer that is negative only if n/d
    | is negative and positive only if n/d is positive, and whose
    | magnitude is as large as possible without exceeding the magnitude
    | of the true mathematical quotient of n and d.


    HTH

    PointedEars
    ___________
    ² <http://www.catb.org/~esr/faqs/smart-questions.html>
    ¹ <http://www.ecma-international.org/publications/files/
    ECMA-ST/ECMA-262.pdf>
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
     
    Thomas 'PointedEars' Lahn, Feb 21, 2011
    #7
  8. In comp.lang.javascript message <Xns9E93A7673186Beejj99@194.109.133.242>
    , Mon, 21 Feb 2011 15:27:21, Evertjan. <>
    posted:

    >Mind that the returned value is not exactly 2.39,
    >due to the root-2 math conversion used in Javascript.


    FYI - root-2 and base-2 are not the same thing.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 8.
    Web <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
    Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
    DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.
     
    Dr J R Stockton, Feb 22, 2011
    #8
  9. ast

    Evertjan. Guest

    Dr J R Stockton wrote on 22 feb 2011 in comp.lang.javascript:

    > In comp.lang.javascript message <Xns9E93A7673186Beejj99@194.109.133.242>
    > , Mon, 21 Feb 2011 15:27:21, Evertjan. <>
    > posted:
    >
    >>Mind that the returned value is not exactly 2.39,
    >>due to the root-2 math conversion used in Javascript.

    >
    > FYI - root-2 and base-2 are not the same thing.


    John, you are always good for the rootics.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 23, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Kruggel

    General modulo-question

    Christian Kruggel, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    469
    Brad BARCLAY
    Jul 7, 2003
  2. Replies:
    1
    Views:
    680
    Darryl L. Pierce
    May 20, 2004
  3. Tjerk Wolterink

    [xsl] sort & modulo

    Tjerk Wolterink, Apr 21, 2005, in forum: XML
    Replies:
    3
    Views:
    584
    Dimitre Novatchev
    Apr 22, 2005
  4. silentlights

    Fast Division/Modulo Operation

    silentlights, Apr 16, 2004, in forum: C Programming
    Replies:
    8
    Views:
    1,007
    Dik T. Winter
    Apr 23, 2004
  5. Murph
    Replies:
    4
    Views:
    544
    Ben Jones
    Dec 6, 2006
Loading...

Share This Page