倍率= 10.35;
私は得るべきである
値= 1035;
指数= -2;
私は計算すると10.35を得るでしょう。
、つまり1035 * 10^-2 = 10.35;
私を助けてください。 ありがとうございます
倍率= 10.35;
私は得るべきである
値= 1035;
指数= -2;
私は計算すると10.35を得るでしょう。
、つまり1035 * 10^-2 = 10.35;
私を助けてください。 ありがとうございます
doubleの小数部分が2のべき乗で格納され、10のべき乗と一致する可能性があるため、一般的にこれは不可能です。
例:2のべき乗と3のべき乗を見るとき:1/2 == 2^-1 == 5 * 10^-1のように一致する1/3 == 3^-1 == ??一致がありません。
しかし、あなたはそれを近似することができます。
2のべき乗を求めるのであれば答えがあります。その場合、二重表現(IEEE-754 hereを参照)を見て、正しいビットを抽出することができます。
非常に単純化(C#の場合):
double size = 10.36;
int power = 0;
while (size != (int)size)
{
size *= 10.0;
power--;
}
Console.WriteLine("{0} * 10 to the {1}", size, power);
私はもう少しよりエレガントな解決策を見つけることができると思ったと確信しているけど。
これは、あなたが大きな数字(103600と言う)を持っていて、あるパワー(1036 * 10^2)に最も小さな値を得たい場合とは逆の方法ではありません。
私は非常に似たようなことをしなければなりませんでした。以下はPythonの解決方法です(それほどうまくテストされていません)。
def normalize(value, fdigits=2):
"""
Convert a string representing a numerical value to value-digit/exponent form.
Round the fractional portion to the given number of digits.
value the value (string)
fdigits the number of digits to which to round the fractional
portion
"""
# if empty string, return error
if not value:
return None
# split value by decimal
v = value.split('.')
# if too many decimals, return error
if len(v) > 2:
return None
# add empty string for fractional portion if missing
elif len(v) == 1:
v.append('')
# assign whole and fractional portions
(w, f) = v
# pad fractional portion up to number of significant digits if necessary
if len(f) < fdigits:
f += ('0' * (fdigits - len(f)))
# if the number of digits in the fractional portion exceeds the
# number of digits allowed by fdigits
elif len(f) > fdigits:
# convert both portions to integers; use '0' for whole portion if missing
(wi, fi) = (int(w or '0'), int(f[:fdigits]))
# round up if first insignificant digit is gteq 5
if int(f[fdigits]) >= 5:
fi += 1
# roll whole value up if fractional portion rounds to a whole
if len(str(fi)) > fdigits:
wi += 1
fi = 0
# replace the whole and fractional strings
(w, f) = (str(wi), ("%0" + str(fdigits) + "d") % fi)
# derive value digits and exponent
n = w.lstrip() + f
l = len(n)
x = -fdigits
n = n.rstrip('0')
x += (l - len(n))
# return value digits and exponent
return (int(n), x)
どのような質問ですか? – SilentGhost