math with integers

This is a discussion on math with integers within the C++ Programming forums, part of the General Programming Boards category; I have a variable that's an int. I say variable = (50 /2000) * 100 The logical answer is 2.5 ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    math with integers

    I have a variable that's an int.

    I say variable = (50 /2000) * 100

    The logical answer is 2.5

    My code say variable = 0.

    What gives?

    Code:
    int varx;
    varx = (50 /2000) * 100;
    oops, yes I know that int doesn't take decimals.
    Last edited by FoodDude; 09-14-2005 at 12:10 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    50/200 equals 0 if you use int's
    if a result of 2 would be good enough you could do
    Code:
    int varx = (50*100) /2000;
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by FoodDude
    Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.
    That doesn't help because 50 is still an int. You have to do it this way
    Code:
    double varx = (50.0 /2000.0) * 100.0;
    Kurt

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Thanks, I didn't realize the input type was tied so closely to the output type. I'll head down that path.

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.
    The variable you store in needs to be of type double. But in your addition with hard coded numbers if you use two ints, your result will be ints. 50/2000 is 0.025, but it is of type int so it only keeps what is in front of the decimal, then multiplies that by 100. Which is 0.

    Make 50, 50.0 or 2000, 2000.0, or both and it will work just fine.

    *edit*
    Bah, while I was typing this somebody else answered!

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Thanks for the help. I am using variables in my code instead of hard-coded numbers but was using hard-coded in some debugging to try and understand it. I've changes 100 to 100.0 and changed the other values to doubles. All is good. Thanks to all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-30-2008, 12:35 AM
  4. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

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