How to determine the last non-zero decimal digit in a float?

I am writing a library of float printing and formatting and want to avoid printing zero digits.

To do this, I would like to pinpoint the last non-zero digit in the first N decimal places after the decimal point. I wonder if there is an effective way to do this.

+3
source share
4 answers

This (non-trivial) problem has been completely resolved. The idea is to print enough numbers so that if you converted the printed numbers back to a floating point number, you will get exactly the number from which you started.

" ", . . .

+3

float .

, , , , ,

+1

, char[N] . , .

, , , 2^(-n) n .

, 2^(-n), n . .

However, this is only a partial solution, since rounding can introduce additional trailing zeros.

0
source
std::cout.precision(n);

// where n are the numbers you want to display after the decimal. If zero is present before the accuracy limit, but after the decimal number, this will be automatically prevented.

eg. std::cout.precision(5);

then my conid is rated as 5.55000, only 5.55 will be printed

0
source

All Articles