21 MyArithmeticType

Type Constructor

MyArithmeticType

Usage

define MyRing: Category == with {MyArithmeticType, ...}
MyFloat: Join(MyArithmeticType, ...) with {...} == add {...}

Description

The category of types with addition, subtraction, and multiplication operations.

MyArithmeticType is the category of types that provide operations that can be performed in a ring.

Remarks

MyArithmeticType provides only the signatures of ring operations. There is no statement made about which axioms these operations have to fulfil. MyAdditiveType and MyArithmeticType are useful to provide a ring signatures to domains like Float, DoubleFloat, and similar types that in many respect behave like rings but are by their imprecise nature not mathematical rings themselves.

174cat: MyArithmeticType 174  (159b)
define MyArithmeticType: Category == with {
        exports: MyArithmeticType 175
    default {
        defaults: MyArithmeticType 177b
    }
}

Defines:
MyArithmeticType, never used.

Exports of MyArithmeticType

MyAdditiveType;

1: % Returns the 1 constant of the type.

one?: % -> Boolean Tests whether its argument is 1.

*: (%, %) -> % Returns the product of its arguments.

^: (%, MachineInteger) -> % Raises its first argument to the power given by the second argument.

commutative?: Boolean Returns true if * is commutative.

times!: (%, %) -> % Returns the product of its arguments.

Of course, an (additive) group-like structure is provided.

175exports: MyArithmeticType 175  (174)  176
MyAdditiveType;

Uses MyAdditiveType 161.

Export of MyArithmeticType

1: %

Usage

if x = 1 then ...
x := 1

Description

Returns the 1 constant of the type.

See also

one? 0 zero?

176exports: MyArithmeticType 175+   (174)  175  177a
1: %;

Export of MyArithmeticType

one?: % -> Boolean

Usage

if one? x then ...

Parameters

x: %

Element to be tested.

Description

Tests whether its argument is 1.

Remarks

Note that one would usually not use this function on floating point types. A test for 1 might fail because of small errors introduced during a computation.

See also

1

177aexports: MyArithmeticType 175+   (174)  176  178
one?: % -> Boolean;
177bdefaults: MyArithmeticType 177b  (174)  182
one?(x: %): Boolean == x = 1;

Export of MyArithmeticType

*: (%, %) -> %

Usage

z := x * y

Parameters

x: %

y: %

Elements to be multiplied.

Description

Returns the product of its arguments.

See also

add! minus!

178exports: MyArithmeticType 175+   (174)  177a  179
*: (%, %) -> %;

Export of MyArithmeticType

^: (%, MachineInteger) -> %

Usage

z := x ^ n

Parameters

x: %

Element to be exponentiated.

n: MachineInteger

Exponent.

Description

Raises its first argument to the power given by the second argument.

See also

* times!

179exports: MyArithmeticType 175+   (174)  178  180
^: (%, MachineInteger) -> %;

Export of MyArithmeticType

commutative?: Boolean

Usage

if commutative? then {
  -- case where the multiplication is known to be commutative
} else {
  -- it is not known whether the multiplication is commutative
}

Description

Returns true if * is commutative.

See also

*

180exports: MyArithmeticType 175+   (174)  179  181
commutative?: Boolean;

Export of MyArithmeticType

times!: (%, %) -> %

Usage

z := times!(y, z);
z := 1;
for x in somelist repeat z := times!(z, x);

Parameters

x: %

y: %

z: %

Elements to be multiplied.

Description

Returns the product of its arguments.

Remarks

See add! and note that there are now two constants, namely 0 and 1.

See also

*

181exports: MyArithmeticType 175+   (174)  180
times!: (%, %) -> %;

In the implementation we make sure that further destructive changes on the result of times! have no influence on the second argument. Unfortunately, one can only do this if the type is know to be copyable. The default implementation of times! is nearly identical to the one of add!. Also the remarks made there apply here accordingly.

182defaults: MyArithmeticType 177b+   (174)  177b
local copy?: Boolean == % has CopyableType;
times!(x: %, y: %): % == {
        one? x => {
                copy? => copy(y) $ (% pretend CopyableType);
                y
        }
        x * y;
}