question about pow()

This is a discussion on question about pow() within the C++ Programming forums, part of the General Programming Boards category; Hello Why will the following code output integer c as 0 (null)? Code: unsigned int c = pow( (double)2 , ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    question about pow()

    Hello

    Why will the following code output integer c as 0 (null)?

    Code:
    unsigned int c = pow( (double)2 , (double)1000);
    std::cout << c << "\n";
    What would be the proper way of calculating 2^1000 in c++?

    Thanks a lot!

  2. #2
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    An unsigned int is unable to store values larger than 2^32.

    I believe a double would be able to store it. Otherwise you would need a to use a bignum library.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146
    Even if a double can store it, it won't be with infinite precision. You will only get a few significant figures.

    If you want the answer with infinite precision, you will have to use a bignum library.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Waterbottle View Post
    An unsigned int is unable to store values larger than 2^32.

    I believe a double would be able to store it. Otherwise you would need a to use a bignum library.
    Actually: (2^32)-1 is the largest you can fit in a 32-bit integer.

    A double should be able to store 1.07E301, which is roughly the value of 2^1000.

    --
    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.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Oh and just poking, but in C++, it is really in our best to use the C++ variants of the C functions, std::pow.
    There should be one, no?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. newbie question
    By antheia in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2003, 10:05 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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