core/Fraction.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
2cb979342e005037c4d13812ffbb6b24b48259cb- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// The canonical Fraction family prototype (D197-C). Exact rational arithmetic
// and negation are ordinary source closures over Integer arithmetic. They read
// canonical components and request the canonical result only through the
// private bootstrap kernel below, which the runtime fills with exactly
// numerator, denominator, normalize and toFloat; the kernel never owns
// arithmetic. Only the exact recognizer is native, because it must not
// dispatch on the candidate. A Float operand is combined after rounding the
// whole exact quotient once to binary64 (D196-B1, D197).
Fraction: Number {
negated: () => {
Fraction.recognizes(this).ifFalse(() => {
Error().signal()
})
_coreFractionKernel.normalize(
0 - _coreFractionKernel.numerator(this),
_coreFractionKernel.denominator(this))
}
// Installed by the runtime as `+`; a slot target cannot spell an operator.
_coreFractionPlus: (other) => {
Fraction.recognizes(this).ifFalse(() => {
Error().signal()
})
if(Float.recognizes(other)) {
_coreFractionKernel.toFloat(this) + other
} else {
if(Complex.recognizes(other)) {
_coreComplexKernel.plus(this, other)
} else {
a: _coreFractionKernel.numerator(this)
b: _coreFractionKernel.denominator(this)
c: _coreFractionKernel.numerator(other)
d: _coreFractionKernel.denominator(other)
_coreFractionKernel.normalize((a * d) + (c * b), b * d)
}
}
}
// Installed by the runtime as `-`.
_coreFractionMinus: (other) => {
Fraction.recognizes(this).ifFalse(() => {
Error().signal()
})
if(Float.recognizes(other)) {
_coreFractionKernel.toFloat(this) - other
} else {
if(Complex.recognizes(other)) {
_coreComplexKernel.minus(this, other)
} else {
a: _coreFractionKernel.numerator(this)
b: _coreFractionKernel.denominator(this)
c: _coreFractionKernel.numerator(other)
d: _coreFractionKernel.denominator(other)
_coreFractionKernel.normalize((a * d) - (c * b), b * d)
}
}
}
// Installed by the runtime as `*`.
_coreFractionTimes: (other) => {
Fraction.recognizes(this).ifFalse(() => {
Error().signal()
})
if(Float.recognizes(other)) {
_coreFractionKernel.toFloat(this) * other
} else {
if(Complex.recognizes(other)) {
_coreComplexKernel.times(this, other)
} else {
a: _coreFractionKernel.numerator(this)
b: _coreFractionKernel.denominator(this)
c: _coreFractionKernel.numerator(other)
d: _coreFractionKernel.denominator(other)
_coreFractionKernel.normalize(a * c, b * d)
}
}
}
// Installed by the runtime as `/`. An exact zero divisor signals the
// standard Error from normalize.
_coreFractionDivide: (other) => {
Fraction.recognizes(this).ifFalse(() => {
Error().signal()
})
if(Float.recognizes(other)) {
_coreFractionKernel.toFloat(this) / other
} else {
if(Complex.recognizes(other)) {
_coreComplexKernel.divide(this, other)
} else {
a: _coreFractionKernel.numerator(this)
b: _coreFractionKernel.denominator(this)
c: _coreFractionKernel.numerator(other)
d: _coreFractionKernel.denominator(other)
_coreFractionKernel.normalize(a * d, b * c)
}
}
}
}
_coreFractionKernel: {}