11 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.

73cat: MyArithmeticType 73  (58b)
define MyArithmeticType: Category == with {
        exports: MyArithmeticType 74
    default {
        defaults: MyArithmeticType 76b
    }
}

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.

74exports: MyArithmeticType 74  (73)  75
MyAdditiveType;

Uses MyAdditiveType 60.

Export of MyArithmeticType

1: %

Usage

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

Description

Returns the 1 constant of the type.

See also

one? 0 zero?

75exports: MyArithmeticType 74+   (73)  74  76a
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

76aexports: MyArithmeticType 74+   (73)  75  77
one?: % -> Boolean;
76bdefaults: MyArithmeticType 76b  (73)  81
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!

77exports: MyArithmeticType 74+   (73)  76a  78
*: (%, %) -> %;

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!

78exports: MyArithmeticType 74+   (73)  77  79
^: (%, 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

*

79exports: MyArithmeticType 74+   (73)  78  80
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

*

80exports: MyArithmeticType 74+   (73)  79
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.

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