Martin's getting sneaky trimming the digits as a string instead of a real number. This is a great trick... if only more people understood some of the nifty stuff you can do with relations. Unfortunately it's one of those rare arts that few seem to know about.
Martin is really giving a goldmine of information here but you have to dig deep to fully appreciate it.
The four functions Martin is using are:
But you really have to examine the code to truly see the beauty here.
The itos function only converts integers to strings. It cannot handle decimal values (real numbers). If you wrote x = itos(12.98765) you'd end up with x="12". The decimal values would be discarded. Therefore, Martin's relations incorporate a thoughtful bit of math to handle this problem.
First, he sets the variable number_digits to 3. Then, first set a variable for the number digits after the decimal to preserve. He uses the floor function to round down the real number x which starts at 12.98765. Once this function has completed it's work, x is 12.987.
Next, Martin uses the itos function. He multiples 12.987 by 10 to the power of 3 (or 1000) to remove the decimals. Thus, once itos has done it's work, the value has changed from 12.987 to "12987" (now a string with the decimal removed)
In the last line of the relations, Martin uses extract and the string_length functions together. This final line writes out the first two characters of the string "12987" which are just "12". Next a dot (.) is written out. Finally, the last 3 characters of the string ("987") are written. Together this is "12.987" which completes the conversion from 3 digit real number to string".
The whole thing is very, very clever. Thanks for posting this Martin.
PS: There's one very slight problem though... I'll leave it to any curious readers to figure it out. Here's a hint... it has to do with the "6" in the original value of x which was 12.98765 Maybe someone will catch it and demonstrate the fix.
Oh, I definitely appreciate Martin's approach. In the day, I was very into programming DBASE III and IV and completely understand the approach and even the syntax.
The use of "FLOOR" might be a bit troubling since a "ROUND" should be available to round to either the "even principle" or the conventional 1-4=down and 5-9=up. But even thsat could be "relationed" to resolution.
Why a "new empty part"?
This stuff has to be written somewhere; No?
Yep... the use of Floor was the "one very slight problem". Martin could've incorporated a test to do the rounding better. As it stands 12.98765 rounds to 12.987 when it should round to 12.988. A simple if/then/else test and the addition of the CEIL command could resolve this.
But that's just being nitpicky...
Brian, I'd love to hear this from the "horse's mouth" so to speak...
Quite some time ago, I heard it was NASA that came up with the Round "5" to Even rule. Is this a fact?
n.nn5 round to n.nn(even) where n.nn(1-4) rounds down and n.nn(6-9) round up.
And where does that leave n.nn51 in relation to n.nn?
Its a great rule but the source has remained ambiguous. I'd love to confirm this with references.
of course you can use relations in any part. I thought the most easiest method for testing is using "new empty part".
To implement the conventional 1-4=down and 5-9=up "ROUND" principle, it is necessary to modify relations slightly, see below.
/* input variables
/* output variables
Yes indeed - adding 0.5 (or 0.0005, depending on the number of decimals you're rounding to) is a much neater technique than if/else. I've used that before with Excel's INT() function.
Unfortunately, "the horse" can neither confirm nor deny that NASA is responsible or the rounding rule you mentioned.
One of my children pulled me aside the other day and said: "Dad... is there life on other planets?" Of course I said "We don't know yet but we're looking... and personally I believe there is life on other planets just by the sheer mathematical probability. I think we'll eventually find it."
This seemed to be a reasonable and, I hoped, thoughtful answer. When I asked why the child wanted to know, he said: "Well Uncle Danny (a huge redneck related to my ex-wife) said NASA already has proof aliens exist but they keep it secret from everyone. Is that true?"
I just caught this thread and was a little excited to see if PTC had actually added a much needed function. But alas .... no .... just another verification that they don't listen to the users.
We have been using this "trick" to convert parameters that are real numbers to notes in drawings since I started. The function we have asked for is RTOS (Real to String) with a way to control the number of decimals.
The actual answer to the posted question is .... there is no direct method .... just Martin's "trick".