cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

operators in relations.

nrollins
1-Newbie

operators in relations.

Hi gang,



Does anyone know how to convert a real number to an integer in a relation?



Something like:



"SEGMENT_QTY=(INPUT_RADIUS*2*PI)INT"



Or do I need to create a new parameter of integer type and set them equal.?



Thanks



-Nate

10 REPLIES 10
TomU
23-Emerald IV
(To:nrollins)

If you set one parameter equal to another, the second will change type to match the first. If you have an integer parameter and set it equal to a real parameter, the integer parameter will change type to a real. (This is how you can change parameter types after they are created without recreating them.)

To convert a real to an integer, you will need to use either the FLOOR, CEIL, functions. If you want to choose whether to round up or down automatically, you will need to write a conditional statement to pick which one to use based on your starting value.

Tom U.

Here's an example of using floor and ceil to round off a number. You ought to be able to adapt it to your needs.

/* thickness = od - id divided by 2
thickness=(d15-d14)/2
/* x1= step diam before rounding
x1=d14+thickness
/* shift moves the decimal point the number of places you want to round to
shift=x1*1000
/* Interger removes the numbers to the right of the decimal
Interger=floor(shift)
/* rounder is just the numbers to the right of the decimal
rounder=shift-interger
/* the following if/else statement compares the rounder to .5
/* if the rounder is equal to or larger than .5 it rounds up. If
/* the rounder is smaller than .5 it rounds down.
If rounder<.5
rounded=floor(shift)
else
rounded=ceil(shift)
endif
/* reshift moves the decimal point back to the correct place
reshift=rounded/1000
/* d13 = the rounded step diam minus the step clearance
d13=reshift-0.05


David Haigh

Thanks everyone!



Here is the answer:



ceil() the smallest integer not less than the real value

floor() the largest integer not greater than the real value



And I also got a great sample to use as a template.



And I never had to open a browser and go to the forum! Love this exploder!



-Nate




bfrandsen
6-Contributor
(To:nrollins)

The simple solution is to use:
floor(x+.5).
This will round x up/down according to the basic rules.

/Bjarne

A simpler method to round to an integer is this:



Rounded_parameter = floor (parameter + 0.5)



By adding 0.5, if the value is closer to the lower integer, floor will
still round down, if it's closer to the upper integer, adding 0.5 will
bump it past the upper integer so floor will essentially round up.



Doug Schaefer
--
Doug Schaefer | Experienced Mechanical Design Engineer
LinkedIn

Should have real all my mail first, Bjarne beat me to it!



Doug Schaefer
--
Doug Schaefer | Experienced Mechanical Design Engineer
LinkedIn

I think you're missing the point of what I was trying to do in the relation example.

Say your math ends up with a number like this 123.15963 and you want to round off to 3 places.
123,15963 x 1000 = 123159.63
123159.63 + .5 = 123160.13
Floor of 123160.13 = 123160
123160 /1000 = 123.160

Or you could do
Shift=(x1*1000)+.5
Rounded=floor(shift)/1000

Floor(x+.5) isn't letting you choose how many places you want to round off to.

Or perhaps I'm missing your point?

David Haigh

Ah yes, excellent point. If all you want to do is round to the nearest
integer (which I think was the original question), the simple one line
solution works. If you need to round to a specific # of places, then
some additional trickery is required.



Doug Schaefer
--
Doug Schaefer | Experienced Mechanical Design Engineer
LinkedIn




"An integer is any number which can be either positive and negative but not a fractional number. It is also a whole number. Examples are -1,256, -589, -1, 0, 1, 569, 5,236. It is always a rational number."

But the whole interchange was educational regardless of the original question ..... thanks gentlemen.

Don




Try this:
L3 = floor(1000*L) - (floor(L)*1000)
if L3 == 0
L4 = "000"
else
L4 = itos(L3)
endif
L2 = itos(floor(L)) + "." + L4 + " "
SIZE = BASIC_DIA + NO_THDS + " x " + L2

[cid:image001.jpg@01CD08DE.56D51720]
The information contained in this electronic mail transmission is intended by Weil-McLain & for the use of the named individual or entity to which it is directed and may contain information that is confidential or privileged. If you have received this electronic mail transmission in error, please notify the sender of the error by reply email so that the sender's address records can be corrected and delete it from your system without copying or forwarding it, including any reply records.
Announcements
Business Continuity with Creo: Learn more about it here.

Top Tags