Operations with negative/decimal numbers?

This is a discussion on Operations with negative/decimal numbers? within the C++ Programming forums, part of the General Programming Boards category; Is there an alternative type to "int" in C++ that will allow me to use negative and decimal inputs but ...

  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 03:30 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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
    Portugal
    Posts
    7,383
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 10: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, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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