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?
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 ...
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?
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
grumpy - cool thanks ;D
Your question has been answered. Perhaps you should learn How To Ask Questions The Smart Way.Originally Posted by e66n06
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.
And you're not using integer math?Originally Posted by e66n06
I made a mistake, since corrected.Originally Posted by grumpy
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."Originally Posted by grumpy
Another bad choice of words.Originally Posted by manutd
"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.
Originally Posted by manutd
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.Originally Posted by e66n06
Please stop nitpicking. You know what I mean.
in that case you cannot use ==Originally Posted by e66n06
use a-b < delta && a- b > - delta
where delta is small enough (like 0.00001)
To be or not to be == true
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.
Bear in mind that you can not use the modulus on floating point numbers.
Originally Posted by grumpy
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.
Computing the remainder on dividing two integers involves only integer mathematics.
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.Originally Posted by vart