cncah

Roundup Function?

Recommended Posts

Is there a function to round UP a decimal to the nearest integer?

I am in need of rounding numbers like 1.415 to 2, 3.46 to 4, etc.

Any help with getting my G-Code more accurate would be greatly appreciated. Thanks.

Share this post


Link to post
Share on other sites

First add 0.50, and then do the normal round.

Share this post


Link to post
Share on other sites

MP has a round( ) function, but I don't see any way to ensure you're always rounding up.   There is also the rounding method in the format statement, but based on the definition below I don't think it will help you

Rounding method. Use the following values:

  • (omitted) - Normal rounding.
  • 2 - Round final decimal to nearest even number.
  • 5 - Round final decimal to nearest multiple of 5.
  • 9 - Truncate the decimal portion of the value at the specified number of digits.

Below is an example that should round the passed in value up (or down), but I can't test it.  Maybe it will help you.

[email protected]    : 1
[email protected]  : 2

[email protected] : -99999

value      : 0
type       : [email protected]

int_value  : 0
frac_value : 0


#//The first argument is being passed by reference '!'

#//'type' is initialized to [email protected](1) and set to [email protected](1) at the end of 'pround'
#//Because of this, we can consider 'type' optional
pround(!value, type)
    #//Get the integer portion of the passed in value
    int_value = int(value)

    #//Get the fractional portion of the passed in value
    #//The fractional portion is not used in this example
    frac_value = frac(value)

     if type = [email protected],
        [
        value = int_value + 1
        ]
     if type = [email protected],
        [
        value = abs_value
        ]
    else,
        [
        value = [email protected]
        ]

    type = [email protected]


#//Calling example
#//Output (depending on format) ->
#//        x_abs is             : 4.75342
#//        x_abs rounded up is  : 5
#//        x_abs rounded down is: 4
#//
#//        x_abs rounded up is  : 5

x_abs : 0
y_abs : 0
z_abs : 0

pany_postblock
    x$ = 4.75342
    x_abs = vequ(x$)

    "x_abs is             :", *x_abs, e$
    pround(!x_abs, [email protected])
    "x_abs rounded up is  :", *x_abs, e$

    x_abs = vequ(x$)
    pround(!x_abs, [email protected])
    "x_abs rounded down is:", *x_abs, e$
    *e$

    #//The 'type' argument is optional
    x_abs = vequ(x$)
    pround(!x_abs)
    "x_abs rounded up is  :", *x_abs, e$

 

  • Like 1

Share this post


Link to post
Share on other sites
19 hours ago, Henk said:

First add 0.50, and then do the normal round.

I was close, but not correct:    2.0 + 0.5 = 2.5, will be round to 3.0

So first check for already being an integer.

then add 0.5 and round.

Share this post


Link to post
Share on other sites

In macros we can use the FUP (fix up) function. Not sure if that will work for you in the post processor.

Share this post


Link to post
Share on other sites

Thanks for the replies. The problem is not knowing which way to round.

I appreciate the example on how to use a function Jeff.

I will do something similar with a bunch of if statements to get the output I need.

Share this post


Link to post
Share on other sites
10 hours ago, cncah said:

Thanks for the replies. The problem is not knowing which way to round.

I appreciate the example on how to use a function Jeff.

I will do something similar with a bunch of if statements to get the output I need.

I found an error in the code snippet I posted previously, a corrected version is below.  

[email protected]    : 1
[email protected]  : 2

[email protected] : -99999

value      : 0
type       : [email protected]

int_value  : 0
frac_value : 0


#//The first argument is being passed by reference '!'

#//'type' is initialized to [email protected](1) and set to [email protected](1) at the end of 'pround'
#//Because of this, we can consider 'type' optional
pround(!value, type)
    #//Get the integer portion of the passed in value
    int_value = int(value)

    #//Get the fractional portion of the passed in value
    #//The fractional portion is not used in this example
    frac_value = frac(value)

	if type = [email protected],
		[
		value = int_value + 1
		]
	else, if type = [email protected],
		[
		value = abs_value
		]
	else,
		[
		value = [email protected]
		]

	type = [email protected]


#//Calling example
#//Output (depending on format) ->
#//        x_abs is             : 4.75342
#//        x_abs rounded up is  : 5
#//        x_abs rounded down is: 4
#//
#//        x_abs rounded up is  : 5

x_abs : 0
y_abs : 0
z_abs : 0

pany_postblock
    x$ = 4.75342
    x_abs = vequ(x$)

    "x_abs is             :", *x_abs, e$
    pround(!x_abs, [email protected])
    "x_abs rounded up is  :", *x_abs, e$

    x_abs = vequ(x$)
    pround(!x_abs, DO[email protected])
    "x_abs rounded down is:", *x_abs, e$
    *e$

    #//The 'type' argument is optional
    x_abs = vequ(x$)
    pround(!x_abs)
    "x_abs rounded up is  :", *x_abs, e$

  It's also worth noting that this example will only work for positive numbers.  It would need to be modified to round positive and negative numbers correctly. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us