Truncating a Number to 5 Significant Digits in MATLAB

  • MATLAB
  • Thread starter asset101
  • Start date
In summary, to create a Matlab Function file that converts an integer with more than 5 significant digits to an integer with 5 significant digits, you can use the logic of taking the base 10 logarithm of the number and adding 1 to it, then rounding down. This will give you the number of digits in the original number. If the number is greater than 99999, you can use this information to truncate the number to 5 significant digits.
  • #1
asset101
11
0
Write a Matlab Function file that takes one integer in and converts it to an integer with only 5 significant digits.
Eg Trunc(123)--------ans=123
Tunc(123345678) ----ans=12345e+03
----------------------------------------------------------------------------------------

My thoughts
First see that the max number that doesn't get truncated is 99999.

set up some logic
if n>99999
insert logic
else
n=n
(n is the value passed into the function).
Can anyone give us a hand?
 
Physics news on Phys.org
  • #2
HINT: What does taking the base 10 logarithm of a number (in MATLAB, log10) tell you?
 
  • #3
Taking the base 10 is to find out how many times you would have to multiply by 10 to get the number. As i understand it to be.
I am still a little trouble with getting the missing logic.
Can you push me just a little bit further.
 
  • #4
asset101 said:
Taking the base 10 is to find out how many times you would have to multiply by 10 to get the number. As i understand it to be.
I am still a little trouble with getting the missing logic.
Can you push me just a little bit further.

Actually, you had the answer, you just phrased it in a manner which isn't so helpful ;-)

Taking the log10 of any number (greater than 1) gives you 1 less than the number of digits in that number.

Consider:
log10(10) = 1
log10(99) = 1.9956

So if you add 1 to log10(number > 1) and then rounded down, you'd get the number of digits in the number the user inputs.

>> digits = floor(log10(number) + 1)

And I think that's about as much as I can give you without giving away everything.

EDIT: Clarification
 
Last edited:
  • #5
Thanks mate was able to get it in the end sorry about the cross posting, novice user here.
 

1. What is truncating a number to 5 significant digits in MATLAB?

Truncating a number to 5 significant digits in MATLAB means limiting the number of digits in a numerical value to 5, starting from the leftmost non-zero digit. This is commonly used in scientific calculations to improve accuracy and avoid rounding errors.

2. How do I truncate a number to 5 significant digits in MATLAB?

To truncate a number to 5 significant digits in MATLAB, you can use the built-in function round() with the argument n set to 5. This will round the number to the nearest 5th significant digit. Alternatively, you can use the format command to set the display format to short e, which automatically truncates numbers to 5 significant digits.

3. Can I specify the position of the decimal point when truncating a number in MATLAB?

Yes, you can specify the position of the decimal point when truncating a number in MATLAB. This can be done by using the format command with the argument short e and specifying the desired number of decimal places after the e symbol. For example, format short e will display numbers with 5 significant digits and 4 decimal places after the e symbol.

4. Will truncating a number to 5 significant digits affect its accuracy?

Truncating a number to 5 significant digits in MATLAB will not affect its accuracy as long as the original number has more than 5 significant digits. However, if the original number has less than 5 significant digits, truncating it to 5 significant digits may result in a loss of precision.

5. Can I use truncation in other programming languages besides MATLAB?

Yes, truncation is a commonly used mathematical operation and can be performed in other programming languages as well. The method of truncation may vary depending on the language, but the concept remains the same – limiting the number of digits in a value to a specified number of significant digits.

Similar threads

  • Biology and Chemistry Homework Help
Replies
2
Views
2K
  • Electrical Engineering
Replies
4
Views
708
  • Programming and Computer Science
Replies
17
Views
992
  • Other Physics Topics
Replies
5
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
Replies
9
Views
1K
Back
Top