exponent and power problems, please help

This is a discussion on exponent and power problems, please help within the C++ Programming forums, part of the General Programming Boards category; I am writing code that deals with very small numbers. I am having problems with these small numbers getting set ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Angry exponent and power problems, please help

    I am writing code that deals with very small numbers. I am having problems with these small numbers getting set to zero. I am using gcc in RedHat 9. For example, if I declare x to be a long double, and I then say

    x = pow(10, -400)

    I get that my program thinks x is 0. The magic exponent at which x is set to zero appears to be -325. Additionally, if I say
    x = pow(10,-324)

    I get x to be equal to about 9.88E-324. I have roughly the same problem with the exp() function. I've tried setting LDBL_MIN_EXP and LDBL_MIN_10_EXP, but it doesn seem to work. Does anyone know what is going on? What am I doing wrong, or what am I not doing? Do I have to recompile something (besides my program) after I set LDBL_MIN_EXP or LDBL_MIN_10_EXP?

    Thanks for your time,
    Andreas

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The size of a double is limiting the precision you can attain. You may just have to accept that it is not possible to store a value that low.

    However, what you could do, is create your own class/structure that stores scientific notation:
    Code:
    class Number
    {
    public:
        double num1; //first part: 3.45
        double num2; //second part: -400
    
        //and a bunch of custom operators
    };
    This would equate to 3.45*10^-400. This may be a possible way to store really small or really large numbers. You would have to create a few operators to deal with this abnormal numerical type, but it shouldn't be too hard.

    If you were to indicate the purpose of doing this, perhaps a better solution could be found.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > I've tried setting LDBL_MIN_EXP and LDBL_MIN_10_EXP
    They're read-only, you can't change them (unless you're actually writing the run-time support yourself)

    Either do as Benny suggests and write your own custom class, or use an existing package like http://www.swox.com/gmp/
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Unhappy RH 9 gcc -- exponentially small numbers getting set to zero

    Thanks bennyandthejets and Salem,
    Since bennyandthejets asked, I am trying to numerically integrate a function that involves a sensitive exponential. The exponential function looks like

    exp(-a/(T(1-T^4)^2)

    Here 'a' is a relatively large number and 'T' varies from 0 to 1. If you are curious, I am looking at how the universe goes through a first-order phase transition. The exponential describes how many bubbles (of protons and neutrons) form in the universe as it cools. The 'T' in the above equation is a dimensionless temperature. These bubbles then grow as the universe evolves. So, what started out as an extremely small bubble can grow to macroscopic sizes. Therefore, I need to keep exponentially small numbers.

    I've looked briefly at what you both suggest. I'm not sure which one will be best for me at this point. I'll give try one and then get back to you.

    Thanks again for your time.

    Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pow(x, y); problems
    By rhouli67 in forum C Programming
    Replies: 14
    Last Post: 02-17-2009, 09:51 AM
  2. help calcualting a floar power
    By mc_line in forum C Programming
    Replies: 2
    Last Post: 02-04-2008, 05:21 PM
  3. Floating-Point Numbers
    By MindLess in forum C Programming
    Replies: 4
    Last Post: 06-24-2007, 11:45 PM
  4. Integer power
    By Sure in forum C Programming
    Replies: 3
    Last Post: 06-10-2005, 03:43 PM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21