there is given real number how find by programming if it is Almost integer?
http://mathworld.wolfram.com/AlmostInteger.html
thanks
there is given real number how find by programming if it is Almost integer?
http://mathworld.wolfram.com/AlmostInteger.html
thanks
Exactly how you would do that depends on the language you're using. For example, in C# using the decimal
type you could have:
public static bool IsAlmostInteger(decimal value, decimal threshold)
{
decimal closestInteger = Math.Round(value);
decimal diff = Math.Abs(closestInteger - value);
return diff < threshold;
}
http://www.java2s.com/Open-Source/Java-Document/Science/Apache-commons-math-1.1/org/apache/commons/math/fraction/Fraction.java.htm has an example Fraction
method which checks for 'almost' integers with an epsilon allowance value, but whether that's accurate enough for you is up to you?
For any language --
Note that the rounding method in (1) only is predictable in the positive numbers. If you want one that works for all, try looking at rounding methods. Suffice to say, most programming languages already have a built-in rounding function, if you can find it.