Truncating a Number to 5 Significant Digits in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter asset101
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around creating a MATLAB function that truncates an integer to five significant digits. Participants explore the logic and mathematical principles necessary for implementing this functionality, including the use of logarithms.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant suggests that the maximum number that should not be truncated is 99999.
  • Another participant hints at using the base 10 logarithm to determine the number of digits in a number.
  • A participant explains that taking the logarithm helps find how many times 10 must be multiplied to reach the number, indicating a need for further logic to complete the function.
  • Further clarification is provided on how to calculate the number of digits using the formula: digits = floor(log10(number) + 1).
  • A participant expresses gratitude for the hints and indicates they were able to complete the task.

Areas of Agreement / Disagreement

Participants generally agree on the approach of using logarithms to determine the number of digits, but the specific implementation details and logic for truncation remain less clear and are not fully resolved.

Contextual Notes

The discussion does not fully resolve the implementation details of the truncation logic, and assumptions about the input range and behavior of the function are not explicitly stated.

asset101
Messages
10
Reaction score
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
HINT: What does taking the base 10 logarithm of a number (in MATLAB, log10) tell you?
 
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.
 
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:
Thanks mate was able to get it in the end sorry about the cross posting, novice user here.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K