Results 1 to 3 of 3

Thread: Faking fixed-point arithmetic

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member Johannes's Avatar
    Join Date
    Nov 2010
    Location
    Wuustwezel, Belgium
    Age
    56
    Posts
    95
    Rep Power
    25

    Faking fixed-point arithmetic

    Even though this is a big integer module, you can fake fixed-point arithmetic.

    What you do is multiply all numbers with the base you're working in raised to the power of the number of digits you want to have after the radix point. For example, if I want to work in radix 10 with three digits after the radix point, my fixed-point factor is 10^3 which is 1000.


    There are some pitfalls, and off the top of my head they are as follows.
    • Changing the radix while preserving any Big Integer would be... unadvisable. In the above example the number 1.000 (which is stored as 1000) would become 3E8 in hexadecimal. Those three decimals are gone and what remains has no connection to 1.000 or 1000.
    • The output format should have separators disabled (BigInt_SetInterval(0)) so that it's easy to insert a radix character for printing. Nope, programmed around that. Separate format settings for regular BigInt and fixed-point.
    • Multiplying two Big Integers will double the precision, so after each multiplication a division (with optional rounding) by the fixed-point factor should be performed.
    • Dividing two Big Integers removes the precision, so before each division a multiplication by the fixed-point factor should be performed.
    • The same goes for square root, and for cube root a multiplication by the fixed-point factor squared should be performed. In both cases before the actual calculation.
    • When raising a number to a power the result should be divided by fixed_point_factor^(power - 1), again with optional rounding, after calculation. (The power itself should not be "converted" to "fixed-point".) Disregard that last bit between parentheses. The power must also be a fixed-point value but the fraction is ignored.
    • When calculating the nth root the number should be multiplied by fixed_point_factor^(power - 1) before the calculation. (The power itself should not be "converted" to "fixed-point".) Same for the last bit between parentheses as directly above.
    • Even/odd checking, Fibonacci, factorial, permutations and combinations are meaningless in this situation. As is anything dealing with primes.
    • BigInt_Inc and BigInc_Dec work at the lowest level, so they become an increase or decrease by 1/fixed_point_factor.
    You'll be pleased to know that adding and subtracting works just fine.

    Also, as long as you don't change the radix during your calculations, you can work in any radix you like. So it's possible to work in radix three with five digits after the radix point. The number 21.20121 would be stored internally as the (decimal) integer 1719 while it means 7.732510288065843621399176954 (the underlined digits repeat forever) which is 2*3+1+2/3+0/9+1/27+2/81+1/243. The fixed-point factor would be 3^5 which is 243.

    If there is a big interest in fixed-point arithmetic in combination with Big Integers, and nobody understands what I'm describing above , I'll whip up an include script.

    When I get back from my vacation, at the end of this month.
    Last edited by Johannes; 12-04-2011 at 16:44. Reason: Post-include script (see below) comments in red.
    Boole and Turing, help me!

    Primary programming: 200 MHz ARM StrongARM, RISC OS 4.02, BASIC V, ARM assembler.
    Secondary programming: 3.16 GHz Intel Core 2 Duo E8500, Vista Home Premium SP2, thinBasic, x86 assembler.

Similar Threads

  1. Arithmetic on the FPU: (vv1+vv2) / (vv3+vv4)
    By Charles Pegge in forum Machine Code: script sources and examples
    Replies: 3
    Last Post: 10-03-2008, 22:43

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •