01-27-2025, 07:44 PM
I need to test whether a number's square root is an integer (nothing after the decimal, test code below).
My first try was to use MOD and see if 1 divides into it evenly, but MOD is integer only.
I thought maybe I could cast the square root to an integer, and subtracting that from the square root value yields 0, we know there is a fractional portion left over, so it's not just an integer.
Did I hear something about a "cast" command being added at some point?
Even if that's not the best way to test the square root, that would be good to know about.
Any guidance would be appreciated...
My first try was to use MOD and see if 1 divides into it evenly, but MOD is integer only.
I thought maybe I could cast the square root to an integer, and subtracting that from the square root value yields 0, we know there is a fractional portion left over, so it's not just an integer.
Did I hear something about a "cast" command being added at some point?
Even if that's not the best way to test the square root, that would be good to know about.
Any guidance would be appreciated...
Code: (Select All)
Dim iLoop%
For iLoop% = 1 To 16
TestSquareRoot iLoop%
Next iLoop%
Sub TestSquareRoot (MyNumber%)
Dim MySquareRoot!
MySquareRoot! = Sqr(MyNumber%)
Print "For value " + _Trim$(Str$(MyNumber%)) + ", " + _
"square root is " + _trim$(str$(MySquareRoot!)) + ", " + _
_IIf((MySquareRoot! Mod 1 = 0), "even", "different")
End Sub