计算浮点数的位数。

时间:2022-09-23 17:35:31

Is there any efficient way (without converting the float into a string) to obtain the number of digits a floating-point number consists of (independent of its length and precision) ?

是否有任何有效的方法(不将浮点数转换成字符串)来获得浮点数由(独立于其长度和精度)所组成的数字的位数?


On that way I can implement a fairly good, portable, problematic-less function for comparison/conditioning by multiplying the float by the number of the digits it consists of.

通过这种方式,我可以实现一个相当好的、可移植的、没有问题的功能,通过将浮点数乘以它所包含的数字的数目来进行比较/调节。

1 个解决方案

#1


3  

Q: Is there any efficient way to obtain the number of digits a floating-point number?
A: I doubt it.

问:有没有有效的方法来获得浮点数的位数?我对此表示怀疑。

Every finite FP number is exact, but maybe not the exact value one thinks.

每个有限的FP数字都是精确的,但可能不是一个人想的精确值。

Due to typical binary64 implementation of a double,
double x = 0.53, x value: .5300000000000000266453525910037569701671600341796875, 52 digits.
double x = 0.1, x value:.1000000000000000055511151231257827021181583404541015625 55 digits.
The next closest double to mathematical 0.1 is .09999999999999999167332731531132594682276248931884765625 with 56 digits.

由于典型的binary64实现的双,双x = 0.53, x值:. 530000000000000000000000266453525910037569701600341796875,52位数字。双x = 0.1, x值:。1000000000000000055511151231257827021181583404541015625 55位。下一个最接近数学0.1的是。09999999999999999999999991673153153113259468476248931884765625和56位数字。

DBL_MAX: in decimal, typically about 300 digits 17976931348623158... ending with 6728515625..

DBL_MAX:在十进制中,通常约为300位数字17976931348623158……以6728515625 . .

DBL_MIN: typically 0.000000(~300 zeros) 22250738585072014... (maybe about 700 more digits).

DBL_MIN:典型的0.000000(~300个零)22250738585072014…(可能多出700个数字)。

Comparison of FP numbers need not determine the number of digitis in its decimal representation. To compare FP numbers, use the usual relationship operators >, >=, ==, etc.

FP数字的比较不需要用十进制数来确定数字的数量。为了比较FP数字,使用通常的关系运算符>,>=,==,等等。

Theses values are illustrative. YMMV.

这些值是说明性的。YMMV。

#1


3  

Q: Is there any efficient way to obtain the number of digits a floating-point number?
A: I doubt it.

问:有没有有效的方法来获得浮点数的位数?我对此表示怀疑。

Every finite FP number is exact, but maybe not the exact value one thinks.

每个有限的FP数字都是精确的,但可能不是一个人想的精确值。

Due to typical binary64 implementation of a double,
double x = 0.53, x value: .5300000000000000266453525910037569701671600341796875, 52 digits.
double x = 0.1, x value:.1000000000000000055511151231257827021181583404541015625 55 digits.
The next closest double to mathematical 0.1 is .09999999999999999167332731531132594682276248931884765625 with 56 digits.

由于典型的binary64实现的双,双x = 0.53, x值:. 530000000000000000000000266453525910037569701600341796875,52位数字。双x = 0.1, x值:。1000000000000000055511151231257827021181583404541015625 55位。下一个最接近数学0.1的是。09999999999999999999999991673153153113259468476248931884765625和56位数字。

DBL_MAX: in decimal, typically about 300 digits 17976931348623158... ending with 6728515625..

DBL_MAX:在十进制中,通常约为300位数字17976931348623158……以6728515625 . .

DBL_MIN: typically 0.000000(~300 zeros) 22250738585072014... (maybe about 700 more digits).

DBL_MIN:典型的0.000000(~300个零)22250738585072014…(可能多出700个数字)。

Comparison of FP numbers need not determine the number of digitis in its decimal representation. To compare FP numbers, use the usual relationship operators >, >=, ==, etc.

FP数字的比较不需要用十进制数来确定数字的数量。为了比较FP数字,使用通常的关系运算符>,>=,==,等等。

Theses values are illustrative. YMMV.

这些值是说明性的。YMMV。