QB64 Phoenix Edition
testing a number's quare root is an integer and casting to a value? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: testing a number's quare root is an integer and casting to a value? (/showthread.php?tid=3428)

Pages: 1 2 3


testing a number's quare root is an integer and casting to a value? - madscijr - 01-27-2025

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... 

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



RE: testing a number's quare root is an integer and casting to a value? - SMcNeill - 01-27-2025

If num = INT(num) THEN it's an integer


RE: testing a number's quare root is an integer and casting to a value? - madscijr - 01-27-2025

(01-27-2025, 07:46 PM)SMcNeill Wrote: If num = INT(num) THEN it's an integer
Thank you sir, that's so simple, I love it.


RE: testing a number's quare root is an integer and casting to a value? - Pete - 01-27-2025

Just don't ever get caught by variable type precision limits...

num = 5542534534534534534534532453453454534534.5 ' Number too big for variable type.
Print InStr(Str$(num), ".") ' Fails.
If num = Int(num) Then Print "Integer" 'Fails

Pete


RE: testing a number's quare root is an integer and casting to a value? - Jack - 01-27-2025

Quote:testing a number's quare root is an integer and casting to a value? 
@madscijr
the question is a bit fuzzy to me, I thought you were asking whether a given square-root of a number was an integer
I haven't tested whether  the sqr function gives exact square-roots of numbers which square-root is an integer


RE: testing a number's quare root is an integer and casting to a value? - bplus - 01-27-2025

What is Mod 1 ?
Code: (Select All)
For i = 1 To 10

    MySquareRoot! = Sqr(i)

    Print i, MySquareRoot!, MySquareRoot! Mod 1, _IIf((MySquareRoot! Mod 1 = 0), "Integer", "Not Integer")
Next

looks like mod 1 = 0 for everything!

I give the testing of a number as an integer with Mod 1, a mod 1 rating. Smile


From Wiki:
Quote:What is mod 1 in math?
The range of values for an integer modulo operation of n is 0 to n − 1. a mod 1 is always 0. When exactly one of a or n is negative, the basic definition breaks down, and programming languages differ in how these values are defined.



RE: testing a number's quare root is an integer and casting to a value? - SMcNeill - 01-27-2025

Just curious:  Is anyone else's OCD trying to explode at the title of this topic?  QUARE ROOT??!! 

GAAAAAHHHHH!!!!!!


RE: testing a number's quare root is an integer and casting to a value? - bplus - 01-27-2025

Sounds OK when I read it out loud Big Grin


RE: testing a number's quare root is an integer and casting to a value? - Pete - 01-28-2025

Stop quaking about, geeze!

Pete Angry


RE: testing a number's quare root is an integer and casting to a value? - madscijr - 01-28-2025

(01-27-2025, 09:56 PM)Jack Wrote:
Quote:testing a number's quare root is an integer and casting to a value? 
@madscijr
the question is a bit fuzzy to me, I thought you were asking whether a given square-root of a number was an integer
I haven't tested whether  the sqr function gives exact square-roots of numbers which square-root is an integer
Yeah, I wanted to find if a number's square root was a straight integer, and thought casting it to an int (or long, just a non-floating point number really), I could then compare it to the actual square root, and see if they're equal. I hadn't tried cast in qb64pe yet but that could be useful for a lot of stuff.

(01-27-2025, 09:15 PM)Pete Wrote: Just don't ever get caught by variable type precision limits...

num = 5542534534534534534534532453453454534534.5 ' Number too big for variable type.
Print InStr(Str$(num), ".") ' Fails.
If num = Int(num) Then Print "Integer" 'Fails

Pete
Ha! Yeah, this isn't for really big numbers, integer is good.