Author Topic: Calculating Square root in Python  (Read 1773 times)

0 Members and 1 Guest are viewing this topic.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Calculating Square root in Python
« on: August 11, 2014, 04:36:06 AM »
I am trying to calculate the square root of subtracting 2 numbers ...

I tried
 sqrt (x)
I get error global name sqrt not defined

I tried
 math.sqrt
I get error global name math not defined

not sure how to do it  :bonk :bonk ... help appreciated

Code: [Select]
def distance(x,y):
distance= sqrt(x-y)
return str(distance)
When Voice command gets tough, use hand gestures

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2319
  • Karma: 47
    • View Profile
Re: Calculating Square root in Python
« Reply #1 on: August 11, 2014, 08:33:27 AM »
Hi Haddood, I tried following in VC and it works

PY.ExecString   result=({1}{3}{2})**.5

{1} is x
{2} is y
{3} is the computationally sign

here the command group (the math.xml is required in the VC payload folder)

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.5-->
<commandGroup open="True" name="math square root" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="620" name="square root" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result=({1}{3}{2})**.5</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>square root</phrase>
    <phrase>from</phrase>
    <payloadRange>1,5000</payloadRange>
    <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">payloads\math.xml</payloadFromXML>
    <payloadRange>1,5000</payloadRange>
  </command>
</commandGroup>

The python code will not work with negative numbers!
« Last Edit: August 11, 2014, 08:41:30 AM by Kalle »
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Calculating Square root in Python
« Reply #2 on: August 11, 2014, 08:49:19 AM »
If you want to use math.sqrt(x)

then at the top of your python code you need to write

import math

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Calculating Square root in Python
« Reply #3 on: August 11, 2014, 08:56:07 AM »
also your distance function does not look right.

try this:
Code: [Select]
import math

def distance(x,y):
    temp = x**2 + y**2
    distance= math.sqrt(temp)
    return str(distance)

print distance(5,6)

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Calculating Square root in Python
« Reply #4 on: August 13, 2014, 08:19:49 AM »
Many thanx James ... That did the trick ... My learning curve of python is pretty flat  :bonk

Kalle thanks for the feedback, I needed to solve it within python ... I will keep you method for when i need sqrt inside VC
When Voice command gets tough, use hand gestures