Sunday, 29 November 2026

Fractional Data Type for Precise Calculations.

Errors accumulation, floating point numbers, and precise fractions.

When we perform even simple calculations on floating point numbers, we often see error accumulation. This can be really bad when we calculate partial results in a loop.

We can, however, store numbers as fractions - as a numerator and a denominator. Such numbers are precise, and we can divide numerator by denominator only once, at the end of our calculation. That way we can have a floating point result, with only minimal error.

This is not replacement for 'Numerical Methods' course, it's just a simple way to make simple calculations precise in easy, quick, and inexpensive way. It can be combined with 'Numerical Methods' still, I think.

We can use a very long integer data type, as for example: BigInteger class in java, or int type in python too.


Expression tree.

When we have an expression string, consisting of parentheses, numbers, and 5 basic operations, we can parse it and build expression tree. Then we may calculate it from bottom to top, from leaves to root.

These 5 basic operations are:
- Addition,
- Subtraction,
- Multiplication,
- Division,
- Power.

Expression trees can also be constructed using other ways, including calling constructors to create tree node objects ( our tree's node can be either number for leaf node, or operation for non-leaf node ), and linking these with setter accessor methods.


Addition and Subtraction.

First we find common denominator by multiplying denominators of our operation, multiply nominators by 'opposite' denominators, then add or subtract resulting nominators.

Then we 'reduce' resulting fraction so it uses least possible denominator.


'Reduction' Algirithm:

We repeat:
1. We perform factorization of both numerator and denominator, to get list of prime factors in each,
2. If we have same prime factor(s) in numerator and denominator, we remove these from both lists until our factor lists are consisting of unique integers,
3. If/when the lists of prime factors have only unique integers, we end our loop.


Multiplication.

We multiply numerator by numerator, and denominator by denominator. Then we 'reduce', as explained above.


Division.

We switch places of numerator and denominator in second fraction of our division (written on the right), then multiply, as explained above.


Power.

We rise both numerator and denominator to power of n, where n is integer number, then we 'reduce' result, as explained above.


Next Challenge.

Next challenge is finding a way to calculate roots of fraction, where the result is also a fraction.

For example: finding a power of 2/3 of a fraction number fn, where result is written as precise fraction.


Java Library Code on GitHub.

/ PLANNED TO BE DONE, PLEASE DO NOT HURRY ME /.
/ BUILDING EXPRESSION TREE IS TO BE INCLUDED, BUT WITHOUT PARSER FOR NOW /.

/ DO NOT WORRY, I CAN HANDLE WRITING PARSER(S) ... I'VE DONE THIS BEFORE /.


'Ola' Programming Language.

We plan to use both unbounded integer numbers and fractional numbers as built-in data types in 'Ola' Programming Language we intend to create.


A Fraggle.
-=- A 'MULP' Fraggle (Wembley). -=-
-=- Fractional Data Type Mascot. -=-

('MULP' is mnemonic for: 'Multiply with Precision').

No comments:

Post a Comment