Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Roundup Function?


cncah
 Share

Recommended Posts

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.

UP@    : 1
DOWN@  : 2

ERROR@ : -99999

value      : 0
type       : UP@

int_value  : 0
frac_value : 0


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

#//'type' is initialized to UP@(1) and set to UP@(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 = UP@,
        [
        value = int_value + 1
        ]
     if type = DOWN@,
        [
        value = abs_value
        ]
    else,
        [
        value = ERROR@
        ]

    type = UP@


#//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, UP@)
    "x_abs rounded up is  :", *x_abs, e$

    x_abs = vequ(x$)
    pround(!x_abs, DOWN@)
    "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
Link to comment
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.  

UP@    : 1
DOWN@  : 2

ERROR@ : -99999

value      : 0
type       : UP@

int_value  : 0
frac_value : 0


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

#//'type' is initialized to UP@(1) and set to UP@(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 = UP@,
		[
		value = int_value + 1
		]
	else, if type = DOWN@,
		[
		value = abs_value
		]
	else,
		[
		value = ERROR@
		]

	type = UP@


#//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, UP@)
    "x_abs rounded up is  :", *x_abs, e$

    x_abs = vequ(x$)
    pround(!x_abs, DOWN@)
    "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. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

 Share

  • 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

×
×
  • Create New...