计算浮点数字的位数 [英] Count the number of digits of a floating-point number
问题描述
通过这种方式,我可以通过将float与其组成的数字相乘来实现比较/调节的相当好的,便携式的,无问题的函数。 / p>
问:是否有任何有效的方法获得浮点数的位数?
答:我怀疑这个问题。
每个有限的FP数字都是精确的,但也许不是人们想象的确切数值。
$ b
由于典型的 binary64 实现了 double
,
double x = 0.53
, x
value: .5300000000000000266453525910037569701671600341796875
,52位数。
doub le x = 0.1
, x
值: .1000000000000000055511151231257827021181583404541015625
55位数。
下一个最接近的 double
到数学 0.1
是 .09999999999999999167332731531132594682276248931884765625
56位数字。
$ b
DBL_MAX
:十进制,通常约300位 17976931348623158 ...
以结尾6728515625。
。
DBL_MIN
:通常 0.000000(〜300零)22250738585072014 ... 700多位数字)
。
FP数字的比较不需要用十进制表示来决定digitis的数量。要比较FP号码,请使用通常的关系运算符> ;,> =,==
等。
这些值是说明性的。 YMMV。
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.
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.
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.
DBL_MAX
: in decimal, typically about 300 digits 17976931348623158...
ending with 6728515625.
.
DBL_MIN
: typically 0.000000(~300 zeros) 22250738585072014... (maybe about 700 more digits)
.
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.
Theses values are illustrative. YMMV.
这篇关于计算浮点数字的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!