CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 07-03-2013, 02:00 PM   #1
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 899
eval -command for math: not working with division

I'm using "eval" to process the text in a textField (math!).
Seems to be working just fine with addition, subtraction and multiplication.
However, with division I get incorrect answers: it appears I only get ints.

eval("float $temp = " + "2/2");
gives 1

eval("float $temp = " + "2/5");
gives 0

Why?

EDIT: (and im using eval because I need these calculations at runtime)
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.

Last edited by DeadlyNightshade : 07-03-2013 at 02:27 PM.
 
Old 07-03-2013, 03:12 PM   #2
djx
Where's my pony?
 
djx's Avatar
David Johnson
vfx & creature td
Melbourne, Australia
 
Join Date: Jun 2003
Posts: 3,414
eval("float $temp = " + "2.0/5.0");

because otherwise maya is assuming they are integers, while doing the math.

David
__________________
http://www.djx.com.au
 
Old 07-03-2013, 05:11 PM   #3
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 899
I see.
Well is there any way of telling maya not to do that? Like make it consider all numbers to be floats and not ints.
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 07-03-2013, 07:17 PM   #4
zeroeffect
Code Junkie
portfolio
Chris Zurbrigg
Software Developer
Independent
Canada
 
Join Date: Jul 2005
Posts: 220
Quote:
Originally Posted by DeadlyNightshade
I see.
Well is there any way of telling maya not to do that? Like make it consider all numbers to be floats and not ints.
This is common behavior for many programming languages. Literals that don't have a decimal are treated as integers and dividing by an integer will return an integer (truncated) value. C/C++, Java and Python (prior to 3.x) all behave this way.

If you want everything to be treated as a float, you will need to parse the string and cast all integers to floats and then recombine the string prior to eval. In the end it would look something like this:

Code:
eval("float $temp = " + "float(2)/float(5)");

I realize you are using MEL, but here is a little python script (mostly untested) that uses a regular expression to split the string and then recombine it after casting integers to floats:
Code:
import re expression = "2*2/50+3.2-2" split_string = re.split("([()*/+-])", expression) final_string = "" for element in split_string: if element.isdigit(): final_string += "float({0})".format(element) else: final_string += element print(final_string) print(eval(final_string))
 
Old 07-03-2013, 09:36 PM   #5
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 899
Well, the plan is to learn Python soon enough anyway (got such a book on my desk right now) so that doesn't matter.

(and the script works just fine btw - thanks!)
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 07-03-2013, 09:48 PM   #6
ThE_JacO
MOBerator-X
 
ThE_JacO's Avatar
CGSociety Member
portfolio
Raffaele Fragapane
That Creature Dude
Animal Logic
Sydney, Australia
 
Join Date: Jul 2002
Posts: 10,954
Quote:
Originally Posted by DeadlyNightshade
Well, the plan is to learn Python soon enough anyway (got such a book on my desk right now) so that doesn't matter.

(and the script works just fine btw - thanks!)

Don't presume that will change though.
The order of operations there is basically performing the operation on ints and THEN casting it to a float.

Python, C++, and most common languages' interpreters would interpret what you wrote the same exact way, and return 0 (int) to then convert (cast) it to 0.0 (float).

It's a very common beginner mistake.
For the sake of readability and making intent explicit get in the habit of clearly distinguishing types of numerals as soon as you can.
__________________
"As an online CG discussion grows longer, the probability of the topic being shifted to subsidies approaches 1"

Free Maya Nodes
 
Old 07-04-2013, 01:21 PM   #7
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 899
Problem though is incorporating that into my MEL-script without having the py-code residing in a secondary file. The process of splitting a string into individual characters and then reassemble the string I can do in MEL with ease - but making sure that Maya understands that my numeric characters are all floats is a harder task. I end up with strings looking like this for example:
2.05.0.3.0/5.0.4.0

The more I use MEL the more I realize that python is far superior.
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 07-04-2013, 02:26 PM   #8
djx
Where's my pony?
 
djx's Avatar
David Johnson
vfx & creature td
Melbourne, Australia
 
Join Date: Jun 2003
Posts: 3,414
Why do you split the string into individual characters? With tokenize and some string functions you should be able to extract the numbers intact. Then just use zeroeffect's suggestion...

string $a = "253";
string $b = "54";
float $x = float($a)/float($b);
// Result: 4.685185 //

David
__________________
http://www.djx.com.au
 
Old 07-04-2013, 02:30 PM   #9
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 899
Yea I guess that's the best way.
But is it possible to feed tokenize with several separation characters?

EDIT: My mind is just not working properly today and I can't get a grip on this -_-
I just want something like this string
2*2/50+3.2-2

to be converted to:

2.0*2.0/50.0+3.2-2.0

...in MEL and I just don't see how tokenize can do that as it's a command that will omitt the separation characters.
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.

Last edited by DeadlyNightshade : 07-04-2013 at 02:57 PM.
 
Old 07-04-2013, 02:58 PM   #10
zeroeffect
Code Junkie
portfolio
Chris Zurbrigg
Software Developer
Independent
Canada
 
Join Date: Jul 2005
Posts: 220
Quote:
Originally Posted by DeadlyNightshade
Yea I guess that's the best way.
But is it possible to feed tokenize with several separation characters?

Tokenize does not match the entire split string; it matches any character in the string. So you could use something like:
Code:
string $e = "2*2/50+3.2-2"; $numTokens = `tokenize $e "+-*/()" $buffer`; print($buffer)

The one problem with tokenize, it doesn't keep a list of the characters that split the string. You would need separate code to extract the operators (in the correct order).

The nice thing about the re.split() function (in Python) is that you can optionally keep the split characters. If you split on math operators (+,-,*,/) the list returned keeps all of the elements in the correct order, making reassembly trivial. The returned list from the previous example looks like this:

Code:
['2', '*', '2', '/', '50', '+', '3.2', '-', '2']

-ChrisZ
 
Old 07-04-2013, 03:21 PM   #11
zeroeffect
Code Junkie
portfolio
Chris Zurbrigg
Software Developer
Independent
Canada
 
Join Date: Jul 2005
Posts: 220
Quote:
Originally Posted by DeadlyNightshade
EDIT: My mind is just not working properly today and I can't get a grip on this -_-
I just want something like this string
2*2/50+3.2-2

to be converted to:

2.0*2.0/50.0+3.2-2.0

...in MEL and I just don't see how tokenize can do that as it's a command that will omitt the separation characters.

My MEL is a little rusty, but...
Code:
string $e = "2*2/50+3.2-2"; string $numbers[]; string $operators[]; int $numNumbers = `tokenize $e "+-*/()" $numbers`; int $numOperators = `tokenize $e "0123456789." $operators`; $finalExpression = "float(" + $numbers[0] + ")"; for ($i = 0; $i < $numOperators; $i++) { $finalExpression += $operators[$i]; $finalExpression += "float(" + $numbers[$i+1] + ")"; } print("\n" + $finalExpression + "\n"); eval("float $result = " + $finalExpression);
This script calls tokenize twice, on the same string, to get different information (numbers and operators). Obviously, this example is very specific to the expected expression format, but it should give you some ideas.

When it comes to string manipulation, MEL is really quite limited when compared to a language like Python.

-ChrisZ
 
Old 07-04-2013, 04:19 PM   #12
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 899
Ah that is clever.
Thank you very much!
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 07-04-2013, 04:19 PM   #13
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:41 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.