How to delete/cancel trailing zeros in complex and imaginary numbers?

Whenever I use or create imaginary and complex numbers, they are always saved with trailing zeros, for example 1.22000000 + 2.150000000i . I want to mimic the behaviour and structure of fixed-point while still being in floating-point. I want 1.22 + 2.15i. How do I delete or cancel the trailing zeros? I have tried different ways and as soon as the imaginary unit is present, the zeros come back.

1 commentaire

"I want 1.22 + 2.15i."
Why?
What specific operations are you performing that cannot be achieved using normal binary floating point numbers?

Connectez-vous pour commenter.

Réponses (2)

The way data is stored and the way it is displayed is not necessarilly connected. You can influence the way data is presented with the format function. If you want more control, you need the fprintf or sprintf functions.
format shortG
1.22 + 2.15i
ans =
1.22 + 2.15i

3 commentaires

Hi, thank you for the answer. I have tried this method and it merely displays the number as shown and does not change the literal number that is stored. I want A, for example, to be 1.22 + 2.15i and not just be displayed as that.
@Joshua Ford You can't have that with double precision numbers. The storage is binary floating point and you can't change that. In fact neither of your 1.22 or 2.15 example numbers can be represented exactly in double precision floating point, and a decimal conversion will have non-zero digits beyond the ones you display above. If you insist on representing these decimal numbers exactly, then you will have to resort to fixed point or Symbolic Toolbox which will greatly slow down your code.
You already have the values very close to that. You can round to a specific number of decimals, but due to what James wrote you can't get close than this:
round(1.223 + 2.148i,2)
ans = 1.2200 + 2.1500i

Connectez-vous pour commenter.

a = 1.22000000 + 2.150000000i
a = 1.2200 + 2.1500i
fprintf("%g + %gi\n", real(a), imag(a));
1.22 + 2.15i
fprintf("%.2f + %.2fi\n", real(a), imag(a));
1.22 + 2.15i

3 commentaires

If you have an array of complex vectors, you can print them out as follows:
aa = repmat(1.22000000 + 2.150000000i, 5,1);
fprintf("%g + %gi\n", [real(aa(:)), imag(aa(:))]' );
1.22 + 2.15i 1.22 + 2.15i 1.22 + 2.15i 1.22 + 2.15i 1.22 + 2.15i
Does this store the numbers like this or does it only display the numbers as shown?
>> Does this store the numbers like this or does it only display the numbers as shown?
The storage has not changed with the choice of display.
Below shows two different variables set to a value with and without trailing zeros. But the two number numbers are represented internally exactly the same way.
a = 1.22 + 2.15i;
b = 1.22000000 + 2.150000000i;
a == b
ans = logical
1

Connectez-vous pour commenter.

Catégories

Commenté :

Rik
le 17 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by