Normalize (remove leading zeros) and then truncate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
Solved! Go to Solution.
- Labels:
-
Math Homework
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you for the start! I will take a look and give it a try.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Oooh I see. Now I just need to figure out how to truncate!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
See below. Try searching the help files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
How about the round function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
OK This is what I came up with. Now I just need to figure out how to put it in a program...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I heard my name called.
David H…here is the document Werner mentioned: http://communities.ptc.com/docs/DOC-4834
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you for the heads up. I forgot to change X to |X| , which is where the error was coming from.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Well spotted