Test integerness: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(5 intermediate revisions by 4 users not shown)
Line 202:
0 -0.05
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">IsInt ← ⊢(0=|˜)⍟⊢1=•Type
 
IsInt¨⊸≍˘ ⟨+, 0, ∞, ⊘, ¯∞, 'q', ¯42, ∞-∞, "BQN", 1e¯300, 9e15, π⟩</syntaxhighlight>
{{out}}
<pre>┌─
╵ 0 +
1 0
0 ∞
0 ⊘
0 ¯∞
0 'q'
1 ¯42
0 NaN
0 "BQN"
0 1e¯300
1 9000000000000000
0 3.141592653589793
┘</pre>
 
=={{header|C}}==
Line 857 ⟶ 877:
Is 5+0i an integer? true
Is 5-5i an integer? false</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
{Complex number}
 
type TComplex = record
Real,Imagine: double;
end;
 
{Tolerance for fuzzy integer test}
 
const Tolerance = 0.00001;
 
function IsFloatInteger(R,Fuzz: extended): boolean;
{Determine if floating point number is an integer}
var F: extended;
begin
F:=Abs(Frac(R));
if IsNan(R) or IsInfinite(R) then Result:=False
else Result:=(F<=Fuzz) or ((1-F)<=Fuzz);
end;
 
 
function IsComplexInteger(C: TComplex; Fuzz: extended): boolean;
{Determine if Complex number is an integer}
begin
Result:=(C.Imagine=0) and IsFloatInteger(C.Real,Fuzz);
end;
 
function GetBooleanStr(B: boolean): string;
{Return Yes/No string depending on boolean}
begin
if B then Result:='Yes' else Result:='No';
end;
 
 
procedure DisplayFloatInteger(Memo: TMemo; R: extended);
{Display test result for single floating point number}
var S: string;
var B1,B2: boolean;
begin
B1:=IsFloatInteger(R,0);
B2:=IsFloatInteger(R,Tolerance);
Memo.Lines.Add(Format('%15.6n, Is Integer = %3S Fuzzy = %3S',[R,GetBooleanStr(B1),GetBooleanStr(B2)]));
end;
 
 
procedure DisplayComplexInteger(Memo: TMemo; C: TComplex);
{Dispaly test result for single complex number}
var S: string;
var B1,B2: boolean;
begin
B1:=IsComplexInteger(C,0);
B2:=IsComplexInteger(C,Tolerance);
Memo.Lines.Add(Format('%3.1n + %3.1ni Is Integer = %3S Fuzzy = %3S',[C.Real,C.Imagine,GetBooleanStr(B1),GetBooleanStr(B2)]));
end;
 
 
procedure TestIntegerness(Memo: TMemo);
var C: TComplex;
begin
DisplayFloatInteger(Memo,25.000000);
DisplayFloatInteger(Memo,24.999999);
DisplayFloatInteger(Memo,25.000100);
DisplayFloatInteger(Memo,-2.1e120);
DisplayFloatInteger(Memo,-5e-2);
DisplayFloatInteger(Memo,NaN);
DisplayFloatInteger(Memo,Infinity);
Memo.Lines.Add('');
 
C.Real:=5; C.Imagine:=0;
DisplayComplexInteger(Memo,C);
C.Real:=5; C.Imagine:=5;
DisplayComplexInteger(Memo,C);
end;
</syntaxhighlight>
{{out}}
<pre>
25.000000, Is Integer = Yes Fuzzy = Yes
24.999999, Is Integer = No Fuzzy = Yes
25.000100, Is Integer = No Fuzzy = No
-2.1E120, Is Integer = Yes Fuzzy = Yes
-0.050000, Is Integer = No Fuzzy = No
NAN, Is Integer = No Fuzzy = No
INF, Is Integer = No Fuzzy = No
 
5.0 + 0.0i Is Integer = Yes Fuzzy = Yes
5.0 + 5.0i Is Integer = No Fuzzy = No
 
Elapsed Time: 11.308 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
func isint x .
if x mod 1 = 0
return 1
.
.
num[] = [ 25.000000 24.999999 25.0001 -2.1e120 -5e-2 0 / 0 1 / 0 ]
#
numfmt 10 0
for n in num[]
write n & " -> "
if isint n = 1
print "integer"
else
print "no integer"
.
.
</syntaxhighlight>
 
{{out}}
<pre>
25 -> integer
24.9999990000 -> no integer
25.0001000000 -> no integer
-2.1e+120 -> integer
-0.0500000000 -> no integer
nan -> no integer
inf -> no integer
</pre>
 
=={{header|Elixir}}==
Line 1,069 ⟶ 1,218:
F (-3.00000000000000,3.14159265358979)
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">#define isInteger(x) iif(Int(val(x)) = val(x), 1, 0)
 
Dim As String test(1 To 8) = {"25.000000", "24.999999", "25.000100", "-2.1e120", "-5e-2", "NaN", "Inf", "-0.05"}
 
For i As Integer = 1 To Ubound(test)
Dim As String s = test(i)
Print s,
If isInteger(s) then Print "is integer" Else Print "is not integer"
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>25.000000 is integer
24.999999 is not integer
25.000100 is not integer
-2.1e120 is integer
-5e-2 is not integer
NaN is not integer
Inf is integer
-0.05 is not integer</pre>
 
=={{header|Free Pascal}}==
Line 2,677 ⟶ 2,849:
{{libheader|Wren-fmt}}
The -2e120 example requires the use of BigRat to reliably determine whether it's an integer or not. Although the Num class can deal with numbers of this size and correctly identifies it as an integer, it would do the same if (say) 0.5 were added to it because integer determination is only reliable up to around 15 digits.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigRat
import "./complex" for Complex
import "./rat" for Rat
import "./fmt" for Fmt
 
var tests1 = [25.000000, 24.999999, 25.000100]
1,982

edits