"""fp-test.py -- fixedpoint versus float comparison"""

# You need the fixedpoint.py module installed
#    http://fixedpoint.sourceforge.net

from fixedpoint import FixedPoint

print "PCT    AMT    Fixed    Float"

# I'm checking percentages, from 1% to 10% inclusive,
# multiplied by "money" amounts from 0.01 (one cent)
# to 1.00 (a buck).

# Given appropriate precision, this problem applies
# to other monetary systems than just USA.

for percent in range(1,11):

    fixed_pct = FixedPoint(percent)
    fixed_pct /= 100

    float_pct = percent / 100.0

    for amount in range(1,101):

        fixed_amt = FixedPoint(amount)
        fixed_amt /= 100
    
        float_amt = amount / 100.0

        ##############################################################
        # Now for the crux of the matter:

        fixed_res = fixed_amt * fixed_pct  # rounding is implicit here
        float_res = round(float_amt * float_pct, 2)

        ##############################################################
        # That's it, let's see if they match:

        if str(fixed_res) != ("%0.2f" % float_res):
            print "0.%02d   0.%02d   %-6s %6.2f" % \
                (percent, amount, fixed_res, float_res)
 
# end of file.
