Jump to content
Sign in to follow this  
Kevin McAllister

Rounding numbers and setting number of decimal places

Recommended Posts

Can someone help with rounding numbers and setting the number of decimal places for Marionette output? I'm using the Round node set to zero and when I start with 150.5 as a layer elevation I get 150.000000 as a result back. Not sure what I'm doing wrong.

Kevin

ubbthreads.php?ubb=download&Number=16305&filename=Screen%20Shot%202016-08-15%20at%208.56.20%20PM.png

Edited by Kevin McAllister

Share this post


Link to post

Hey Kevin,

So rounding doesn't appear to truncate the number, which is what you're really looking for.

If you use the "trunc" node after your "round" node, I think you'll get what you want.

I'm also in the process of getting nodes to follow the rounding display preferences set in the application, but that won't be available for a bit longer.

EDIT:

Trunc may only solve the issue if you're looking for whole numbers.

Edited by MarissaF

Share this post


Link to post

Also - to follow up.

Since the reason for your rounding is display purposes, another way to get rid of trailing zeros would be to use the "String" node (str) in the String category.

This would be useful for the cases where you may want to round out 3 decimal places but still see what's past the whole number.

After you convert the number to a string, you can put it right back into the text object.

Edited by MarissaF

Share this post


Link to post

Hi Marissa,

Thanks for the info. I'll look at Trunc.

I accidentally posted an old image with my original post. Here's a clearer example of what's happening. The Layer elevation is set to 150.5mm. The top object contains the Round node, which doesn't round or truncate. The bottom node is without it showing the value it should be acting on.

The Round node is the first node I've encountered that only contains Python script. It doesn't call the Vectorscript rounding function. I looked online for some documentation and Round seems to be correct. I found this documentation that talks about getting weird results from a "Float" - https://docs.python.org/3/library/functions.html#round

Perhaps its something with the output of the Get Layer Elevation node output? I don't know what a "Float" is....

Kevin

ubbthreads.php?ubb=download&Number=16307&filename=Screen%20Shot%202016-08-17%20at%208.43.48%20AM.png

Edited by Kevin McAllister

Share this post


Link to post

A float is a variable type. You could compare it to a double.

Since Python is an interpreted language, it generally chooses the type of our variables on its own.

In actuality, I believe ALL of our Math nodes use only Python. This is for development reasons, Vectorscript isn't really being maintained anymore since Python is much more flexible.

The reason the number of decimal places is so long is because VW uses long floats in its math. This becomes more apparent when displaying values from a script. Since VW has the preference of how far out numbers are displayed, many users aren't aware how precise the values used in the software actually are.

In my post right above your most recent one is my best solution so far. The Str node will get rid of trailing zeros, so after you pick your precision with the Round node, it's done the best at displaying what you want (except in the case of an Integer, then you would want to use Trunc). This will also change the variable type, though, from float to string, so I would advise doing any math you want done BEFORE converting it.

You're more than welcome to use the VS Round function, but it is unlikely we will ever ship it with default content since it's getting outdated.

Share this post


Link to post

I also want to clarify what you're seeing with python's Round function.

https://docs.python.org/2/library/functions.html#round

Note: The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

http://stackoverflow.com/questions/7515266/rounding-problem-with-python

The documentation for the built-in round() function says that it rounds to the nearest value, rounding ties away from zero. Since the decimal fraction 2.675 is exactly halfway between 2.67 and 2.68, you might expect the result here to be (a binary approximation to) 2.68. It’s not, because when the decimal string 2.675 is converted to a binary floating-point number, it’s again replaced with a binary approximation, whose exact value is

2.67499999999999982236431605997495353221893310546875

Since this approximation is slightly closer to 2.67 than to 2.68, it’s rounded down.

Edited by MarissaF

Share this post


Link to post

Thanks for all of the info. I can get what I need from this.

One last question - is there a way to convert a node's output from a float to something that will round properly? After some experimenting it seems rounding is required before converting to a string. I can find a node that converts to a Float, but nothing that converts from a Float to a real number for example.

(I suppose this may all be solved when you finish updating the nodes so the follow the rounding display precision....)

Kevin

Share this post


Link to post

I haven't found anything browsing the internet that can do that.

Python appears to only have 4 numeric types - int, float, long, and complex - unfortunately it doesn't look like any of them would play nicer with round.

Share this post


Link to post

Ok. Interesting.

This explains why I couldn't get things to Round properly in Alan's fence network. I ended up using Ceil and Floor instead.

This makes your project to make the nodes follow the rounding display precision even more important... except how are you going to round things to the correct precision :-)

Kevin

Share this post


Link to post

Kevin, this one rounds the numbers

Hans-Olav set this up, Marissa added the add thousands and sub-string and I added the text formatting.

Just move the object between the design layer in the OIP and the note changes

Also the rounding node rounds up and down by the <> 0.5 of the result in the file attached.

Hope I am not missing the point again?

HTH

Level_Marker.vwx

Rounding_001.vwx

Edited by Marissa Farrell
Attached files

Share this post


Link to post

Thanks Alan. Your prodding made me go back at look at things again and I tracked down the cause.

It turns out the problem is my Concat node. Not sure why. Even though its getting a rounded number as input and its only supposed to combine strings, it somehow adds back the extra zeros. It may be a limitation of the VS Concat command.

I really wish we could use the Debug mode to trace things within a Marionette object.....

And that selecting the Node dropdown menu automatically switched back out of the Debug mode....

Kevin

ubbthreads.php?ubb=download&Number=16325&filename=Screen%20Shot%202016-08-18%20at%208.13.19%20AM.png

Edited by Kevin McAllister

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×
×
  • Create New...