Velocity Reviews > modulo % with non integers operands

# modulo % with non integers operands

ast
Guest
Posts: n/a

 02-21-2011
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 ?

Luuk
Guest
Posts: n/a

 02-21-2011
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

ast
Guest
Posts: n/a

 02-21-2011

"ast" <(E-Mail Removed)> a écrit dans le message de news:4d62707b\$0\$18569\$(E-Mail Removed)...
> 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
Guest
Posts: n/a

 02-21-2011

"Luuk" <(E-Mail Removed)> a écrit dans le message de news:(E-Mail Removed)...
> 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

John G Harris
Guest
Posts: n/a

 02-21-2011
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

Evertjan.
Guest
Posts: n/a

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

>
> "ast" <(E-Mail Removed)> a écrit dans le message de
> news:4d62707b\$0\$18569\$(E-Mail Removed)...
>> 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;
};

=============================

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.

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 02-21-2011
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.)

Dr J R Stockton
Guest
Posts: n/a

 02-22-2011
In comp.lang.javascript message <Xns9E93A7673186Beejj99@194.109.133.242>
, Mon, 21 Feb 2011 15:27:21, Evertjan. <(E-Mail Removed)>
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,

Evertjan.
Guest
Posts: n/a

 02-23-2011
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. <(E-Mail Removed)>
> 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.