Thread: Operations with negative/decimal numbers?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Unhappy Operations with negative/decimal numbers?

    Is there an alternative type to "int" in C++ that will allow me to use negative and decimal inputs but uses the same operations (+, -, /, *) as int does? I'm trying to write a program to do augmented matrices, and negatives at the very least are essential.
    Last edited by Dominus Casuum; 01-12-2007 at 04:30 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Is there an alternative variable to "int"
    "int" is not a variable, it is a type. A variable is an entity whose value can vary.

    that will allow me to use negative and decimal inputs but uses the same operations (+, -, /, *) as int does?
    Try the type double.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    So, as in:

    double x=-12.78;

    ?

    Is there a specific include statement or namespace statement you need to use double?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You must read on signed and unsigned types. Integral types, of which int is one of them, can be either signed or unsigned.

    If you are defining your variables as being of type int and can't produce negative values, then your compiler is not respecting the standard that states at some point integral types (execpt for boolean) are signed by default.

    On a standard compliant compiler, int value = -13, or signed int value = -13 should create value as a signed int. If that is not the case, you should create your integral variables with the signed specifier whenever you need to work with negative values.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why don't you just try? int, double, float, char - these are language keywords, you don't include them. Unless you specify them as "unsigned" (another keyword) they'll handle negative numbers as well.

    As far as I know, mathematical operators work on negative numbers just the same way as they do on positives

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Thanks for the help. I just don't know my way around the language, that's all.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by anon
    Why don't you just try? int, double, float, char - these are language keywords, you don't include them. Unless you specify them as "unsigned" (another keyword) they'll handle negative numbers as well.

    As far as I know, mathematical operators work on negative numbers just the same way as they do on positives
    char is implementation dependend
    it can be signed or unsigned.
    If you need to have signed char - better to write it explicetly
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM