Thread: number bigger than long long double

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    number bigger than long long double

    Actually I'm doing a puzzle..
    where I need to find last 3 digits of the number before decimal.

    Number is [ 3 + sqrt(5) ] ^ P

    where P is taken as input.. This questions looks simple.
    but the problem here comes when the number becomes bigger than "long long double"

    for example .. take P = 28
    it showing -808 as output.. which is impossible.

    how should I deal with this kind of problem.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Get an arbitrary precision library like GMP: The GNU MP Bignum Library.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    (3+sqrt(5))^28 is approximately 1.35565e20, which is within the range of a float, let alone a long double (there is no long long double that I know of). However, you may not have all the decimal digits accurately, which is what you want.

    If all you need is the last three digits of the number before the decimal point, then that just means you should never store a number larger than 1000 (and if you go over 1000, then start subtracting 1000 until you aren't).

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    hmmm.. I compiled with P =28 by using long double..
    this is answer. "-2147483648.000000" negative.. which is wrong.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I think GMP lib is one possible way.. but can't I get off with C lib's..
    Last edited by suryak; 08-18-2011 at 01:13 PM. Reason: typo

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by suryak View Post
    hmmm.. I compiled with P =28 by using long double..
    this is answer. "-2147483648.000000" negative.. which is wrong.
    What compiler / OS are you using?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by suryak View Post
    I think GMP lib is one possible way.. but can't I get off with C lib's..
    Ok... out of curiosity did you try that as ... pow(3+sqrt(5.0),n)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're using Windows and MinGW, then you have to do some work for printing long double -- I think I've posted this code here before as a quick-and-dirty way to see the numbers:
    Code:
    char *printf_ld(long double arg, char answer[15]) {
    
        /* For my own sake: scientific notation with six digits */
        double mantissa, exponent, lg;
        lg = log10l(fabs(arg));
        exponent = floor(lg);
        mantissa = pow(10, lg-exponent);
        mantissa = copysign(mantissa, arg);
        sprintf(answer, "%.6fe%+04g", mantissa, exponent);
        return answer;
    Now that that's out of the way, you actually don't want to do this in the way you are doing -- if you ever keep a number bigger than 1000, you're doing it wrong.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > hmmm.. I compiled with P =28 by using long double..
    > this is answer. "-2147483648.000000" negative.. which is wrong.
    And where's your code?
    Frankly, your terse posts with snippets of information are getting tiresome.
    How do we know you didn't mess up the printf format, or anything else for that matter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Print your long doubles using printf and "%Lf" as a format specifier. You should also be using sqrtl and powl (instead of just sqrt and pow), for extra precision. You'll notice that even with long double, your answers are not exact, and may be off by tens of thousands. That's because of floating point inaccuracy, which is exacerbated by the algorithm used in powl(). Read this article for details: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

    Quote Originally Posted by suryak
    but can't I get off with C lib's..
    Not quite sure what you mean here. Could you not get it to work or are you not allowed to use 3rd party libraries? Maybe this is an assignment to illustrate the issues with floating point calculations...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  2. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  3. Long (really long) number
    By Doriän in forum C Programming
    Replies: 5
    Last Post: 09-10-2007, 03:27 PM
  4. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  5. long double help
    By nieds05 in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 05:18 PM

Tags for this Thread