How do pocket calculators simplify fractions and store inaccurate numbers as fractions?

Can anyone explain how calculators (eg casio pocket) manage equations like "500/12" and can return "125/3" as a result, in turn, someone can name some algorithms that do this ?

By imprecise numbers, I mean numbers that cannot be represented in a fixed number of decimal places, like 0.333, repeated.

The Windows Calculator is able to demonstrate this, if you do "1/3" you get the answer "0.3333333333333333", but if you multiply that by 3, you get back to "1".

+3


source to share


2 answers


My HP faction display allows you to set several faction display modes:

  • Set the highest denominator. Displayed fraction n/d

    closest to internal floating point value not exceeding d. For example, if the maximum value is 10, the floating point number for pi will be closer to a fraction 22/7

    . However, if the maximum is 1000, then the nearest fraction 355/113

    .

  • Set the exact denominator and decrease the result. The fraction displayed is the n/d

    one closest to the internal floating point value, where d is the exact denominator. After calculating n, the fraction is then reduced using the largest common denominator. For example, if the denominator is fixed at 32, then the floating point number 0.51 is closest to 16/32

    , which decreases to 1/2

    . Likewise, a floating point number of 0.516 is closest to 17/32

    that is irreducible.

  • Set the exact denominator and don't diminish the result. For example, 0.51 is shown as the 16/32

    unreduced fraction.

The maximum denominator approach algorithm uses continued beats . A simple example in Python can be found in the limit_denominator method at http://hg.python.org/cpython/file/2.7/Lib/fractions.py#l206 .



The exact denominator method is easier. Given the denominator d and the floating point number x, the numerator is simply d * x

rounded to the nearest integer. Then decrease the fraction n/d

by calculating the greatest common factor.

Optionally, the original floating point number can be replaced with the displayed fraction. This is called grid snapping. So you can enter 0.333 to create a fraction that is exactly equal 1/3

. This allows precise fractional arithmetic to be performed without rounding.

Hope this answer clears everything up for you :-) Let me know if any part needs development or further explanation.

+1


source


I would suggest you take a look at the GMP library of rational number functions . At some point you will need to accept finite precision in your calculations, unless the sequence of operations is simple. Irrational (transcendental functions / constants) can be approximated, for example, as continued fractions.



+1


source







All Articles