Skip to main content
1-Visitor
April 7, 2014
Solved

Normalize (remove leading zeros) and then truncate?

  • April 7, 2014
  • 16 replies
  • 5402 views

Hello!

I am a student, and I am working on a homework problem that I can't quite solve. I need to:

"Write a subroutine or function which will truncate a variable X to D decimal places after normalization. For example:

D = 3, X=0.0069347

Should be returned as:

X = 0.00693

"

Can someone help me write a program to do this? I don't typically write programs in MathCAD, and i really don't know where to start.

PS: X can be any number, not just 0.0069347.

Best answer by Werner_E

Whats the log of 0.004321 and what the log of 54321? Especially if you round up that log to the next highest integer using ceil(). Lets call the new value e. What do get if you divide your number by 10^e?

16 replies

Werner_E
25-Diamond I
April 7, 2014

I don't typically write programs in MathCAD

I guess thats exactly what you are supposed to learn.

A starting sure would be to take the logarithm to the base of 10 of the number and reason, how this log (you may use ceil or floor on it) can help you you transform any number to 0.xyz. Then multiply by 10^D, use trunc and reverse the procedure.

Next steps would be to eal with negative numbers, numver zero and with people demanding 0 significant digits or even less.

Give it a try and if you encounter problems post your sheet here.

1-Visitor
April 7, 2014

Thank you for the start! I will take a look and give it a try.

1-Visitor
April 7, 2014

OK This is what I came up with. Now I just need to figure out how to put it in a program...

Werner_E
25-Diamond I
April 7, 2014

if you simply nest those functions instead of assigning the outcome to various variables and use those in the next calculation, you will have sort of a oneliner.

BTW, Fred Lusk had posted something you may be interested in in that respect some time ago.

14-Alexandrite
April 7, 2014

I heard my name called.

David H…here is the document Werner mentioned: http://communities.ptc.com/docs/DOC-4834

1-Visitor
April 7, 2014

I think this is good enough for what the requirements are. I wouldn't say that it is elegant, but since this is my first assignment, I think its OK.

Thanks again for all of the help!!!

1-Visitor
April 7, 2014

I think you need to check your workings.

You don't use F so this could be eliminated and the X.f does not return the same value as P.

1-Visitor
April 7, 2014

Thank you for the heads up. I forgot to change X to |X| , which is where the error was coming from.