SQR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The '''SQR''' function returns the square root of a numerical value. {{PageSyntax}} :: square_root = '''SQR('''value''')''' * The ''square root'' returned is normally a SINGLE or DOUBLE numerical value. * The ''value'' parameter can be any '''positive''' numerical type. '''Negative parameter values will not work!''' * Other exponential root functions can use fractional exponents(^) enclosed in '''parenthesis only'''. EX: {{text|root <nowiki> = </nowiki> c...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:


{{PageSyntax}}
{{PageSyntax}}
:: square_root = '''SQR('''value''')'''
: square_root = '''SQR('''value''')'''




{{PageDescription}}
* The ''square root'' returned is normally a [[SINGLE]] or [[DOUBLE]] numerical value.
* The ''square root'' returned is normally a [[SINGLE]] or [[DOUBLE]] numerical value.
* The ''value'' parameter can be any '''positive''' numerical type. '''Negative parameter values will not work!'''
* The ''value'' parameter can be any '''positive''' numerical type. '''Negative parameter values will not work!'''
* Other exponential root functions can use fractional exponents([[^]]) enclosed in '''parenthesis only'''. EX: {{text|root <nowiki> = </nowiki> c ^ (a / b)|green}}
* Other exponential root functions can use fractional exponents([[^]]) enclosed in '''parenthesis only'''. EX: {{Text|root <nowiki> = </nowiki> c ^ (a / b)|green}}




{{PageExamples}}
''Example 1:'' Finding the hypotenuse of a right triangle:
''Example 1:'' Finding the hypotenuse of a right triangle:
{{CodeStart}} '' ''
{{CodeStart}}
  A% = 3: B% = 4
  A% = 3: B% = 4
  PRINT "hypotenuse! ="; SQR((A% ^ 2) + (B% ^ 2)) '' ''
  PRINT "hypotenuse! ="; SQR((A% ^ 2) + (B% ^ 2))
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 21: Line 23:




''Example 2:'' Finding the Cube root of a number.  
''Example 2:'' Finding the Cube root of a number.
{{CodeStart}} '' ''
{{CodeStart}}
  number = 8
  number = 8
  cuberoot = number {{Cl|^}} (1/3)
  cuberoot = number {{Cl|^}} (1/3)
  PRINT cuberoot '' ''
  PRINT cuberoot
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 33: Line 35:


''Example 3:'' Negative roots return fractional values of one.
''Example 3:'' Negative roots return fractional values of one.
{{CodeStart}} '' ''
{{CodeStart}}
  number = 8
  number = 8
  negroot = number {{Cl|^}} -2
  negroot = number {{Cl|^}} -2
  PRINT negroot '' ''
  PRINT negroot
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 60: Line 62:
IF PR = 1 THEN PRIME = 0  'MOD returns true but 1 is not a prime by definition
IF PR = 1 THEN PRIME = 0  'MOD returns true but 1 is not a prime by definition
IF PRIME THEN PRINT "PRIME! How'd you find me? " ELSE PRINT "Not a prime, you lose!"
IF PRIME THEN PRINT "PRIME! How'd you find me? " ELSE PRINT "Not a prime, you lose!"
LOOP UNTIL PR = 0 '' ''
LOOP UNTIL PR = 0
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 69: Line 71:




''See also:''
{{PageSeeAlso}}
*[[MOD]] {{text|(integer remainder division)}}
* [[MOD]]
*[[^]] {{text|(exponential operator)}}
* [[^]]
*[[Mathematical Operations]]
* [[Mathematical Operations]]
*[[Mathematical_Operations#Derived_Mathematical_Functions|Derived Trigonometric Functions]]
* [[Mathematical Operations#Derived_Mathematical_Functions|Derived Mathematical Functions]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 22:51, 11 February 2023

The SQR function returns the square root of a numerical value.


Syntax

square_root = SQR(value)


Description

  • The square root returned is normally a SINGLE or DOUBLE numerical value.
  • The value parameter can be any positive numerical type. Negative parameter values will not work!
  • Other exponential root functions can use fractional exponents(^) enclosed in parenthesis only. EX: root = c ^ (a / b)


Examples

Example 1: Finding the hypotenuse of a right triangle:

 A% = 3: B% = 4
 PRINT "hypotenuse! ="; SQR((A% ^ 2) + (B% ^ 2))
 hypotenuse = 5


Example 2: Finding the Cube root of a number.

 number = 8
 cuberoot = number ^ (1/3)
 PRINT cuberoot
 2


Example 3: Negative roots return fractional values of one.

 number = 8
 negroot = number ^ -2
 PRINT negroot
 .015625
Explanation: A negative root means that the exponent value is actually inverted to a fraction of 1. So x ^ -2 actually means the result will be: 1 / (x ^ 2).


Example 4: Fast Prime number checker limits the numbers checked to the square root (half way).

DEFLNG P
DO
PRIME = -1   'set PRIME as True
INPUT "Enter any number to check up to 2 million (Enter quits): ", guess$
PR = VAL(guess$)
IF PR MOD 2 THEN              'check for even number
  FOR P = 3 TO SQR(PR) STEP 2 'largest number that could be a multiple is the SQR
    IF PR MOD P = 0 THEN PRIME = 0: EXIT FOR 'MOD = 0 when evenly divisible by another
  NEXT
ELSE : PRIME = 0 'number to be checked is even so it cannot be a prime
END IF
IF PR = 2 THEN PRIME = -1 '2 is the ONLY even prime
IF PR = 1 THEN PRIME = 0  'MOD returns true but 1 is not a prime by definition
IF PRIME THEN PRINT "PRIME! How'd you find me? " ELSE PRINT "Not a prime, you lose!"
LOOP UNTIL PR = 0
Enter any number to check up to 2 million (Enter quits): 12379
PRIME! How'd you find me?
Note: Prime numbers cannot be evenly divided by any other number except one.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link