modulo % with non integers operands

A

ast

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 ?
 
L

Luuk

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....)
 
A

ast

ast said:
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
 
A

ast

Luuk said:
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
 
J

John G Harris

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
 
E

Evertjan.

ast wrote on 21 feb 2011 in comp.lang.javascript:
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.
 
T

Thomas 'PointedEars' Lahn

ast said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top