Test integerness: Difference between revisions

no edit summary
(Added Java)
No edit summary
Line 438:
Value: 1.23e-10 of type: double is integer - true
</pre>
 
=={{header|C#}}==
<lang c sharp>
 
using System;
 
namespace Test_integerness
{
class Program
{
public static void Main(string[] args)
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" ***************************************************");
Console.WriteLine(" * *");
Console.WriteLine(" * Integerness test *");
Console.WriteLine(" * *");
Console.WriteLine(" ***************************************************");
Console.WriteLine();
ConsoleKeyInfo key = new ConsoleKeyInfo('Y',ConsoleKey.Y,true,true,true);
while(key.Key == ConsoleKey.Y)
{
// Get number value from keyboard
Console.Write(" Enter number value : ");
string LINE = Console.ReadLine();
// Get tolerance value from keyboard
Console.Write(" Enter tolerance value : ");
double TOLERANCE = double.Parse(Console.ReadLine());
// Resolve entered number format and set NUMBER value
double NUMBER = 0;
string [] N;
// Rational number value
if(LINE.Contains("/"))
{
N = LINE.Split('/');
NUMBER = double.Parse(N[0]) / double.Parse(N[1]);
}
// Inf value
else if(LINE.ToUpper().Contains("INF"))
{
NUMBER = double.PositiveInfinity;
}
// NaN value
else if(LINE.ToUpper().Contains("NAN"))
{
NUMBER = double.NaN;
}
// Complex value
else if(LINE.ToUpper().Contains("I"))
{
LINE = LINE.ToUpper().Replace("I","");
if( LINE.Contains("+") )
N = LINE.Split('+');
else
N = LINE.Split('-');
NUMBER = double.Parse(N[1]);
if(NUMBER==0)
NUMBER = double.Parse(N[0]);
else
NUMBER = double.NaN;
}
// Real value
else
NUMBER = double.Parse(LINE);
// Test
bool IS_INTEGER = false;
bool IS_INTEGER_T = false;
if(double.IsNaN(NUMBER))
IS_INTEGER=false;
else if(Math.Round(NUMBER,0).ToString() == NUMBER.ToString())
IS_INTEGER = true;
else if((decimal)TOLERANCE >= (decimal)Math.Abs( Math.Round(NUMBER,0) - NUMBER ))
IS_INTEGER_T = true;
if(IS_INTEGER)
Console.WriteLine(" Is exact integer " + IS_INTEGER);
else
{
Console.WriteLine( " Is exact integer " + IS_INTEGER );
Console.WriteLine( " Is integer with tolerance " + IS_INTEGER_T );
}
Console.WriteLine();
Console.Write(" Another test < Y /N > . . . ");
Console.ReadKey(true);
Console.WriteLine();
Console.WriteLine();
}
}
}
}
 
</lang>
{{out| Program Input and Output :}}
<pre>
 
***************************************************
* *
* Integerness test *
* *
***************************************************
 
Enter number value : 25,000000
Enter tolerance value : 0,00001
Is exact integer True
 
Another test < Y /N > . . .
 
Enter number value : 24,999999
Enter tolerance value : 0,00001
Is exact integer False
Is integer with tolerance True
 
Another test < Y /N > . . .
 
Enter number value : 25,000100
Enter tolerance value : 0,00001
Is exact integer False
Is integer with tolerance False
 
Another test < Y /N > . . .
 
Enter number value : -2.1e120
Enter tolerance value : 0,00001
Is exact integer True
 
Another test < Y /N > . . .
 
Enter number value : -5e-2
Enter tolerance value : 0,00001
Is exact integer False
Is integer with tolerance False
 
Another test < Y /N > . . .
 
Enter number value : NaN
Enter tolerance value : 0,00001
Is exact integer False
Is integer with tolerance False
 
Another test < Y /N > . . .
 
Enter number value : Inf
Enter tolerance value : 0,00001
Is exact integer True
 
Another test < Y /N > . . .
 
Enter number value : 5,0+0,0i
Enter tolerance value : 0,00001
Is exact integer True
 
Another test < Y /N > . . .
 
Enter number value : 5-5i
Enter tolerance value : 0,00001
Is exact integer False
Is integer with tolerance False
 
Another test < Y /N > . . .
 
Enter number value : 1,1
Enter tolerance value : 0,1
Is exact integer False
Is integer with tolerance True
 
Another test < Y /N > . . .
 
 
</pre>
 
 
 
=={{header|COBOL}}==