How to delete/cancel trailing zeros in complex and imaginary numbers?
6 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 17 May 2021
"I want 1.22 + 2.15i."
Why?
What specific operations are you performing that cannot be achieved using normal binary floating point numbers?
Answers (2)
Rik
on 17 May 2021
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
3 Comments
Paul Hoffrichter
on 17 May 2021
Edited: Paul Hoffrichter
on 17 May 2021
a = 1.22000000 + 2.150000000i
fprintf("%g + %gi\n", real(a), imag(a));
fprintf("%.2f + %.2fi\n", real(a), imag(a));
3 Comments
Paul Hoffrichter
on 17 May 2021
>> 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
See Also
Categories
Find more on Numbers and Precision 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!