How can I test if something is an integer or not?

This is a discussion on How can I test if something is an integer or not? within the C Programming forums, part of the General Programming Boards category; all i want to do is test if something like: (1+ 6.0*7.0)/8.0 gives me an integer or not is there ...

  1. #16
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    all i want to do is test if something like: (1+ 6.0*7.0)/8.0 gives me an integer or not

    is there a function for it?

  2. #17
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I think for what the OP is actually trying to do, all his variable should be floats, that way his expression would be a float.

    If this is the case, after your reach your float result, you can see if it is an integer by subtracting its floor value and seeing if the result is zero or not.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #18
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    grumpy - cool thanks ;D

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by e66n06
    can anyone actually answer my question please, instead of ranting
    Your question has been answered. Perhaps you should learn How To Ask Questions The Smart Way.

    While you're at it, learn some math, and you'll have your answer.

    2 = 10 / 5
    5 = 10 / 2
    10 = 2 * 5

    Now, given your bit of math, create yourself a test scenario to see if the values you get wind up the same. If they are, then all of your math was perfect little integers, and you didn't have any decimals.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Quote Originally Posted by e66n06
    well i have the following integers: k, t and e
    And you're not using integer math?
    Quote Originally Posted by grumpy
    Rubbish. All rounding of integers in C and C++ truncates towards zero.
    I made a mistake, since corrected.
    Quote Originally Posted by grumpy
    does not involve anything being computed as a floating point value
    Bad choice of words on my part. I meant "everything that would be a floating point number using normal math is truncated using integer math."
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by manutd
    Bad choice of words on my part. I meant "everything that would be a floating point number using normal math is truncated using integer math."
    Another bad choice of words.

    "Normal mathematics" has nothing to do with floating point numbers. The mathematics for division of real values has some different characteristics from division of integers. Mainly because integers are discrete values, but real values can take any value on a continuum.

    Consider the problem : we have two values a and c, with a > c and need to find a value b such that a>b>c. With a and b being integers, no result is possible if c = a+1 as there is no integer between a and c With real values, is it guaranteed a value b will exist, as (a+c)/2 will always satisfy the required a>b>c.

    Floating point types are just a means by which computers approximately represent real numbers. One characteristic is that they cannot represent all real values. So it is possible to have floating point values a and c with a > c, and for it to be impossible to find any floating point value b that is between them.

  7. #22
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    Quote Originally Posted by manutd
    And you're not using integer math?

    no, i was not using integer math, sorry for not making that clear XD

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by e66n06
    no, i was not using integer math, sorry for not making that clear XD
    If my first post in this thread helped you, you are using integer math.

  9. #24
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Please stop nitpicking. You know what I mean.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #25
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by e66n06
    no, i was not using integer math, sorry for not making that clear XD
    in that case you cannot use ==
    use a-b < delta && a- b > - delta

    where delta is small enough (like 0.00001)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #26
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    If you are wanting to know if the value of something will be evenly divisible by another, try % in place of /. % is the mod operator and returns the remainder of a divide.

    However, your question is worded in a strange, ambiguous manner. What everyone is telling you is that in C, any int / int = int PERIOD -- there is no such thing as int / int = float, unless you did (float)((float) int / (float) int) = float. I believe, however, that your question is not int but whole number, this is what I have answered.

  12. #27
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Bear in mind that you can not use the modulus on floating point numbers.

  13. #28
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    Quote Originally Posted by grumpy
    If my first post in this thread helped you, you are using integer math.

    you post helped cos i was using integer maths to solve the problem. but i was not using integer maths in the sense quzah mentioned them as i am interested in the remainder.

  14. #29
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Computing the remainder on dividing two integers involves only integer mathematics.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by vart
    in that case you cannot use ==
    use a-b < delta && a- b > - delta

    where delta is small enough (like 0.00001)
    I believe an integer of sufficiently small absolute value can be represented exactly as a floating-point type of given size. For example, an IEEE 754 32-bit float uses 23 bits for the mantissa (and 8 for the exponent, and 1 for the sign bit), and 2^23 is roughly 8 million, so I believe integers between roughly plus and minus 4 million can be represented exactly as such a float. In this case, it would be okay to use ==. If that's not enough precision, an IEEE 754 64-bit double uses (52, 11, 1) bits for the 3 fields, so should be able to represent integers between roughly plus and minus 2^51.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM

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