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

How To: Format a real number relation in a note?

SOLVED

Re: How To: Format a real number relation in a note?

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:

  • FLOOR - rounds any real number down to the next closest value with the number of digits specified. The format is floor(real number,number of digits to round to)
  • ITOS - the "integer-to-string" function. The format is just itos(integer).
  • EXTRACT - grabs a portion of a string. The format is extract(string, start position, number of chars to extract)
  • STRING_LENGTH - provides the length of a string in characters. The format is string_length(string)

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.

Thanks!

-Brian

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.

Re: How To: Format a real number relation in a note?

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?

Re: How To: Format a real number relation in a note?

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...

Re: How To: Format a real number relation in a note?

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.

Highlighted

Re: How To: Format a real number relation in a note?

Antonius,

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
number_digits=3
x=12.98799
x=x+5/10^(number_digits+1)

/* output variables
t1=floor(x,number_digits)
t2=itos(t1*10^number_digits)
t3=extract(t2,1,string_length(t2)-number_digits)+"."+extract(t2,string_length(t2)-number_digits+1,number_digits)

Martin Hanak


Martin Hanák

Re: How To: Format a real number relation in a note?

Very clever!

Re: How To: Format a real number relation in a note?

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.

Re: How To: Format a real number relation in a note?

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?"

(Facepalm)


Re: How To: Format a real number relation in a note?

Now he's just showijng off.

Re: How To: Format a real number relation in a note?

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".