How to Test If a Double Is an Integer

How to test if a double is an integer

if ((variable == Math.floor(variable)) && !Double.isInfinite(variable)) {
// integer type
}

This checks if the rounded-down value of the double is the same as the double.

Your variable could have an int or double value and Math.floor(variable) always has an int value, so if your variable is equal to Math.floor(variable) then it must have an int value.

This also doesn't work if the value of the variable is infinite or negative infinite hence adding 'as long as the variable isn't inifinite' to the condition.

How can I accurately determine if a double is an integer?

double can store an exact representation of certain values, such as small integers and (negative or positive) powers of two.

If it does indeed store an exact integer, then ((int)d == d) works fine. And indeed, for any 32-bit integer i, (int)((double)i) == i since a double can exactly represent it.

Note that for very large numbers (greater than about 2**52 in magnitude), a double will always appear to be an integer, as it will no longer be able to store any fractional part. This has implications if you are trying to cast to a Java long, for instance.

How can I check if a value is of type Integer?

If input value can be in numeric form other than integer , check by

if (x == (int)x)
{
// Number is integer
}

If string value is being passed , use Integer.parseInt(string_var).
Please ensure error handling using try catch in case conversion fails.

Checking if a double value is an integer - Swift

Try 'flooring' the double value then checking if it is unchanged:

let dbl = 2.0
let isInteger = floor(dbl) == dbl // true

Fails if it is not an integer

let dbl = 2.4
let isInteger = floor(dbl) == dbl // false

How to determine if a decimal/double is an integer?

For floating point numbers, n % 1 == 0 is typically the way to check if there is anything past the decimal point.

public static void Main (string[] args)
{
decimal d = 3.1M;
Console.WriteLine((d % 1) == 0);
d = 3.0M;
Console.WriteLine((d % 1) == 0);
}

Output:

False
True

Update: As @Adrian Lopez mentioned below, comparison with a small value epsilon will discard floating-point computation mis-calculations. Since the question is about double values, below will be a more floating-point calculation proof answer:

Math.Abs(d % 1) <= (Double.Epsilon * 100)

What's a good way to check if a double is an integer in C#?

return Math.Truncate(number) == number;

As mentioned in the comments, you might need to take account of the fact that a double representation of your number might not be an exact integer. In that case you'll need to allow for some margin-of-error:

double diff = Math.Abs(Math.Truncate(number) - number);
return (diff < 0.0000001) || (diff > 0.9999999);

How to determine whether a double variable has integer value?

If I would be you, I will simply take the int/long value of the roots and re-verify the equation to make sure that int/long value of the root is OK or not e.g.

// using round because an equivalent int/long may be represented by a near binary fraction
// as floating point calculations aren't exact
// e.g. 3.999999.. for 4
long longRoot = Math.round(root1);
if(a*longRoot*longRoot + b*longRoot + c==0){
//its valid int root
}else{
//ignore it
}

How to verify if user's input is a double or an integer?

I'm assuming your code contains the following statements:

      Scanner userIn = new Scanner(System.in);
//userIn.next() somewhere in your code, which is what you want to test

You could use the following method (pass userIn.next() as an argument):

        public boolean isDouble(String userInput){
if(userInput.contains(".")){
return true;
}
else{
return false;
}
}

This code basically checks if the userInput String contains a decimal point, if so the input is a double else it would be a false, you could also add preliminary conditions to make sure that the userInput is a number and not anything else.


EDIT:
As Klitos Kyriacou has rightfully pointed out in the comments, my answer did not initially answer the question asked, however the OP was satisfied... for the sake of completeness, I will answer the question specifically:

The sqrt method from the static class Math returns a double, so using the test above will not work as Math.sqrt(25) will return 5.0.

To go about this one can simply compare the rounded value of the sqrt's return value to the actual return value of the sqrt method.

You could use the following method (As in the case above userIn.next(), will be the argument to the method):

 public boolean isPerfectSquare(String userInput){
if(Math.sqrt(Double.parseDouble(userInput)) == Math.round(Math.sqrt(Double.parseDouble(userInput)))){
return true;
}
return false;
}

Check double variable if it contains an integer, and not floating point

Use std::modf:

double intpart;
modf(value, &intpart) == 0.0

Don't convert to int! The number 1.0e+300 is an integer too you know.

Edit: As Pete Kirkham points out, passing 0 as the second argument is not guaranteed by the standard to work, requiring the use of a dummy variable and, unfortunately, making the code a lot less elegant.



Related Topics



Leave a reply



Submit