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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Normalize (remove leading zeros) and then truncate?

ptc-4451268
1-Newbie

Normalize (remove leading zeros) and then truncate?

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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?

View solution in original post

16 REPLIES 16

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.

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

I am sorry, but I don't understand how taking the Log of 0.000xyz can help me get 0.xyz. Can you explain that to me?

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?

Oooh I see. Now I just need to figure out how to truncate!

MikeArmstrong
5-Regular Member
(To:ptc-4451268)

See below. Try searching the help files.

Clipboard01.jpg

Thank you for the input. I am still trying to write the program to normalize - but I have to truncate to a specific a specific amount of decimals, For instance:

X = 0.00112

D=2

newX=0.0011

MikeArmstrong
5-Regular Member
(To:ptc-4451268)

How about the round function?

Clipboard01.jpg

I guess you should not truncate but round and not to a number of decimals but to a number of significant digits, right?

While dividing multiplying by and with powers of ten as I roughly outlined may be clearer to follow, a more efficient way would be to use the round function and its second argument. You can determine that second argument depending on the number of significant digits wanted and the truncated log of the number - think over it an dgive it a try.

07.04.png

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

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.

FredLusk
12-Amethyst
(To:Werner_E)

I heard my name called.

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

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

MikeArmstrong
5-Regular Member
(To:ptc-4451268)

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.

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

MikeArmstrong
5-Regular Member
(To:ptc-4451268)

Well spotted

Top Tags