Is there a way to write values ​​where the last digits of the value are the ones that separate them, but only use 6 digits, plus the exponent?

1 view (last 30 days)
Peter on 22 Jun 2022
Commented: Steven Lord on 22 Jun 2022
I am a designer. I can not get hold of my scientist colleagues right now but am in kind of a hurry. I hope I can explain my problem in an understandable way. My colleagues use Matlab but this graph will be displayed in another application.
I am designing a graph for scientists where the values are 1.054516E18 +-3. The maximum space in the graph is 6 digits plus the exponent.
If I write them out in plain numbers they are:
10 545 159 999 999 999 997
10 545 159 999 999 999 998
10 545 159 999 999 999 999
10 545 160 000 000 000 000
10 545 160 000 000 000 001
10 545 160 000 000 000 002
10 545 160 000 000 000 003
But since there is only room for 6 digits in the graph this will not work.
If I write them out in scientific e notation they come out like this:
1.0545160000000000089 e19
1.0545160000000000089 e19
1.0545160000000000089 e19
1.054516 e19
1.0545160000000000089 e19
1.0545160000000000089 e19
1.0545160000000000089 e19
But the "interesting" part of the numbers doesn't show. AND they also use too many digits.
Is there a way to write these numbers so the discerning parts of the numbers show with only 6 digits plus exponent? It doesn't have to be scientifically correct. What matters is that the graph is readable.
I apologise if I use the wrong terminology and for other mistakes.
Kind regards,
Peter Gibson
Steven Lord
Steven Lord on 22 Jun 2022
@Stephen23 is right. Not all integers can be represented in double precision when your numbers are that large in magnitude.
x = 10545159999999999997;
spacingToNextLargestNumber = eps(x)
spacingToNextLargestNumber = 2048
So how do you know that your value of x has 7 as the 1's digit? [Hint: the stored value doesn't!]
rem(x, 10)
ans = 0
How do you know that what was stored was not supposed to represent something different from x (that is also not exactly representable)?
y = x + 123;
y == x % true is the correct answer
ans = logical
We can add enough to get to the next largest number (thanks to rounding.)
z = x + (spacingToNextLargestNumber/2+1);
z == x % false
ans = logical
If we add something less than that, we don't get to the next largest number.
w = x + (spacingToNextLargestNumber/2-1);
w == x % true
ans = logical
Let's look at the bit patterns of x, y, w, and z.
format hex
[x; y; w; z]
ans = 4×1
1.0e+00 * 43e24afde4c77510 43e24afde4c77510 43e24afde4c77510 43e24afde4c77511
x, y, and w are exactly equal. z differs from x in the last bit.

Sign in to comment.

Accepted Answer

KSSV on 22 Jun 2022
Read about format and fprintf

More Answers (1)

Karim on 22 Jun 2022
the only way I can think of that matlab can deal with such big numbers and precision is by treating them as strings ( perhaps it is also possible by using the symbolic toolbox)
Anyhow, the idea is to do the mathematics in matlab on the offest value. As you indicated the values lie within 1.054516E19 +-3. Hence do all mathematics in the [-3 3] interval, and then overwirite the labels of the plot, see below for an example.
BigNumbers = ["10545159999999999997";
% Some pretend data
N = 50;
x = sort(rand(N,1)*10);
y = randi([-3 3],N,1); % random y data in the [-3 3] interval
% Plot the figure
% Define the tick locations, and label them
ylabel('very Big Numbers')
xlabel('random data')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!