# Number protocol

### From Erights

Kevin Reid (Talk | contribs) |
(add modPow, missing space for max and min, fix XXXes, mention other-number-type issue) |
||

Line 3: | Line 3: | ||

This protocol is implemented by [[Integer]] and [[Float64]]. | This protocol is implemented by [[Integer]] and [[Float64]]. | ||

- | {{XXX | + | {{XXX|This list is not complete, not properly documented, and not discussed.}} |

+ | |||

+ | {{XXX|Consider whether this page could be turned into a template which generates tests of the number protocol for specific number types.}} | ||

+ | |||

+ | {{XXX|This documentation needs to acknowledge behavior with respect to numbers of other types; e.g. where this is an integer and an argument is a float. Just saying type T doesn't acknowlege that the result might be of another type.}} | ||

==Protocol== | ==Protocol== | ||

Line 26: | Line 30: | ||

Sugar expansion of [[operator /|/]]. {{XXX}} write documentation | Sugar expansion of [[operator /|/]]. {{XXX}} write documentation | ||

+ | |||

+ | {{instance msgdoc|remainder|1|<var>other</var> :T|T}} | ||

+ | |||

+ | Sugar expansion of [[operator %|%]]. {{XXX}} write documentation | ||

+ | |||

+ | {{instance msgdoc|mod|1|<var>other</var> :T|T}} | ||

+ | |||

+ | Sugar expansion of [[operator %%|%%]]. {{XXX}} write documentation | ||

+ | |||

+ | {{instance msgdoc|pow|1|<var>other</var> :T|T}} | ||

+ | |||

+ | Sugar expansion of [[operator **|**]]. {{XXX}} write documentation | ||

+ | |||

+ | {{instance msgdoc|modPow|2|<var>exponent</var> :T, <var>modulus</var> :T|T}} | ||

+ | |||

+ | Sugar expansion of <code>this ** exponent %% modulus</code>. | ||

+ | |||

+ | {{specbycode|<var>this</var>.pow(<var>exponent</var>).mod(<var>modulus</var>)}} | ||

+ | |||

+ | It is expected that when this is an integer, <var>modulus</var> is a nonzero integer and <var>exponent</var> is a positive integer, this operation will be [http://en.wikipedia.org/wiki/Modular_exponentiation implemented efficiently]. | ||

+ | |||

+ | {{XXX|In [[E-on-Java]], a negative modulus will throw an exception rather than computing the compositional answer. The above spec I just wrote contradicts this. Resolve. --[[User:Kevin Reid|Kevin Reid]] 23:51, 30 July 2009 (CDT)}} [[Category:Unresolved design issues]] | ||

{{instance msgdoc|negate|0||T}} | {{instance msgdoc|negate|0||T}} | ||

Line 36: | Line 62: | ||

{{instance msgdoc|max|1|<var>other</var> :T|T}} | {{instance msgdoc|max|1|<var>other</var> :T|T}} | ||

+ | |||

Compare the recipient to <var>other</var>, and return whichever is greater. | Compare the recipient to <var>other</var>, and return whichever is greater. | ||

- | {{instance msgdoc| | + | {{instance msgdoc|min|1|<var>other</var> :T|T}} |

+ | |||

Compare the recipient to <var>other</var>, and return whichever is lesser. | Compare the recipient to <var>other</var>, and return whichever is lesser. | ||

{{stub}} | {{stub}} |

## Latest revision as of 04:51, 31 July 2009

For some type T which is like a number, these are the conventional messages to implement.

This protocol is implemented by Integer and Float64.

XXX This list is not complete, not properly documented, and not discussed.

XXX Consider whether this page could be turned into a template which generates tests of the number protocol for specific number types.

XXX This documentation needs to acknowledge behavior with respect to numbers of other types; e.g. where this is an integer and an argument is a float. Just saying type T doesn't acknowlege that the result might be of another type.

## Contents |

## Protocol

### add/1

*Signature:*add(`other`:T) :T

Sugar expansion of +. XXX write documentation

### subtract/1

*Signature:*subtract(`other`:T) :T

Sugar expansion of -. XXX write documentation

### multiply/1

*Signature:*multiply(`other`:T) :T

Sugar expansion of *. XXX write documentation

### floorDivide/1

*Signature:*floorDivide(`other`:T) :T

Sugar expansion of //. XXX write documentation

### approxDivide/1

*Signature:*approxDivide(`other`:T) :T

Sugar expansion of /. XXX write documentation

### remainder/1

*Signature:*remainder(`other`:T) :T

Sugar expansion of %. XXX write documentation

### mod/1

*Signature:*mod(`other`:T) :T

Sugar expansion of %%. XXX write documentation

### pow/1

*Signature:*pow(`other`:T) :T

Sugar expansion of **. XXX write documentation

### modPow/2

*Signature:*modPow(`exponent`:T,`modulus`:T) :T

Sugar expansion of `this ** exponent %% modulus`

.

Equivalent to

.
`this`.pow(`exponent`).mod(`modulus`)

It is expected that when this is an integer, `modulus` is a nonzero integer and `exponent` is a positive integer, this operation will be implemented efficiently.

XXX In E-on-Java, a negative modulus will throw an exception rather than computing the compositional answer. The above spec I just wrote contradicts this. Resolve. --Kevin Reid 23:51, 30 July 2009 (CDT)

### negate/0

*Signature:*negate() :T

Sugar expansion of -. Return the additive inverse of the receiver.

### abs/0

*Signature:*abs() :T

Return the absolute value of the receiver.

### max/1

*Signature:*max(`other`:T) :T

Compare the recipient to `other`, and return whichever is greater.

### min/1

*Signature:*min(`other`:T) :T

Compare the recipient to `other`, and return whichever is lesser.

*This page is a stub; it should be expanded with more information. If doing so, check the original E web site and the mailing list archives for content which could be moved into this page.*