MatLab Not Rounding Large Trailing Numbers
1 view (last 30 days)
Show older comments
I need to round numbers to a 5 decimal place. But my process spits out number that are like -6.8449e+30 and when you do floor(-6.8449e+30) you still get -6.8449e+30.
Is there a way I can get around this? I have tried round(-6.8449e+30*1000000)/100000 and it still spits out -6.8449e+30.
Also, is there a way I can make the trailing 0s go away too? I don't want -6.844958000000000000......
0 Comments
Answers (1)
Walter Roberson
on 11 Feb 2016
You cannot do that. Binary floating point numbers can resolve down only to one part in 2^53, which is about 1 part in 10^16. You are dealing with numbers on the order of 5*10^30 so the smallest it can resolve down is roughly 5*10^14, which is about 19 more decimal places than you want to be able to round to. Even if MATLAB were using 128 bit floating point representation it is unlikely you would be able to resolve down to 0.00001 from a number in the order 5*10^30
I think there is a confusion here between "decimal places" and "digits". "Decimal places" refer to something smaller than the units place -- the 1/10th position, 1/100th, 1/1000th and so on. It sounds like instead you want to round to digits.
For that:
numDig = 5;
mag = floor(log10(abs(x)));
scale = 10.^(mag-numDig+1);
new_x = round(x ./ scale) .* scale;
This is vectorized so it will handle arrays of x, adjusting each of them individually according to its magnitude.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!