Thread: Largest/Smallest Int, Short, Long using pow?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Question Largest/Smallest Int, Short, Long using pow?

    I'm trying to figure out how to compute the largest and smallest int, short, and long, both signed and unsigned, using pow(). (Obviously not computing min unsigned number since that's zero). (And I'm using cmath library). What would this look like syntax wise?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Assuming 2's compliment and 8 bit chars, which most modern systems use:

    max = pow(2,size_of(int)*8-1)-1

    You can read up on two's compliment numbers for the other values. But there are macros and templates that will give you the correct constants without using pow.
    Last edited by King Mir; 01-20-2013 at 10:00 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't use pow for this, it would be like going around the block just to get next foor. Just use a bitshift.
    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"

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For future reference, cross-posting (i.e. here and here) is generally considered bad etiquette.

    To understand why, I'd recommend reading this through: How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by iMalc View Post
    Don't use pow for this, it would be like going around the block just to get next foor. Just use a bitshift.
    Some compilers can do that as an optimization anyway. So it's not a big loss.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Only if they know what pow actually does, which is not guaranteed.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum up the largest and smallest ones
    By programmer1 in forum C Programming
    Replies: 8
    Last Post: 11-08-2011, 11:35 AM
  2. Kth largest &kth smallest
    By baffleddreams in forum C Programming
    Replies: 16
    Last Post: 10-01-2010, 05:08 PM
  3. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  4. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  5. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 02:00 PM