Author Topic: How do I make floating point numbers = integers??  (Read 2985 times)

0 Members and 1 Guest are viewing this topic.

monkee

  • $upporter
  • Jr. Member
  • *****
  • Posts: 40
  • Karma: 4
    • View Profile
How do I make floating point numbers = integers??
« on: May 12, 2017, 03:21:58 AM »
Ok, So I'm getting crazy answers to simple math I'm trying to do in Python.  After researching, it seems python does math in a strange way that noobs like myself find confusing (floating point).  I use python math in several VC commands so it's strange I'm just running into this problem for the first time now. 

The PY.ExecString action that is giving me trouble is this one:

Code: [Select]
result=({Var.var1}-{var.var2})
where both var 1 and 2 are decimals (ie. 0.123456)

When I replace them with the actual numbers instead of variables, the math works fine so for some reason the variables must be in float format instead of integers, I guess? 

Anyway, how do I tell that PYExecString to force the result to be an integer and not super long floating point numbers? 

I've spent hours on this so I hope someone can help!  Thank you!

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: How do I make floating point numbers = integers??
« Reply #1 on: May 12, 2017, 08:02:34 AM »
This is a Python question so you'll find a Python answer. Search Google for Python convert to integer.

For example: https://www.digitalocean.com/community/tutorials/how-to-convert-data-types-in-python-3

If you want us to give you a more specific answer we need to see all your code that sets the variables etc.
« Last Edit: May 12, 2017, 09:23:25 AM by jitterjames »

monkee

  • $upporter
  • Jr. Member
  • *****
  • Posts: 40
  • Karma: 4
    • View Profile
Re: How do I make floating point numbers = integers??
« Reply #2 on: May 12, 2017, 01:30:20 PM »
This is a Python question so you'll find a Python answer. Search Google for Python convert to integer.

For example: https://www.digitalocean.com/community/tutorials/how-to-convert-data-types-in-python-3

If you want us to give you a more specific answer we need to see all your code that sets the variables etc.

Thanks and yes I'd spent at least 5 hours over the last few days on at least a dozen python websites trying to figure out how to make this work before posting here.  While I certainly have a better understanding of why this happens, I still have not found a solution that works.  I very much appreciate your time and don't post on sites if I haven't nearly exhausted my other options first.  I love figuring things like this out but sometimes you need to hit that "hint" button to progress :)

I figured out that the variables don't matter.  I actually still get the same error using numbers directly, counter to what I thought in my first post.  I think at this point it's probably a syntax error.  I've tried inserting "int" every conceivable way, as well as at least a hundred variations of this "simple" subtraction line. I'm sure it's something basic and fully prepared to *facepalm* when I see the correct way to do it. 

Thank you again very much and sample code is below.  Please show me how to format it to get the correct answer 0.000001 :)

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.2.9-->
<command id="423" name="newcommandname" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=0.012345-0.012344</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{lastresult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: How do I make floating point numbers = integers??
« Reply #3 on: May 12, 2017, 02:08:33 PM »
Of course it helps if you know what an integer is.  An integer is a whole number without any decimal places so that is definitely not what you want because you would end up with 0.

Basically Python is using scientific notation and you want to suppress scientific notation.  It's a Python formatting thing.

The solution is just to put '%f' %  ( ... ) around your number.

So in your example the parameter code would be:
Code: [Select]
result='%f' %  (0.012345-0.012344)
I found the answer here: http://stackoverflow.com/questions/658763/how-do-i-suppress-scientific-notation-in-python

monkee

  • $upporter
  • Jr. Member
  • *****
  • Posts: 40
  • Karma: 4
    • View Profile
Re: How do I make floating point numbers = integers??
« Reply #4 on: May 12, 2017, 02:37:32 PM »
Of course it helps if you know what an integer is.  An integer is a whole number without any decimal places so that is definitely not what you want because you would end up with 0.

Basically Python is using scientific notation and you want to suppress scientific notation.  It's a Python formatting thing.

The solution is just to put '%f' %  ( ... ) around your number.

So in your example the parameter code would be:
Code: [Select]
result='%f' %  (0.012345-0.012344)
I found the answer here: http://stackoverflow.com/questions/658763/how-do-i-suppress-scientific-notation-in-python

True, true, haha.  I was definitely not searching using the correct terminology.  Thank you SO much.  I hadn't even come across the "f" thing in anything I'd read so I was far from solving this myself.  Thank you again, it works!!