Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
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.
Solved! Go to Solution.
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?
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!
See below. Try searching the help files.
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
How about the round function?
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.
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.
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!!!
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.
Well spotted