variables larger than floats

This is a discussion on variables larger than floats within the C Programming forums, part of the General Programming Boards category; So I am having trouble with numbers bigger than floats. A few questions. Can the variable type Code: long long ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    variables larger than floats

    So I am having trouble with numbers bigger than floats.

    A few questions. Can the variable type

    Code:
    long long
    hold decimal places. I think it doesnt.
    I am running an iterative calculation in a for loop that runs 100 times. The first few answers are small enough for a float to hold - need up to 8 decimal places. But then they become to big for floats, so I wanted to hold them all in long long type variable. But it doesnt seem to store decimals.

    Please can someone tell me a veriable type, that is very large, but can also hold decimals. And what the code is when printing or scanning for one.

    i.e printf("%d", int);

    the %d is the "code for integers".

    Thanks

    Alex

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    I tend to default to double, but there is also long double. What are your exact requirements, including precision and accuracy?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Floating point numbers aren't really that great for high precision. You'd be better off finding a way to convert over to integers, since there is no loss of precision there. Floating point numbers suffer from precision loss due to the way they're stored.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Basically for smaller programs I always used floats because they could hold decimals. I had no idea of a precision or accuracy. Now that the actual size of the numbers are too large, I need something like a float, that can hold a few decimal places - forget 8 if thats too many, but can also hold the number size.

    I have tried long long, but i dont think it can hold decimals.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    Turkey
    Posts
    12
    Quote Originally Posted by a.mlw.walker View Post
    Basically for smaller programs I always used floats because they could hold decimals. I had no idea of a precision or accuracy. Now that the actual size of the numbers are too large, I need something like a float, that can hold a few decimal places - forget 8 if thats too many, but can also hold the number size.

    I have tried long long, but i dont think it can hold decimals.
    If i were you i would try double,if it does not enough to satisfied then maybe long double..

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Doubles are probably good enough; try them out. You read and print them with
    Code:
    double n;
    scanf("%f", &n);  /* scanf: %f is for floats and doubles */
    printf("%lf\n", n);  /* printf: %f is for floats, %lf is for doubles */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    When you say to big for floats do you mean to many decimal places or the number is higher than the maximum value for a float?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Just because an integral type doesn't normally hold decimals doesn't mean you cant use it as if it did. For example if you wanted to hold 3 decimal places you can store the numbers multiplied by 1000. So a value of 3200 would mean 3.2 etc. You have to multiply of divide by 1000 when doing some things to keep the numbers in the right range, but it's not very hard.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Doubles are too small, what are the symbols for long doubles?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Really, what are you doing, and what range/number of decimals do you need?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    Quote Originally Posted by a.mlw.walker
    Doubles are too small, what are the symbols for long doubles?
    %Lf in both cases.

    You may also want to consider using a fixed precision math library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    Please clarify what you requirements are. What is the largest and smallest number you will need to represent. And what is the precision you need? I see you are an engineering student so you should know what i mean by precision.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    A couple of decimal places would be nice enough, but the numbers get very large due the calculation being Lorenz' equation for his butterfly effect -so they get bigger each time - it managed to print out
    11343638689789134000000000000000.000000
    but couldnt get the next one.

    the values are calculated based on any number the user enters, possibly ranging from 0.2 to maybe 1000.

    thanks

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds like you need GMP (GNU MultiPrecision library) - it has functions to give as much precision as the machine can support in terms of memory - something like millions or billions of digits in a modern PC.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  3. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 05:42 PM
  4. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  5. Replies: 6
    Last Post: 01-02-2004, 12:01 PM

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