CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Posts
    22

    VeryLong Fractions

    Hi,

    I think, I invented the wheel the thousandst time by writinig a very long integer-Library.

    Now I wonder how to work with divisions. Of course you can divide with integers and look at the rest.
    It is also easy to think about a fraction as a set ot two integers, implementing the known rules for adding, multiplying, ... them.
    An other approach is to work with very long integers and remember the position of the comma.
    Disadvantage of this is, that you cannot divide by 3 exactly. I do not know how to proceed.

    Is there any description of a library working with very long fractions or very long decimal fractions?

    A very interesting question is, how to implement the rounding in the different models.
    Continued fraction maybe?

    Whatever: I am shure that somebody thought about these problems, but with Google i need some keywords to search for. Any hints?


    Best Regards
    GMarco

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: VeryLong Fractions

    Look at existing bignum libraries, e.g., GMP.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2011
    Posts
    22

    Re: VeryLong Fractions

    Quote Originally Posted by laserlight View Post
    Look at existing bignum libraries, e.g., GMP.
    Hi Laserlight,

    i looked at GMP-reference and Java.BigInteger, nice methods provided (like next Prime...), but nothing about fractions.

    With fractions I mean some strutcure like
    x=(BigInteger p, BigInteger q);
    x1*x2=(p1*p2,q1+q2),
    x1+x2=(p1*q2+p2*q1,q1*q2),
    ...

    I am especcially interested in fast algorithms for approximate fractions of given fractions.


    GMarco

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: VeryLong Fractions

    Whenever ordinary hardware floating point won't do you need to carefully consider why not?

    Is it really inherent to the problem domain? If not it may be possible to do calculations differently for example by algebraic manipulations of formulas and/or adequate approximations.

    My point is that maybe it's possible to stay withing hardware supported arithmetics after all.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: VeryLong Fractions

    if you're using C++, take a look at the boost rational library. It's specifically designed to be used with an underlying integer type that behaves at least as the usual C integer types with respect to operations complexities and range representability. Indeed, it can be used in conjunction with any C++ big integer library to represent arbitrary rational numbers.

    anyway, as nuzzle pointed out, are you sure this really meets your requirements ? for example, if your aim is to represent real numbers with arbitrary precision then it's certainly a bad choice, in general. Just consider that in an arbitrary vicinity of each rational number representable by n-bits there are infinitely many rational numbers that require strictly more than n-bits in order to be represented, for every n. In other words, given a fixed range underlying integer type, you can overflow any such rational via an arbitrarily small step ...

    Unless there are specific reasons to not do so ( note that there are infinitely many ways of finitely representing dense subsets of the reals ... ), I think you should stick to floating point numbers or, if you need more accuracy, to some sort of decimal representation with prescribed rounding semantics.
    Last edited by superbonzo; July 31st, 2012 at 09:01 AM.

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: VeryLong Fractions

    Quote Originally Posted by GMarco View Post
    Hi Laserlight,

    i looked at GMP-reference and Java.BigInteger, nice methods provided (like next Prime...), but nothing about fractions.
    The GMP page explicitly states that it provides for rational numbers of arbitray precision. See http://gmplib.org/
    Quote Originally Posted by GMP Page
    GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers.
    Here is the link to the GMP manual for describing the functions available for rational numbers: "Rational Number Functions" at http://gmplib.org/manual/Rational-Number-Functions.html

    Mike

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured